PHP ve MySQL takvim sorunu

2 Cevap php

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!

Teşekkürler.

2 Cevap

Yerine

  /* 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;

Daha ciddi, ilk durumda bir sorgu gibi olurdu

SELECT date,event from entries where date = $date

ve şimdi olurdu

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

Sonra gün endeksli bir ilişkisel dizi, sonuçları saklayın.

Evet, ben senin ... ah ... cesur öneri ile giderdim. Yukarıda yayınlanmıştır gibi, bir döngü içinde sorguyu çalıştırmak için hiçbir mantıklı. Eğer, yardıma tam olarak ne yapmam gerekiyor?