0

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.

3
  • 1
    I can't even believe that guy is posting code with a query in a loop ;) Commented Dec 9, 2009 at 11:41
  • He does say in the comments it's for the sake of keeping the article simple. Commented Dec 9, 2009 at 11:45
  • Oh, ok, thanks. Although I don't see how much harder it would be :) Commented Dec 9, 2009 at 11:46

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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....
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)
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!
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.
0

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?

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.