views:

111

answers:

2

Hi folks! I'm writing trying to build a calendar right from scratch. I'm using the function written by David Walsh (see link) and it's great. He does a query for each day's cell. But, I'm afraid that when the script's gonna have to run 30 queries in each render, it's gonna be sloooow.

So, I was trying to think in another logic, for example, make a big query from X date to Y date at the begining of the script and then, at each day, check if that particular day has an event in the previous query. But I have no idea of how to do this... So, if anyone can help, please shout!

Thanks.

A: 

Yes, I would go with your...uh...bold suggestion. As posted above, it makes no sense to run the query in a loop. What exactly do you need help with, then?

Franz
+2  A: 

Instead of

  /* keep going with days.... */ 
  for($list_day = 1; $list_day <= $days_in_month; $list_day++):
      $calendar.= '<td class="calendar-day">';
      /* add in the day number */ 
      $calendar.= '<div class="day-number">'.$list_day.'</div>';

      /** QUERY THE DATABASE FOR AN ENTRY FOR THIS DAY !!  
          IF MATCHES FOUND, PRINT THEM !! **/ 

      $calendar.= str_repeat('<p>&nbsp;</p>',2);
      $calendar.= '</td>';
      if($running_day == 6):
          $calendar.= '</tr>';
          if(($day_counter+1) != $days_in_month):
              $calendar.= '<tr class="calendar-row">';
          endif;
          $running_day = -1;
          $days_in_this_week = 0;
      endif;
      $days_in_this_week++; $running_day++; $day_counter++;
  endfor;

do

   /** QUERY THE DATABASE FOR AN ENTRY FOR THE PERIOD !!  
          IF MATCHES FOUND, STORE THEM !! **/ 
  $entries = getEntriesFromDB($month,$year);

  /* keep going with days.... */ 
  for($list_day = 1; $list_day <= $days_in_month; $list_day++):
      $calendar.= '<td class="calendar-day">';
      /* add in the day number */ 
      $calendar.= '<div class="day-number">'.$list_day.'</div>';
      if ($entries[$list_day]):
          /** ADD THE ENTRIES FOR THE DAY!! **/
      endif;
      $calendar.= str_repeat('<p>&nbsp;</p>',2);
      $calendar.= '</td>';
      if($running_day == 6):
          $calendar.= '</tr>';
          if(($day_counter+1) != $days_in_month):
              $calendar.= '<tr class="calendar-row">';
          endif;
          $running_day = -1;
          $days_in_this_week = 0;
      endif;
      $days_in_this_week++; $running_day++; $day_counter++;
  endfor;

More seriously, in the first case you'd have a query like

SELECT date,event from entries where date = $date

and now you'd have

SELECT date,event from entries where date between $date_minus_one_month and $date

Then store the results in an associative array, indexed by days.

Vinko Vrsalovic
Yes! that's what I'm saying. But my question is: do I need to use a multidimensional array? How do I find in a query the events of this particular day, and extract the id, time, title, description, etc....
fedeisas
Just like the last query shows. You need an associative array, where you point a date to an array with all the data. For example if 2009-10-01 had two events the equivalent to $events['2009-10-01'] = array(array($id,$time,$title,$description),array($id2,$time2,$title2,$description2)) would be written (of course the nested array declaration would be done via a loop)
Vinko Vrsalovic
Yes! That's right what I meant. But, how do I put a query into an associative array indexed by days and then check for events on every day? Could you give me a hint on that..? Thank @Vinko!
fedeisas
Ask another question with the code you have, with the database tables you are using. That way you'll get better help. I've already given you all the hints I could without knowing more details.
Vinko Vrsalovic