Рисование пустых фиктивных столбцов в цикле - PullRequest
0 голосов
/ 27 ноября 2018

Учтите, что следующему массиву данных присвоено значение "$ my_array".Я включил снимок экрана с ожидаемым выводом на основе данных, приведенных в нижней части этого поста.

Примечание: по какой-то причине я не могу изменить вставку данных в базу данных, поэтому мне нужнорисовать пустые столбцы на основе этой структуры.

Array
(
[1] => Array //This is row #1
    (
        [0] => Array
            (
                [0] => column 2 text
                [1] => column2 //column identifier
            )

        [1] => Array
            (
                [0] => column 3 text
                [1] => column3 //column identifier
            )

        [2] => Array
            (
                [0] => column 4 text
                [1] => column4 //column identifier
            )
    )

[2] => Array //This is row #2
    (
        [0] => Array
            (
                [0] => column 1 text
                [1] => column1 //column identifier
            )

        [1] => Array
            (
                [0] => column 4 text
                [1] => column4 //column identifier
            )

    )
)

Вот что:

Я хочу нарисовать 4 столбца в каждой строке, независимо от того, возвращает ли строка 1, 2 или 3 столбца данных.Я могу добавить идентификатор, по которому столбец должен быть нарисован.

Пока что я что-то вроде этого: Примечание: я действительно не знаю, как определить, являются ли некоторые изстолбцы не в строке данных и добавить пустое место, так что это то, что я до сих пор:

foreach($my_array as $row) {
    echo '<div class="row">'; //draw the rows

    //I used for loop instead of foreach to create four columns
    for($x = 0; $x < 4; $x++) {
        //draw the column here

        //write column text data $row[$x][0]
    }

    echo '</div>';
}

enter image description here

Ответы [ 3 ]

0 голосов
/ 27 ноября 2018

Исходя из того, что вы всегда хотите иметь 4 столбца, вы можете сделать это следующим образом:

$columns = ['column1','column2','column3','column4'];        // create list to check for identifier
foreach($my_array as $row) {
  echo '<div class="row">';
  $drawColumns = [0,0,0,0];                                  // if 0, col empty, if 1 data exist
  $columnIndex = [0,0,0,0];
  foreach($row as $index => $column) {                                  // loop over row to fill $drawColumns 
    $columnPosition = array_search($column[1],$columns);     // array_search gives index of the found entry
    $drawColumns[$columnPosition] = 1;                       // mark column as not empty
    $columnIndex[$columnPosition] = $index;         // store the original index of the column in the row
  }
  for($x = 0; $x < 4; $x++) {                                // just loop over drawColumns 
    if ($drawColumns[$x] == 1) {                             // draw data if exists
      echo '<div class="col">'.$row[$columnIndex[$x]][0].'</div>';
    } else {                                                 // else draw empty column
      echo '<div class="col">empty column</div>';
  }
  echo '</div>';
}
0 голосов
/ 27 ноября 2018
foreach($my_array as $row) {
    echo '<div class="row">';

    for($x = 1; $x <= 4; $x++) {
      $colval = '-'; // default value to output for empty column;
                     // you can also use an empty string or other placeholder
      foreach($row as $val) {
        if( $val[1] == 'column'.$x ) {
          $colval = $val[0]; // take this value, if entry with columnX exists
          break;
        }
      }
      echo '<div class="col">', $colval, '</div>';
    }

    echo '</div>';
}
0 голосов
/ 27 ноября 2018
foreach($row as $r){
    $colid[] = $r['1'];
}
for($x = 0; $x < 4; $x++) {
    if(in_array($row[$x]['1'],$colid)){
        echo '<div class="col-md-4">'.$row[$x]['1'].'</div>';
    } else {
        echo '<div class="col-md-4">Empty Cell</div>';
    }
}
...