2

在我的网站中,我想在页面中显示学生详细信息。我将表格用于设计目的。我需要的是,我想在每行显示两个学生。因此,如果数据库中共有 10 个学生,那么显示页面中应该有 5 行,每行有两个学生。

我怎样才能做到这一点?

4

2 回答 2

2
$students = Array('S1', 'S2', 'S3', 'S4', 'S5', 'S6', 'S7', 'S8', 'S9', 'S10');
$html = '<table>';
for ($i = 0; $i<count($students); $i+=2)
{
    $html.= '<tr>
                <td>'.$students[$i].'</td>
                <td>'.$students[$i+1].'</td>
            </tr>'; 
}
$html.= '</table>';
于 2013-10-10T08:03:27.577 回答
0

So let's say that you have an array with your 10 students, for example $myStudents

To create your table try this code

<table>
<tr>
  <td>Student Name</td>
  <td>Student Name</td>
</tr>
<?php
 $cols = 2;
 $colNum=0;
 $trOpened = false;
 for($i=0;$i<count($myStudents);$i++):

    if( $colNum % 2 != 0 || $colNum==0 ){
       echo "<tr>";
        $trOpened = true;
    }
   ?>
     <td><?=$myStudents[$i]['StudentName']?></td>
 <?php
   $colNum++;
   if(  $trOpened == true && $colNum % 2 == 0 ){
        echo "</tr>";
        $trOpened = false;
        $colNum = 0;
   }

 endfor;


   ?>
</table>
于 2013-10-10T08:12:04.260 回答