0

guys Im still having a problem about appending into an array then put it in json for me to use it in ajax here is my code.

global $school_id;

        $get_section = "SELECT *
                        FROM section a
                        LEFT JOIN advisory_sections b ON(a.section_id = b.section_id)
                        LEFT JOIN school_faculty c ON (b.faculty_id = c.faculty_id)

                        WHERE a.school_id = '$school_id'  ORDER BY section_level ASC";





                        $result=mysql_query($get_section)or die(mysql_error());

                            $i=0;

                                $data=array();
                    while ($row = mysql_fetch_array($result))
                                {
                                    $data[] = array(
                                       'sec_id'=>$row['section_id'],
                                       'sec_name'=>$row['section_name'],
                                       'sec_dept'=>$row['section_department'],
                                       'sec_lvl'=>$row['section_level'],
                                      'advisory_id'=>$row['advisory_id'],
                                      'first_name'=>$row['f_firstname'],
                                      'last_name'=>$row['f_lastname'],
                                      'middle_name'=>$row['f_middlename'],
                                      'advisor_id'=>$row['faculty_id'],
                                    );


                    $get_subjects = "SELECT subject_name
                                        FROM subjects  
                                        WHERE level = '".$row['section_level']."' ";


                        $result_get_subjects =mysql_query($get_subjects)or die(mysql_error());

                        $subjects_count = mysql_num_rows($result_get_subjects);


                    $check_archive_subjects = " SELECT b.subject_name 
                                                FROM registrar_grade_archive a
                                                LEFT JOIN subjects b ON(a.subject_id=b.subject_id)

                                                WHERE a.advisor_faculty_id = '".$row['faculty_id']."'
                                                GROUP BY b.subject_name ASC
                                                 " ;

                     $query_checking =mysql_query($check_archive_subjects)or die(mysql_error());

                    $subjects_count_sent = mysql_num_rows($query_checking);



                                if($subjects_count_sent == $subjects_count){

                                        array_push($data, 'status:complete');
                                    }else{

                                        array_push($data, 'status:Incomplete');
                                    }


                                $i++;
                                }



        echo json_encode($data);

AJAX:

 function get_sections_status(){
     $.ajax({                
    url: 'teacher_class_get.php',
    dataType: 'json',
    type: 'POST', //u missed this line.
    data:{'func_num':'6'},
    success: function (data){
      $.each(data, function(i, item) {



        html = "<tr>";

                              html += "<td style='width:10%;'><input type='radio' name='section_id' rel='"+data[i].advisory_id+"' value='"+data[i].sec_id+"'></td>";
                              html += "<td style='width:25%;'><label>"+data[i].status+"</label></td>";
                              html += "<td style='width:15%;'><label id='year_level' rel='"+data[i].sec_lvl+"''>"+data[i].sec_lvl+"</label></td>";
                              html += "<td style='width:20%;'><label>"+data[i].sec_name+"</label></td>";
                              html += "<td style='width:30%;'><label id='faculty_id' rel='"+data[i].advisor_id+"'>"+data[i].last_name+", "+data[i].first_name+" "+data[i].middle_name+"</label></td>";                              
                              html += "</tr>";




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

           }
      });

   }
   get_sections_status();

and im getting this kind of response:

[{"sec_id":"36","sec_name":"black","sec_dept":"highschool","sec_lvl":"firstyear","advisory_id":"60","first_name":"asdf","last_name":"asdf","middle_name":"asdf","advisor_id":"1"},"status:Incomplete",{"sec_id":"32","sec_name":"level-up","sec_dept":"highschool","sec_lvl":"firstyear","advisory_id":"53","first_name":"asdf","last_name":"asdf","middle_name":"asdf","advisor_id":"1"},"status:Incomplete"

as you can see the status is not inside the array. thats why im getting this kind of out put.

This is my out put

in my out put im having a new row with a status but with other undefined values and the others has values but with an undefined status.. please help guys.. thanks in advance

4
  • And how is the final JSON supposed to look like? Is there always only one entry? The status otherwise always the same for all result rows? Commented Jan 26, 2013 at 3:28
  • Not enough code, we need more to pinpoint the problem. Try posting here your entire project and maybe some screen shots of all the pages in the website. Thanks! Commented Jan 26, 2013 at 4:01
  • Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. Commented Jan 26, 2013 at 4:39
  • ill be switching with mysqli after this project. still currently studying it. Commented Jan 26, 2013 at 16:22

1 Answer 1

1

You need to add status at a different level of your data structure.

Try this (see comments in the code):

....
while ($row = mysql_fetch_array($result))
{
//Create a new item to add to $data later
    $item = array(
        'sec_id'=>$row['section_id'],
        'sec_name'=>$row['section_name'],
        'sec_dept'=>$row['section_department']
....

    //Add status to $item, rather than $data
    //I don't think array_push will give you what you want here
    if($subjects_count_sent == $subjects_count){
        $item['status']='complete';
    }else{
        $item['status']='incomplete';
    }
    //Add the new item after status has been set
    $data[]=$item;
    $i++;
}
....

And yes, you should look at switching to mysqli or PDO.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks a lot for the code. but can you tell me why $data[] dont append the status?..
putting it in $data[] gives something like this: $data=array(array(<item properties>),'status'=><status>,array(<item properties>),'status'=><status>) so status ends up being treated as an item by itself. Status needs to be inside the item, like this: $data=array(array(<item properties>,'status'=><status>),array(<item properties>,'status'=><status>)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.