开发者

PHP and MySQL calendar issue

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 fro开发者_如何学Pythonm 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.


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.


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜