Reputation: 1049
I have a array
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
)
)
I want to display it in table format according to col and row value
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
I am new to arrays and need you support to sort this table.
Upvotes: 0
Views: 2211
Reputation: 1049
Here is what i did to display results
<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>
Upvotes: 0
Reputation: 363
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>";
}
for sorting you can try jquery plugin - datatables
custom sorting:
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;
}
}
}
Upvotes: 1
Reputation: 26386
Try this
<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>
Upvotes: 0