2013-02-02 74 views
0

我嘗試使用PHP陣推未定義

$select_all_schools = "SELECT * FROM schools "; 
$query = mysql_query($select_all_schools) OR die(mysql_error()); 

while($row = mysql_fetch_array($query)){ 
    $item=array( 
     'school_name' => $row['school_name'], 
     'school_address' => $row['school_address'], 
     'school_id' => $row['school_id'], 
    ); 

    $select_sections = "SELECT * FROM section WHERE school_id = '".$row['school_id']."'"; 

    $query_section = mysql_query($select_sections) or die(mysql_error()); 
    $sections_counts = mysql_num_rows($query_section); 
    $select_sections_deped_archive = "SELECT * FROM deped_grade_archive 
             WHERE school_id = '".$row['school_id']."' 
             GROUP BY section_id "; 

    $query_section_deped_archive = mysql_query($select_sections_deped_archive) or die(mysql_error()); 
    $sections_counts_grade_archive = mysql_num_rows($query_section_deped_archive); 

    if($sections_counts_grade_archive == $sections_counts){ 
     $item['stat'] = 'Complete'; 
    } 
    else{ 
     $item['stat'] ='Incomplete'; 
    } 
}   

echo json_encode($item); 

然後在我的數組推新值用ajax

function get_all_school_status(){ 
    $.ajax({ 
     type:'POST', 
     url:'deped_functions.php', 
     dataType:'json', 
     data:{'func_num':'1'}, 
     success:function (data){ 
      $.each(data, function(i, item) { 
       html = "<tr>"; 
       html += "<td style='width:20%;'><input type='radio' name='school_id' value='"+data[i].school_id+"'></td>"; 
       html += "<td style='width:25%;'><label>"+data[i].stat+"</label></td>"; 
       html += "<td style='width:55%;'><label >"+data[i].school_name+"</label></td>"; 
       html += "</tr>"; 

       $('#table-schools-content').append(html); 
      }); 
     } 
    }); 
} 
get_all_school_status(); 

但不幸的是即時得到未定義的值,雖然得到的值我控制檯顯示我正確地從php到ajax的值。我做了什麼錯事?請幫助傢伙。 tnx

+0

你提到「陣推」,但我實際上沒有看到該項目被推到一個數組中的任何一點。相反,你只是在無用循環遍歷所有行後輸出最後一個項目。上面的代碼中缺少一些東西嗎? –

+0

即時嘗試把$ item ['stat'] ='不完整';數組裏面$ item我做錯了嗎? – Aoi

+0

就像我喜歡清理代碼一樣,請從下一次開始正確地格式化您的代碼。如果代碼易於閱讀,我們可以更容易理解它,並且更容易幫助您。 –

回答

1

Js部分代碼是正確的,但是您的php數組形成了錯誤
嘗試替換上面的代碼。錯誤評論。

$count = 0; // keys of array 
while($row = mysql_fetch_array($query)){ 
    $count++; // in prevoius version you get only one row from table 
    // correct version of school array 
    $item[$count] = array( 
     'school_name' => $row['school_name'], 
     'school_address' => $row['school_address'], 
     'school_id' => $row['school_id'], 
    ); 

    $select_sections = "SELECT * FROM section WHERE school_id = '".$row['school_id']."'"; 

    $query_section = mysql_query($select_sections) or die(mysql_error()); 
    $sections_counts = mysql_num_rows($query_section); 
    $select_sections_deped_archive = "SELECT * FROM deped_grade_archive 
             WHERE school_id = '".$row['school_id']."' 
             GROUP BY section_id "; 

    $query_section_deped_archive = mysql_query($select_sections_deped_archive) or die(mysql_error()); 
    $sections_counts_grade_archive = mysql_num_rows($query_section_deped_archive); 

    if($sections_counts_grade_archive == $sections_counts){ 
     // success or error message with key 'status' 
     $item[$count]['status'] = 'Complete'; 
    } 
    else{ 
     $item[$count]['status'] = 'Incomplete'; 
    } 
} 

這樣做的結果是: http://clip2net.com/s/2MJmt

-1

jQuery .each method不是用來遍歷數組,而是用HTML元素。你的學校數組應該進行遍歷正常使用FOR循環:

success: function(data) { 
       if(typeof data == "string") //handle PHP errors - this happens if non-json string is returned by a server 
        alert("JQuery failed to convert data to JS object!"); 
       for(var i=0; i<data.length; i++) { 
        /*Do whatever with data[i]*/ 
       } 
     } 

如果數據數組包含類似數據的非*命令鍵[「school1」]或類似的東西,你必須使用for(var i in data)它類似於PHP的foreach 。

此外,我不確定,但我認爲你應該有dataType:'application/json',這是正確的json mime type

+1

當jQuery無法解析JSON時,你確定調用'success'回調嗎?我認爲'error'回調被調用。 '$ .each'恰恰用於遍歷數組和對象:http://api.jquery.com/jQuery.each/。您鏈接到的'.each'方法不是OP使用的方法。 –

+0

好吧,正如你所看到的,我並不經常使用jQuery,所以我不知道。每個,但我看到字面上**沒有**理由使用.each而不是FOR循環。 –

+0

雖然這更多是一種風格偏好,與OP的問題無關。 –