General developer forum

while and foreach loops to build timetable

 
Andrew
while and foreach loops to build timetable
 

Hi Everyone,

I'd like to build a timetable but not sure how to complete the while portion of a script I found from stackoverflow (link). I can build this directly via PHP without a problem but I'm finding translating this to Moodle to be a tricky process.

In a report plugin, I've loaded the timetable data via the $DB->get_records_sql($sqlclass,$paramsclassname); with $paramsclassname attached to a validated user input form. The SQL returns the correct data in an associative array based on a WHERE = match from selector.


So far, I've built a header using:


    print "<table border='5' style= 'background-color: #fff; color: #000; margin: 0 auto;' >";
    print " <tr>
                <th><b>Time</b></th>";
               
    //days heading
    $daysqry = $DB->get_records_sql($sqldays,$paramdays);
   
    foreach($daysqry as $day=>$records){
            echo '<th > '.$day.' </th>';
            }



but I am not sure how to use a while loop to populate and display the data.

Can anyone recommend the best approach? (or point me in the right direction)

Thanks,

Andrew


 
Average of ratings: -
Picture of Michael Milette
Re: while and foreach loops to build timetable
Core developersParticularly helpful MoodlersPlugin developers

Hi Andrew,

TIP: The var_dump() function is your friend here. Add something like the following in your source code to see how the data is structured. 

echo "<pre>\n";
var_dump($records);
echo "\n</pre>"; die;

This also show you whether you are actually doing your SQL query correctly. It has happened to me more than once where I am trying to format the output of an SQL query just to discover that it didn't contain the information I expected.

Once you understand what is being returned, it will become a lot easier for you to write a loop to extract and format the information you need.

You can actually do this with just about any variable containing almost any simple or complex structured information in PHP.

Hope this useful.

Best regards,

Michael


 
Average of ratings: -