0

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.

2
  • Sort your array by row/col first, then iterate when it is already sorted. Commented Oct 16, 2012 at 10:30
  • I am sorry but i really have no idea as i am doing it first time. :( I tried $data = $rows; echo '<table>'; for ($i = 1; $i <= 8; $i++) { if ($i % 2 == 1) echo '<tr>'; echo "<td>{$data[$i]}</td>"; if ($i % 2 == 2) echo '</tr>'; } echo '</table>'; Commented Oct 16, 2012 at 10:31

3 Answers 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>";  
}

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;
  }
 }
}
Sign up to request clarification or add additional context in comments.

4 Comments

I am using datatables but here i can't as this is timetable/schedule. So i can't use datatables
@Saleem then it depends what you want to sort by and where(client or server side). You can easily write your own sorting method.
Thank you Flame. I am new and trying to fine a way to handle this :) Client or server side any can i just want to be displayed as timetable
@Saleem updated answer with custom sorting by time in ascending order
0

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>

1 Comment

Thank you I need to add information to of 1st inner array to row 1 col 1. and 2 nd array to row 1 col 2. and onward :(
0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.