views:

19

answers:

1

the subject basically sums it up, but i am working on a calendar in php for a client that should pull events from a table and display them in the appropriate cells.

the calendar table is working no prob, but what i am trying to avoid is making a separate DB call for each day of the month.

i am trying to figure out a way to store the results in an array of arrays first, then as the calendar is created, each day that has an event(s) would display some portion of the data for that even there.

it's a conceptual problem i am having and i can't seem to solve it.

also, not sure if it's possible.

any help is appreciated.

thanks.

for example:

select day, title, desc where month = $month and year = $year order by day asc

and ultimately output something like this without hitting the DB 31 times:

day1.
title / desc


day2.
title / desc <br>
title / desc


day3. 
title / desc
A: 

you can do one query to get all the events in this mounth,

like you write

 select day, title, desc from mytable 
where month = $month and year = $year order by day asc

and in the foreach loop on the results, store it in array like

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $events[$row['day']][] = $row;
}

so in the end of the loop you have array $events, where every entry contain an array of the events in this day.

like $events[1], $events[2] ....

Haim Evgi
Thanks, you definitely got me on the right track.
Jason
i think when i was working on this last night i was too tired. your solution was close enough to get it finished. the code below allows for multiple events on the same day by adding another array based on the unique id. thanks again!while ($row = mysql_fetch_array($getEvent)){ $events[ltrim($row['day'], 0)][$row['id']]['title'] = $row['title'];}foreach($events[$day] as $event){ $calendar_body .= $event['title'].'<br><br>';}
Jason