简体   繁体   中英

echo a multidimensional array into multiple html table rows

I have a query that I'm running and it will output an unknown number of results. I want to display these results in a table of 5 columns. So I need the array print until the sixth result and then start a new row.

The way I tried to do it was to take the original array and chunk it into blocks of 5.

$display=array_chunk($row_Classrooms,5);

which gives me an array like this.

  Array ( 
      [0] => Array ( 
            [0] => Array ( 
                  [id_room] => 1 
                  [Name] => Classroom 1 
                  [class] => Yes 
                  ) 
            [1] => Array ( 
                  [id_room] => 5 
                  [Name] => Classroom 2 
                  [class] => Yes 
                  ) 
            [2] => Array ( 
                  [id_room] => 6 
                  [Name] => Classroom 3 
                  [class] => Yes 
                  ) 
            [3] => Array ( 
                  [id_room] => 7 
                  [Name] => Classroom 4 
                  [class] => Yes  
                  ) 
            [4] => Array ( 
                 [id_room] => 8 
                 [Name] => Classroom 5 
                 [class] => Yes 
                 ) 
            ) 
     [1] => Array ( 
           [0] => Array ( 
                  [id_room] => 9 
                  [Name] => Classroom 6 
                  [class] => Yes 
                  ) 
             ) 
   ) 

I'm then trying to echo this out with a pair of while loops, like such.

while ($rows = $display) {
                echo '<tr>';
                    while ($class = $rows) {
                        echo'<td>'.$class['name'].'<br>
                            <input name="check'.$i.' type="checkbox" value="'.$class['id_room'].'></td>';
                            $i++;
                    }
                echo '</tr>';
            }

When I run this it apparently gets stuck in a never ending loop because nothing gets displayed but the browser just keeps chewing up more and more memory :)

The while statements are wrong. Have a look at here - in your while -statement you are always assigning the complete $display - not one entry.

You could try using while(($rows = array_shift($display)) !== false) - that will always get the first array item until there are no more items.

The same case in the second while-statement.

I ended up replacing the while loops with foreach loops instead which solved the issue.

    foreach ($display as $rows) {
            echo '<tr class="popup">';
                foreach($rows as $class) {
                    if(isset($row_Rego)){
                        $exist=NULL;
                        $exist=array_search($class['id_room'], array_column($row_Rego, 'id_room'));
                    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM