如何在沒有URL的情況下使用JSON編碼數組?

[英]How can I use a JSON encoded array without a URL?


Hi I'm using fullCalendar 1.5.1, and I've created a PHP query that returns almost everything I want to use in the calendar. This is an integrated system so I had to switch to _ASSOC in order to only return a MySQLi asssociative array:

嗨,我正在使用fullCalendar 1.5.1,我已經創建了一個PHP查詢,幾乎可以返回我想在日歷中使用的所有內容。這是一個集成系統,所以我不得不切換到_ASSOC才能返回一個MySQLi asssociative數組:

<?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;
?>

...except it won't accept the data because the key in the key/value pairs is quoted (as per JSON standards so I've read).

...除非它不接受數據,因為鍵/值對中的鍵被引用(根據JSON標准,所以我已閱讀)。

If I just enter the resultant MySQL array, the Javascript just shows the data as "Array". If I use json_encode() on the array, I get what looks to be a proper JSON object but it doesn't work with fullCalendar due to the keys being quoted.

如果我只輸入結果MySQL數組,Javascript只顯示數據為“數組”。如果我在數組上使用json_encode(),我會得到一個看似合適的JSON對象,但由於引用了鍵,它不能用於fullCalendar。

Here is my jQuery for fullcalendar:

這是我的fullcalendar jQuery:

$(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
    })
}); 

Here is the generated HTML source if I use the MySQL array for $json_result :

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

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

And here is the result if I use json_encode($thesamearray)

如果我使用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
        })
    });

How can I adjust this so it will work as an array within fullCalendar? It doesn't seem to want to take a JSON string or an array, at least in the format the array is in now from PHP.

我如何調整它以使其在fullCalendar中作為數組工作?它似乎不想采用JSON字符串或數組,至少采用PHP現在的數據格式。

Thanks, Chris

SOLUTION:

Alias the MySQL fields:

別名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;
?>

Also adjusted the jQuery to remove my start/endparams and changed allDaySlot to allDayDefault:

還調整了jQuery以刪除我的start / 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
    })
});

Now, if I could only find a way to make it show an 8-day horizontal agenda, my troubles could be over! Thanks all for the tips.

現在,如果我只能找到一種方法讓它顯示為期8天的橫向議程,那么我的麻煩可能就結束了!謝謝大家的提示。

2 个解决方案

#1


1  

According to the documentation for Event Object the start date is required and must be specified as the property named start - you're using the name eventstart

根據Event Object的文檔,開始日期是必需的,必須指定為名為start的屬性 - 您正在使用名稱eventstart

Note that according to the docs, startParam and endParam are parameters for the JSON feed option for events and don't seem to change the name of the property that is used for start and end times.

請注意,根據文檔,startParam和endParam是事件的JSON提要選項的參數,似乎不會更改用於開始和結束時間的屬性的名稱。


Update 1

I played around with this a bit and somewhat counter-intuitively, unless you specify allDay: false as a property for an event, even if has a specific time range, it shows up in the all day slot at the top of the calendar which you had disabled with allDaySlot: false. Here's a working example using almost all of your original setup.

我玩了一下這個並且有點反直覺,除非你指定allDay:false作為事件的屬性,即使有特定的時間范圍,它也會顯示在日歷頂部的全天候插槽中已使用allDaySlot禁用:false。這是一個使用幾乎所有原始設置的工作示例。


Update 2

From your comment to Matt's answer and from the documentation, I see the global default for the allDay property is inherited from the allDayDefault property which is by default true. Thus, by passing allDayDefult: false in your options to fullCalendar(), you avoid the need to specify allDay: false for each event individually. Here's is the updated fiddle.. Note that you don't have to get rid of allDaySlot, that simply dictates whether the slot showing all day events is displayed on top or not.

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

#2


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);

?>

Then pass this in events like this

然后在這樣的事件中傳遞它

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

and it will work.

它會起作用。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2011/06/05/72f5a728259d8ca601409ed23d2ad45d.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com