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 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 プラグイン - datatables を試すことができます

カスタムソート:

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 に答える