JSON output of a table in PHP -
this question not programming question exactly. have requirement of making json output of picture shown. know usage of json_encode()
problem not getting how logically show these details.
the output got is:
{"name":"zxy","success":1,"subjects":["digital communication technique(2012-13)","digital signal processing(2012-13)","antennas , wave propagation(2012-13)","digital switching theory , networks(2012-13)","accounting managers(2012-13)","digital communication technique lab(2012-13)","digital signal processing lab(2012-13)"]}
.
please tell me can valid json output of details have,
edit: have json output mysql data. sample data of student have provided in image. json have shown result of php page, not satisfied json output getting. asking better way represent data in effective manner in json format.
this php code have written getting json output.
<?php $regno = $_get['regno']; $host = 'localhost'; $user = 'root'; $password = ''; $database = 'android_app_details'; $dbc = mysqli_connect($host,$user,$password,$database) or die('error in connecting database'); $query = "select sd.regno, std.fname, std.mname, std.lname, sd.subject_name, ad.present_count, ad.total_count ". "from attendance_details ad, subject_details sd, student_details std ". "where ad.enrollment_id = sd.enrollment_id ". "and sd.regno = '".$regno."' ". "and ad.batch_subject_id = sd.batch_subject_id ". "and sd.student_id = std.student_id "; $result = mysqli_query($dbc, $query) or die('error in query'); $subject = array(); $att = array(); $total = array(); $i = 0; $success = 0; while($row = mysqli_fetch_array($result)){ if($row['mname']) $name = $row['fname'].' '.$row['mname'].' '.$row['lname']; else $name = $row['fname'].' '.$row['lname']; $subject[$i] = $row['subject_name']; $i++; $success = 1; } if($success) $response = array( "name" => $name, "success" => $success, "subjects" => $subject ); else $response = array( "success" => $success ); echo json_encode($response); ?>
you output pefectly formed json (as expected json_encode
function).
from comments see upsests subjects:[...]
array second not valid, is.
what json, xml , other structured data formats structured data holds in definition both structure , data if have:
{object: {subobject:{subobjectname:"penny"} }
you'll have structure object->subobject->subobjectname
3 , data "penny"
.
now can see json array collection of entities same tagname, example this:
{object:{ subobjects:[ {subobjectname:"penny",subobjectlasname:"rose"}, {subobjectname:"lewis",subobjectlasname:"armstrong"}, ... ] }}
which have {}s
looking for, if @ in means of structure/data last array array of structures(objects), elements json objects.
what have in posted json output array of data(strings) are not structural elements , not have specific tagname other subjecs
.
if familiar xml should easier understand:
<student> <name>penny</name> <last_name>rose</last_name> <subjects>subject1</subjects> <subjects>subject2</subjects> <subjects>subject3</subjects> </student>
this above valid xml , translates in json of kind have in output, because subject1, subject2, subject3
text entities not have parent tagname subjects
array of tags same name containing plain text.
i know doesn't answer question, hope clears dubts output got.
Comments
Post a Comment