0

我有一个数组

Array ( 
    Array ( 
        [id] => 1 ,
        [schedule_id] => 4 ,
        [subject] => Subject 1 ,
        [classroom] => 1 ,
        [time] => 08:00:00 ,
        [col] => 1 ,
        [row] => 1 
    ), 
    Array ( 
        [id] => 2 ,
        [schedule_id] => 4 ,
        [subject] => Subject 2 ,
        [classroom] => 1 ,
        [time] => 08:00:00 ,
        [col] => 2 ,
        [row] => 1 
    ), 
    Array ( 
        [id] => 3 ,
        [schedule_id] => 4 ,
        [subject] => Subject 3 ,
        [classroom] => 2 ,
        [time] => 09:00:00 ,
        [col] => 1 ,
        [row] => 2 
    ), 
    Array ( 
        [id] => 4 ,
        [schedule_id] => 4 ,
        [subject] => Subject 4 ,
        [classroom] => 2 ,
        [time] => 09:00:00 ,
        [col] => 2 ,
        [row] => 2 
    ) 
)

我想根据 col 和 row 值以表格格式显示它

col 1 & row 1           col 2 $ row 1   
1st array data          2nd array data
Subject, room, time     Subject, room, time
1,  1,  08:00           2,  1,  08:00

col 1 $ row 2           col 2 $ row 2   
3rd array data          4th array data
Subject, room, time     Subject, room, time
3,  2,  09:00           4,  2,  08:00

我是数组的新手,需要您支持才能对这张表进行排序。

4

3 回答 3

1
if(count($array)>0){ // check if array has elements in it  
 echo "<table>";  
 // print table header  
 echo "<thead><tr>";  
 foreach($array[0] as $key=>$value){  
    echo "<th>$key</th>";  
 }  
 echo "</tr></thead>";

 // print rows  
 echo "<tbody>";
 foreach($array as $index=>$row){ 
  echo '<tr>';
  foreach($row as $key=>$value){  
   echo "<td>$value</td>";  
  }   
  echo "</tr>";  
 }  
 echo "</tbody></table>";  
}

对于排序,您可以尝试 jquery 插件 - 数据表

自定义排序:

foreach($array as $index=>$arr){
 for($i=$index; $i< count($array); $i++){
  if(strtotime($array[$index]['time']) > strtotime($array[$i]['time'])){ // acsending order
   $temp = $array[$index];
   $array[$index] = $array[$i];
   $array[$i] = $temp;
  }
 }
}
于 2012-10-16T10:37:16.353 回答
0

尝试这个

<table>
<tr>
   <th>id</th>
   <th>schedule_id</th>
   <th>subject</th>
   <th>classroom</th>
   <th>time</th>
   <th>col</th>
   <th>row</th>
</tr>
foreach($myarray as $row)
{
    echo("<tr>");
       echo("<td>".$row['id']."</td>");
       echo("<td>".$row['schedule_id']."</td>");
       echo("<td>".$row['subject']."</td>");
       echo("<td>".$row['classroom']."</td>");
       echo("<td>".$row['time']."</td>");
       echo("<td>".$row['col']."</td>");
       echo("<td>".$row['row']."</td>");
    echo("</tr>");
}
</table>
于 2012-10-16T10:37:22.530 回答
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>
于 2012-10-19T04:37:08.040 回答