The company I work for decided to keep all the database queries in a RESTful API file. Was a big learning curve but finally I have the JSON object returning with the data. My problem now is how to populate the values into an HTML table.
Below I will show you what I have for now, what I already tried and the sample code, so you may suggest where I need to be corrected.
Thanks So much.
1. First, I request the data:
$schoolid = $_GET['schoolid'];
$data = file_get_contents("http://www.mydomainname.com/api/webapi.php?Oper=liststudents&schoolid=" . $schoolid);
echo $data;
2. And here's a sample of the results. Obviously A Json string:
{"data":[{
"ID":"0450",
"FirstName":"\u05d9\u05d5\u05e1\u05e3\u05de\u05e0\u05d7\u05dd",
"LastName":"\u05d1\u05d9\u05e0\u05e0\u05e2\u05e8",
"SchoolID":"17",
"UID":"0001","
TeacherID":"26"},{"ID":"0017",
"FirstName":"\u05d0\u05e8\u05e8\u05d9\u05d9\u05e0\u05d3",
"LastName":"\u05d6\u05d0\u05d1",
"SchoolID":"17","UID":"0017",
"TeacherID":"24"},{"ID":"0018",
"FirstName":"\u05d3\u05d5\u05d1 \u05d2\u05dc\u05d5\u05d9\u05d1,
"LastName":"\u05d9\u05e9\u05db\u05e8",
"SchoolID":"17",
"UID":"0018",
"TeacherID":"24"}
I guess it's still an object that has to be parsed... So now,
3. Following is an illustration of what I'm trying to achieve.
|ID| FirstNm | LastNm | School |UID| Teacher|
|---|------------|-----------|-----------|-----|------------|
|---|------------|-----------|-----------|-----|------------|
And so on, and so furth...
4. Here are some methods I tried so far but I either get "0" results, or it throws an error
A.
foreach($data->{'data'} as $obj){
echo '<tr>
<td>' . $obj->{'ID'} .'</td>
<td>' . $obj->{'FirstName'} .'</td>
<td>' . $obj->{'LastName'} .'</td>
<td>' . $obj->{'SchoolID'} .'</td>
<td>' . $obj->{'UID'} .'</td>
<td>' . $obj->{'TeacherID'} .'</td>
</tr>'
B.
foreach ($data as $key => $jsons) {
$table ='<table class="'.$jsons['class'].'" border="1">';
foreach ($jsons as $rkey => $rvalue) {
if($rkey=='head')
{
$table.='<tr>';
foreach($rvalue as $rvv)
{
$table.='<th>'.$rvv.'</th>';
}
$table.='</tr>';
}else
if($rkey=='rows')
{
foreach($rvalue as $rvv)
{
$table.='<tr>';
foreach($rvv as $rv)
{
$table.='<td>'.$rv.'</td>';
}
$table.='</tr>';
}
}
}
}
echo $table;
Tried few more things, but I only need ONE working model...