【问题标题】:Displaying results in array with condition into a table将带有条件的数组结果显示到表格中
【发布时间】:2012-10-07 04:59:02
【问题描述】:

我正在尝试根据行和列在数组中显示结果。我已经尝试了下面的代码,但是下一行显示了 2 个结果我希望它是同一行但不同的列。

这是我的代码

<table id="TimeTable">
      <thead>
      <th style="width:20px; padding-right:10px;">No.</th>
      <th style="width:160px; padding-right:10px;">Monday</th>
      <th style="width:160px; padding-right:10px;">Tuesday</th>
      <th style="width:160px; padding-right:10px;">Wednesday</th>
      <th style="width:160px; padding-right:10px;">Thursday</th>
      <th style="width:160px; padding-right:10px;">Friday</th>
      </thead>
      <tbody>
        <?php foreach ($rows as $row):

                for($r=1; $r<=8; $r++) {

                    if ($row->row == $r) {
        ?>
            <tr>

            <td style="text-align: center; padding-right: 10px; padding-right: 10px;"><strong><?php echo $r; ?>.</strong></td>

            <?php for($c=1; $c<=5; $c++) { ?>

                <td><?php 

                    if(($row->col == $c)) {
                        echo $row->subject." <br />Classroom: ".$row->classroom." <br />Time: ".$row->time; 
                    } 

                ?></td>

                     <?php  } //col for loop ?>


            </tr>

        <?php       } //if  
                } // row for loop 
         endforeach; ?>
        </tbody>
    </table>

如果row = 1 它现在应该在同一行,它正在创建 5。

非常感谢您的建议和帮助。

【问题讨论】:

  • 我能看到$rows的值吗?
  • 您好,我将 foreach 循环的位置更改为内部 td 并且它起作用了。谢谢

标签: php html arrays html-table


【解决方案1】:

抱歉,我能在这里解决的任何问题是更正

<table id="TimeTable">
      <thead>
      <th style="width:20px; padding-right:10px;">No.</th>
      <th style="width:160px; padding-right:10px;">Monday</th>
      <th style="width:160px; padding-right:10px;">Tuesday</th>
      <th style="width:160px; padding-right:10px;">Wednesday</th>
      <th style="width:160px; padding-right:10px;">Thursday</th>
      <th style="width:160px; padding-right:10px;">Friday</th>
      </thead>
      <tbody>
        <?php for($r=1; $r<=8; $r++) {  ?>
            <tr>

            <td style="text-align: center; padding-right: 10px; padding-right: 10px;"><strong><?php echo $r; ?>.</strong></td>

            <?php for($c=1; $c<=5; $c++) { ?>

                <td><?php 
                    foreach ($rows as $row):
                        if(($row->row == $r) && ($row->col == $c)) {
                            echo $row->subject." <br />Classroom: ".$row->classroom." <br />Time: ".$row->time; 
                        } 
                    endforeach;
                ?></td>

                     <?php  } //col for loop ?>

            </tr>

        <?php    
                } // row for loop 
          ?>
        </tbody>
    </table>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-13
    • 2016-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-19
    • 2021-01-12
    相关资源
    最近更新 更多