0

im trying to push new value in my array using 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);

then get the values using 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();

but unfortunately im getting undefined values though my console shows that i properly get the values from php to ajax. what wrong did i do?.. please help guys. tnx in advance

Felix Kling
  • 705,106
  • 160
  • 1,004
  • 1,072
Aoi
  • 885
  • 6
  • 12
  • 27
  • You mention "array push", but I don't actually see the item being pushed to an array at any point. Instead, you're just outputting the last item after uselessly looping over all rows. Is something missing from the above code? – Will Palmer Feb 02 '13 at 12:53
  • im trying to put $item['stat'] ='Incomplete'; inside the array $item am i doing it wrong? – Aoi Feb 02 '13 at 12:56
  • As much as I like cleaning up code, please format your code properly from the beginning next time. If the code is easy to read, it's easier for us to understand it and easier for us to help you. – Felix Kling Feb 02 '13 at 12:57
  • Given that there is no reference to `$items` anywhere in the above code: yes, you are doing it wrong. – Will Palmer Feb 02 '13 at 12:58
  • i edit my last comment im trying to push it to $item=array( 'school_name' => $row['school_name'], 'school_address' => $row['school_address'], 'school_id' => $row['school_id'], ); sorry for the typo – Aoi Feb 02 '13 at 13:02
  • Use your browser's developer tools and have a look at network tab (or equivalent) and check what response you get from the server. – Felix Kling Feb 02 '13 at 13:03
  • im getting this respones from my console {"school_name":"Dasmarinas High School","school_address":"dasmarinas cavite","school_id":"1","stat":"Incomplete"} – Aoi Feb 02 '13 at 13:07
  • Well, then it seems like that in your `success` callback, `data` is a single object with properties `school_name`, `school_address`, etc. `data` is **not** an array. If you access `data.school_name`, do you get the correct value? – Felix Kling Feb 02 '13 at 13:10
  • ow yes im getting the correct values.. but what if i have a multiple schools. – Aoi Feb 02 '13 at 13:14
  • Then you would create an array on the server side, encode it as JSON and the code your currently have in the client side would work find. – Felix Kling Feb 02 '13 at 13:23
  • is there a way to get the length value of data? so i could create a condition. – Aoi Feb 02 '13 at 13:52

2 Answers2

1

Js part of code is correct, but your php array formed wrong.
Try to replace code above. The mistakes are commented.

$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';
    }
}

The result of this: http://clip2net.com/s/2MJmt

Andrei Zhamoida
  • 1,392
  • 1
  • 18
  • 29
-1

jQuery .each method is not meant to be used to traverse arrays but HTML elements. Your array of schools should be traversed normally using FOR loop:

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]*/
               }
         }

In case data array contains non*ordered keys like data["school1"] or something like that, you must use for(var i in data) which is similar to PHP foreach.

Also, I'm not sure about that, but i think you should have dataType:'application/json', which is the correct json mime type.

Community
  • 1
  • 1
  • 1
    Are you sure the `success` callback is called when jQuery cannot parse the JSON? I think the `error` callback is called then. And `$.each` is exactly for traversing arrays and objects: http://api.jquery.com/jQuery.each/. The `.each` method you are linking to is not the one the OP uses. – Felix Kling Feb 02 '13 at 13:00
  • Well, as you can see, I'm not using jQuery very frequently, so I did not know about .each, but I see literally **NO** reason to use .each instead of FOR loop. – Tomáš Zato - Reinstate Monica Feb 02 '13 at 13:06
  • That's more a style preference though and has nothing to do with the OP's problem. – Felix Kling Feb 02 '13 at 13:08