简体   繁体   English

将多维数组回显到多个html表行中

[英]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. 我想在5列的表格中显示这些结果。 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. 我尝试这样做的方法是将原始数组分成块,分成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循环将其回显。

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. while语句是错误的。 Have a look at here - in your while -statement you are always assigning the complete $display - not one entry. 看看这里 -在您的while语句中,您总是分配完整的$display display- while不是一个条目。

You could try using while(($rows = array_shift($display)) !== false) - that will always get the first array item until there are no more items. 您可以尝试使用while(($rows = array_shift($display)) !== false) -这将始终获得第一个数组项,直到没有更多项为止。

The same case in the second while-statement. 第二个while语句中的情况相同。

I ended up replacing the while loops with foreach loops instead which solved the issue. 我最终用foreach循环替换了while循环,从而解决了问题。

    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'));
                    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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