Brownman Revival
Brownman Revival

Reputation: 3850

Ajax success jquery php

[{ 
    "SchoolId": "015-08-0034-009-37",
    "SubjectId": "08-0034-00613",
    "StudentId": "T-15981",
    "StudentName": "John"
},{
    "SchoolId": "015-08-0034-009-37",
    "SubjectId": "08-0034-00613",
    "StudentId": "T-15982",
    "StudentName": "Paul"
}]

This is the json_encode response from my php page when i do echo if would look like this

Schoolid=015-08-0034-009-37
Subjectid=08-0034-00613
Studentid=T-15981
Studentname=John
Schoolid=015-08-0034-009-37
Subjectid=08-0034-00613
Studentid=T-15982
Studentname=Paul

I can get this result by doing like this

for (var i = 0; i < data.length; i++) {
    console.log(data[i].schoolid);
    console.log(data[i].subjectid);
    console.log(data[i].studentid);
    console.log(data[i].studentname);
}

How can i make it in a way that the value of same entry will be generated only once like the value of school id and subject id will be shown only once the unique value like student id and student name will be shown twice since there are two different values for it.

I am hoping to get the result as

Schoolid=015-08-0034-009-37
Subjectid=08-0034-00613

Studentid=T-15981
Studentname=John
Studentid=T-15982
Studentname=Paul

Where should i do the fixing for this one in php before passing the json_encode or in ajax after receiving the json_encode from php?

if ($stmt - > rowCount() > 0) {
    while ($selected_row = $stmt - > fetch(PDO::FETCH_ASSOC)) {
        $basicinfo[] = array('schoolid' => $selected_row['schoolid'], 'subjectid' => $selected_row['subjectid'], 'studentid' => $selected_row['studentid'], 'studentname' => $selected_row['studentname']);
        //$basicinfo1[] = array('schoolid' => $selected_row['schoolid'], 'subjectid' => $selected_row['subjectid']);
        //$basicinfo2[] = array('studentid' => $selected_row['studentid'], 'studentname' => $selected_row['studentname']);
    }
    //$merge = array_merge($basicinfo1 , $basicinfo2);
    //$add = $basicinfo1 + $basicinfo2)
    //$input = array_map("unserialize", array_unique(array_map("serialize", $merge)));
    $input = array_map("unserialize", array_unique(array_map("serialize", $basicinfo)));
    echo json_encode($input, JSON_UNESCAPED_UNICODE);
}

Upvotes: 2

Views: 104

Answers (1)

Lepanto
Lepanto

Reputation: 1413

Following code will help you fix the array in PHP

if ($stmt - > rowCount() > 0) {
    //$new_data = array();
    while ($v = $stmt - > fetch(PDO::FETCH_ASSOC)) {
        $tmp_key = $v['SchoolId'].'-'.$v['SubjectId']; //adding a temp key

        $new_data[$tmp_key]['SchoolId'] = $v['SchoolId'];
        $new_data[$tmp_key]['SubjectId'] = $v['SubjectId'];

        $sudky = (isset($new_data[$tmp_key]['Student'])) ? count($new_data[$tmp_key]['Student']) : 0; //getting the key for student child array
        $new_data[$tmp_key]['Student'][$sudky]['StudentId'] = $v['StudentId'];
        $new_data[$tmp_key]['Student'][$sudky]['StudentName'] = $v['StudentName'];
    }
    $new_data = array_values($new_data);
    echo json_encode($new_data, JSON_UNESCAPED_UNICODE);
}

The final JSON data will be like following

[{
    "SchoolId":"015-08-0034-009-37",
    "SubjectId":"08-0034-00613",
    "Student":[
        {"StudentId":"T-15981","StudentName":"John"},
        {"StudentId":"T-15982","StudentName":"Paul"}
    ]
}]

In Javascript you will have to add another loop inside the for loop you alreardy have that goes through Student data

Following is the Javascript section

<script>
$(document).ready(function($){
    $.ajax({
        type: "post",
        url: "YOURSCRIPT",
        dataType: "json",
        success: function(data){
            if(data!=''){
                for (var i = 0; i < data.length; i++) {
                    alert(data[i].SchoolId);
                    alert(data[i].SubjectId);
                    var student = data[i].Student;
                    for (var j = 0; j < student.length; j++) {
                        alert(student[j].StudentId);
                        alert(student[j].StudentName);
                    }
                }
            }
        }
    });
});
</script>

Upvotes: 1

Related Questions