2013-01-09 23 views
0

即時通訊使用JQuery完整日曆事件。我知道到目前爲止沒有一個選項可以通過限制來加載事件。JQuery完全日曆限制事件與PHP

在月視圖中,如果我一天有100個事件,它會使表單元格變得非常大。我想限制它每天10個事件。

我想我會做的是在服務器端使用PHP來做到這一點。由於在給定日期範圍的情況下使用PHP加載所有事件,我以爲我會一天一天地加載並將其限制爲每天10個事件。

這就是我對PHP的一面:

if ($currView == 'month') //curr view is the current view of the calendar 
    { 
     $start = $_GET['start']; //gets from the calendar usually start of month 
     $end = $_GET['end']; //gets from calendar usually end of month 
     //some array to store all events before using json_encode 
     $limitEvents = array(); 
     $limit = 10; 
     //loop through from start till end by 1 day incremental 
     for ($i = $start; $i < $end; $i+=strtotime($i, "+1 day")) 
     { 
      //make the end of day to be the current day but 23:59:59 
      $endOfDay = date('m/d/Y', $start); 
      $endOfDay = strtotime($endOfDay . ' 23:59:59'); 
      //load the events from DB using date range which is 1 day and limit of events 
      $loaded = $this->loadDataFromDB($start, $endofDay, $limit); 
      //merge the arrays 
      array_merge($limitTasks, $loaded); 
     } 
    } 

現在有了這個代碼的問題是,它不是最佳的,當我嘗試運行它,它遍歷每一天,每天的查詢和需要很長時間,它甚至超時。

我的問題是,我怎麼會在MySQL端做這個?我會給出從月初到月底的日期範圍。而不是每天循環PHP邊和每天加載數據,我會在MySQL方面做到這一點。例如查詢將看起來像什麼:

SELECT * 
    FROM Events_Table 
    WHERE Ev_Date BETWEEN :startMonth AND :endMonth 
    (have extra query to load by days within given start and end month 
    and have LIMIT 10 per day) 

不能完成上面的查詢中,括號中的文字需要有效的查詢被轉移,以便可以選擇每天10個事件,並有31天,以便它應該選擇大約310個事件

如果有人有更好的解決方案,請幫助。

+0

查詢的哪一部分給你帶來麻煩:每天限制爲10或者開始和結束邊界的where子句? – jimbojw

+0

它不給我麻煩,我無法完成查詢,以便它選擇每天10個事件,並有31天,所以它應該選擇約310個事件 – Giorgi

+0

你是否在意你按時間順序獲得前10名?或者只是你在給定的一天中得到的不超過10個? – jimbojw

回答

1

,如果你想擁有最多10元左右,改變這種像下面以減少限制,你遍歷天...

$currView == 'month') //curr view is the current view of the calendar 
    { 
     $start = $_GET['start']; //gets from the calendar usually start of month 
     $end = $_GET['end']; //gets from calendar usually end of month 
     //some array to store all events before using json_encode 
     $limitEvents = array(); 
     $limit = 10; 
     //loop through from start till end by 1 day incremental 
     for ($i = $start; $i < $end; $i+=strtotime($i, "+1 day")) 
     { 
      //make the end of day to be the current day but 23:59:59 
      $endOfDay = date('m/d/Y', $start); 
      $endOfDay = strtotime($endOfDay . ' 23:59:59'); 
      //load the events from DB using date range which is 
      //1 day and limit of events 
      if ($limit>0){ //if you want to have 10 in total add this 
       $loaded = $this->loadDataFromDB($start, $endofDay, $limit); 
       $limit -= count($loaded); //and this 
      } //and this 
      //merge the arrays 
      array_merge($limitTasks, $loaded); 
     } 

,併爲你的MySQL查詢到實際的極限量,你得到

SELECT * 
    FROM Events_Table 
    WHERE Ev_Date BETWEEN :startMonth AND :endMonth 
    LIMIT 10 

然後,如果你仍然有它慢,請檢查您的數據庫在您的領域的正確索引。當您在where子句中使用開始和結束月份時,當您向每個索引添加索引時,它們的查詢速度會快得多

另外,不確定您用於運行查詢的內容,但要確保您添加單引號你的佔位符,或者你仍然可以有mysql注入發生;只是逃避變量是不夠的

+0

這並不重要,但那不是我想要達到的目標,我需要在每個月的每一天每天10個事件。所以如果一個月有30天,我需要30 * 10個事件或300個事件。上面的代碼非常慢,即時通訊使用PDO – Giorgi

+0

是的,正如我在我的答案中提到的那樣,然後使用第二個;將LIMIT 10添加到您的查詢並將其添加到startMonth和endMonth – scott