2011-06-05 22 views
0

嗨我正在使用fullCalendar 1.5.1,並且我創建了一個PHP查詢,它返回幾乎所有我想在日曆中使用的東西。這是一個集成的系統,所以我不得不爲了切換到_ASSOC只返回庫MySQLi asssociative陣列:如何使用不帶URL的JSON編碼數組?

<?php 
global $json_result; 
global $myqueryresult; 
$GLOBALS["ADODB_FETCH_MODE"] = ADODB_FETCH_ASSOC; 
$myquery = "SELECT id, title, eventstart, eventend FROM View1"; 
$myqueryresult = $GLOBALS["conn"]->GetAll($myquery); 
$json_result = json_encode($myqueryresult); 
$GLOBALS["ADODB_FETCH_MODE"] = ADODB_FETCH_BOTH; 
?> 

...但它不會接受的數據,因爲在鍵/值對的關鍵是引用(按照JSON標準,所以我讀過)。

如果我只輸入生成的MySQL數組,那麼Javascript只會將數據顯示爲「Array」。如果我在數組上使用json_encode(),我會得到看起來是正確的JSON對象的東西,但由於引用了鍵,它不適用於fullCalendar。

這裏是我的jQuery的fullcalendar:

$(document).ready(function() {        
    $('#calendar').fullCalendar({   
    height: 600, 
    firstDay: 6, 
    theme: true, 
    year: 2012, 
    month: 0,      
    date: 7,      
    minTime: 6, 
    maxTime: 23, 
    startParam: 'eventstart', 
    endParam: 'eventend', 
    defaultView: 'agendaWeek',   
    allDaySlot: false, 
    slotMinutes: 15,      
    events: <?php echo $json_result; ?> 

     // put your options and callbacks here 
    }) 
}); 

這裏是生成的HTML源代碼,如果我使用$ json_result MySQL的數組:

$(document).ready(function() {        
     $('#calendar').fullCalendar({   
<...snip...> 
     slotMinutes: 15,      
     events: Array 
      // put your options and callbacks here 
     }) 
    }); 

這裏是結果,如果我使用json_encode ($ thesamearray)

$(document).ready(function() {        
<...snip...>      
     events: [{"id":"51","title":"Ship in Miami, Florida","eventstart":"2012-01-07 07:00:00","eventend":"2012-01-07 15:00:00"},{"id":"547","title":"Miami, Florida","eventstart":"2012-01-14 07:00:00","eventend":null}] 
      // put your options and callbacks here 
     }) 
    }); 

我該如何調整它,以便它可以作爲一個數組fullCalendar?它似乎不想採用JSON字符串或數組,至少現在使用PHP中的數組格式。

謝謝, 克里斯

SOLUTION:

別名MySQL的領域:

<?php 
global $json_result; 
$GLOBALS["ADODB_FETCH_MODE"] = ADODB_FETCH_ASSOC; 
$myquery = "SELECT id, title, eventstart AS start, eventend AS end FROM View1"; 
$myqueryresult = $GLOBALS["conn"]->GetAll($myquery); 
$json_result = json_encode($myqueryresult); 
$GLOBALS["ADODB_FETCH_MODE"] = ADODB_FETCH_BOTH; 
?> 

還調整了jQuery來刪除我的啓動/ endparams,改變allDaySlot到allDayDefault:

$(document).ready(function() {        
    $('#calendar').fullCalendar({   
    height: 600, 
    firstDay: 6, 
    theme: true, 
    year: 2012, 
    month: 0,      
    date: 7,      
    minTime: 6, 
    maxTime: 23, 
    defaultView: 'agendaWeek',   
    allDayDefault: false, 
    slotMinutes: 15,      
    events: <?php echo $json_result; ?> 

     // put your options and callbacks here 
    }) 
}); 

現在,如果我只能找到一種方法讓它顯示8天的橫向議程,我的麻煩就可以結束了!感謝所有提示。

+0

'json_encode'd數組看起來對我來說很好。對於FullCalendar而言,引用對象鍵不會有任何影響。 – 2011-06-05 01:06:36

+0

你是對的,它確實需要引用鍵很好,謝謝你。 – Chris 2011-06-05 01:53:08

回答

1

據爲Event Object開始日期是必需的,必須被指定爲命名start屬性的文件 - 您使用的名稱eventstart

注意,根據文檔,startParamendParam是參數針對事件的JSON供稿選項並且似乎不會更改用於開始和結束時間的屬性的名稱。


更新1

我這個有點,有點反直覺發揮各地,除非你指定allDay: false作爲事件的性質,即使有一個特定的時間範圍內,它顯示了在您使用allDaySlot: false禁用的日曆頂部的全天插槽中。這裏的a working example使用幾乎所有的原始設置。


更新2

從馬特的答案,並從文檔的評論,我看到了allDay屬性的全局默認從allDayDefault屬性默認情況下爲true繼承。因此,通過將您的選項中的allDayDefult: false傳遞給fullCalendar(),可以避免單獨爲每個事件指定allDay: false。這是updated fiddle.。請注意,您不必擺脫allDaySlot,即只顯示顯示全天事件的插槽是否顯示在頂部。

+0

非常感謝大家,稍微調整一下,讓他們都能正常出現。我將用解決方案更新問題。 – Chris 2011-06-05 01:47:08

+0

另外一個好點,我添加了allDaySlot:false來擺脫它,仍然保持所有功能。我仍然可以看到很多選項,因爲文本在事件中重疊,但現在確實可行,我相信它不會很難修復。再次感謝! – Chris 2011-06-05 02:10:28

0
<? 
$username = "root"; 
$password = ""; 
$hostname = "localhost"; 
$dbhandle = mysql_connect($hostname, $username, $password) 
    or die("Unable to connect to MySQL"); 
    $selected = mysql_select_db("db_name",$dbhandle) 
    or die("Could not select examples"); 
    $sql = 'SELECT * from schedule'; 
    $result = mysql_query($sql); 
    $events = array(); 
while ($row = mysql_fetch_assoc($result)) { 
    $eventsArray = array(); 
    $eventsArray['id'] = $row['idschedule']; 
    $eventsArray['title'] = $row['schedule_name']; 
    $eventsArray['start'] = $row['date_start']. " " . $row['time_start']; 
    $eventsArray['end'] = $row['date_end']. " ".$row['time_end']; 

    if($all==true) 
    { 
     $vall = str_replace('}',',"allDay":true}',json_encode($eventsArray)); 
    } 
    else 
    { 
     $vall = str_replace('}',',"allDay":false}',json_encode($eventsArray)); 
    } 
    $events[] = trim($vall); 
} 
$val = implode($events); 
$val2 = str_replace('}','},',$val); 

?> 

然後在事件通過這個類似這樣的

events: [<?=$val2?>], 

,它會工作。

+0

你看到其他答案的權利?你的答案太過於複雜,我只需要重命名SQL查詢中使用的別名即可。儘管感謝您的回覆。 – Chris 2011-06-10 16:31:06