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 提前

4

2 回答 2

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

于 2013-02-02T15:30:33.383 回答
-1

jQuery .each 方法不是用来遍历数组而是 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]*/
               }
         }

如果数据数组包含像 data["school1"] 或类似的非*有序键,则必须使用for(var i in data)类似于 PHP foreach 的键。

另外,我不确定,但我认为你应该有dataType:'application/json',这是正确的json mime 类型

于 2013-02-02T12:55:42.190 回答