将带有条件的数组中的结果显示到表中

时间:2012-10-17 01:22:33

标签: php html arrays html-table

我正在尝试根据行和列显示数组中的结果。我已经尝试了下面的代码但是下一行显示了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>

Here is the table what i am getting

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

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

1 个答案:

答案 0 :(得分:0)

对不起,我能解决的任何问题都是纠正

<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>