2013-03-24 62 views
0

我現在有一個功能,看起來像這樣:返回通過AJAX一系列複雜的命令(或者重組一次通過Ajax返回)

function getEvents($weekNumStart, $weekNumEnd){ 
    $mysqli = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME); 
    if (!$mysqli) { 
     die('There was a problem connecting to the database.'); 
    } 
    else { 
     if ($weekNumEnd == '') { 
      $weekNumEnd = $weekNumStart; 
     } 
     $Group = $_SESSION['Group']; 
     $query = $mysqli->prepare("SELECT EventID, DAYOFWEEK(Start) AS wday, Events.Start, HOUR(Start) AS sHour, HOUR(End) AS eHour, Events.End, Events.Group, Events.Unit, Type, Room, Lecturer, Cancelled, StartName FROM Events, Week WHERE StartName >= '$weekNumStart' AND StartName <= '$weekNumEnd' AND Events.Start >= StartWeek AND Events.Start <=EndWeek AND (Events.Group = '$Group' OR Events.Group = '');"); 
     $query->execute(); 
     $query->bind_result($eventID, $dayofweek, $startDateTime, $startHour, $endHour, $endDateTime, $group, $unit, $type, $room, $lecturer, $cancelled, $weekName); 
     $data_arr = array(); 
     while ($query->fetch()){ 
      $data_arr[] = array(
       $eventID, $dayofweek, $startDateTime, $startHour, $endHour, $endDateTime, $group, $unit, $type, $room, $lecturer, $cancelled, $weekName 
      ); 
     } 
     return $data_arr; // 
    } 
    $mysqli->close(); 
} 

此功能如何我想它。我目前操作它是這樣的:

if (!isset($weekNumEnd)){ 
     $weekNumEnd = ''; 
    } 
    $data = getEvents($weekNumStart, $weekNumEnd); 
    foreach($data as $day) { 
     for($i=0;$i<7;$i++){ 
      for($j=9;$j<=18;$j++){ 
       if ($day[1] == $i && $day[3] == $j){ 
        $unit = $day[7]; 
        $type = $day[8]; 
        $room = $day[9]; 
        if ($i == 2){ 
        ?> 
        <script> 
        var mon<?=$j?>unit = '<?=$unit?>'; 
        var mon<?=$j?>type = '<?=$type?>'; 
        var mon<?=$j?>room = '<?=$room?>'; 
        $('#mon-<?=$j?>').append(
         "<p>"+mon<?=$j?>unit+"<br>"+mon<?=$j?>type+"<br>"+mon<?=$j?>room+"</p>" 
        ); 
        </script> 
        <?php 
        if (($day[4] - $day[3]) > 1) { 
         for($q=($day[3]+1);$q<$day[4];$q++){ 
         ?> 
         <script> 
          var mon<?=$q?>unit = '<?=$unit?>'; 
          var mon<?=$q?>type = '<?=$type?>'; 
          var mon<?=$q?>room = '<?=$room?>'; 
          $('#mon-<?=$q?>').append(
           "<p>"+mon<?=$q?>unit+"<br>"+mon<?=$q?>type+"<br>"+mon<?=$q?>room+"</p>" 
          ); 
         </script> 
         <?php 
         } 
        } 
       } 

的問題。我顯然無法實時運行它,以便每週或任何更改時不斷更新事件列表。

所以要解決它,我決定嘗試使用Ajax。到目前爲止,我有這樣的:

function getEvents() { 
     var data = 'Events=Yes'; 
     $.ajax({ 
      type: "POST", 
      url: "functions/updateWeek.php", 
      data: data, 
      cache: false, 
      dataType: 'json', 
      success: function(html) { 
        alert(html.returned_val); 
      } 
    }); 
} 

而且我有這樣的返回:

if($_POST['Events'] == "Yes"){ 
    $weekNumStart = $_SESSION['startWeek']; 
    $weekNumEnd = $_SESSION['endWeek']; 
    $data = getEvents($weekNumStart, $weekNumEnd); 
    echo json_encode(array('returned_val' => $data)); 
} 

這......這類作品。至少也就是說它將數據返回到一個醜陋的大塊中。

的事情是我不能再操縱它,因爲我做了如上前:

data = getEvents($weekNumStart, $weekNumEnd); 
    foreach($data as $day) { 
     for($i=0;$i<7;$i++){ 
      for($j=9;$j<=18;$j++){ 
       if ($day[1] == $i && $day[3] == $j){ 
        $unit = $day[7]; 
        $type = $day[8]; 
        $room = $day[9]; 
        if ($i == 2){ 
        ?> 
        <script> 
        var mon<?=$j?>unit = '<?=$unit?>'; 
        var mon<?=$j?>type = '<?=$type?>'; 
        var mon<?=$j?>room = '<?=$room?>'; 
        $('#mon-<?=$j?>').append(
         "<p>"+mon<?=$j?>unit+"<br>"+mon<?=$j?>type+"<br>"+mon<?=$j?>room+"</p>" 
        ); 
        </script> 

也就是說 - 我不知道如何重新處理它的時候,我叫信息通過ajax回來。我會以完全javascript的方式重寫上述所有內容嗎?有沒有更合乎邏輯的做法,我不知道/明白?或者有什麼辦法可以讓代碼在不同的窗口中工作(使用php和javascript完成),並通過ajax將其全部發回,通過ajax進行更正,以便在頁面上自動工作而無需重寫信息是通過ajax拉回來的?

回答

1

首先,使用json_encode($ data)就足夠了,您將從AJAX獲取數組。

其次,檢查它與螢火蟲,因爲你應該得到結構化的數據,而不是大塊。

警報()只是試圖打印一切爲單個字符串,這是所有

for(var i = 0; i < html.length; i++) 
    // do something with html[i] 
+0

哦~~! 當然。謝謝。這一手固定了我所有的問題。 – 2013-03-25 00:05:31