[thelist] php/mysql get next two records

Burhan Khalid thelist at meidomus.com
Mon Feb 7 00:26:27 CST 2005


Mark wrote :
> this is probably really simple but... could some enlighten me 
> to getting the next two records (next, after that) following 
> the first one (now playing) to be displayed?

Here is how I would do it. I would get all the results for a particular day
into an array, then step through the array by the hour, which would allow me
to print our results such as in 5 hours, in 3 hours, etc.  I would also
adjust my query so that all records that are pulled are for today (ie,
show_date = now()) -- this would avoid having to do an additional
comparison.

So, while($row = mysql_fetch_assoc($result)) { $schedule[] = $row }, and now
$schedule has all the records for a particular day. Then, $now =
strtotime("today"); $two_hours_from_now = strtotime("+2 Hours"), and so on.

Finally, foreach($schedule as $record) { if ($record['show_start'] == $now)
{ ..dosomething..} }

Hopefully this helps.




More information about the thelist mailing list