Display json content in php table - php

I am trying to display json content in php table but I'm getting error every time. I have some syntax error and cant figure out what should i change?
PS. Trying built it with Slim framework
Here is my code:
<div class="data-table-wrapper">
<?php
$myData = file_get_contents("http://ergast.com/api/f1/current.json");
$myObject = json_decode($myData);
?>
<table class="data-table">
<thead>
<tr>
<td>Date</td>
<td>Time</td>
<td>Round</td>
<td>Circuit</td>
<td>Location</td>
</tr>
</thead>
<?PHP
foreach($myObject as $key=>$item);
?>
<tr>
<td><?PHP echo $item->Date; ?></td>
<td><?PHP echo $item->Time; ?></td>
<td><?PHP echo $item->Round; ?></td>
<td><?PHP echo $item->Circuit; ?></td>
<td><?PHP echo $item->Location; ?></td>
</tr>
<?PHP
}
?>
</table>
</div>
My error is:
Notice: Undefined property: stdClass::$Date in C:\xampp\htdocs\challenge\app\view\challenge.php on line 38
Notice: Undefined property: stdClass::$Time in C:\xampp\htdocs\challenge\app\view\challenge.php on line 39
Notice: Undefined property: stdClass::$Round in C:\xampp\htdocs\challenge\app\view\challenge.php on line 40
Notice: Undefined property: stdClass::$Circuit in C:\xampp\htdocs\challenge\app\view\challenge.php on line 41

I have just looked at your json format in comparison to the code. The path mapping to the json is incorrect. I have rectified that below;
Please review the following:
PHP code:
$myData = file_get_contents("http://ergast.com/api/f1/current.json");
$myObject = json_decode($myData);
$myObjectMap = $myObject->MRData->RaceTable->Races;
For each format:
<?php foreach($myObjectMap as $key => $item): ?>
<tr>
<td><?PHP echo $item->date; ?></td>
<td><?PHP echo $item->time; ?></td>
<td><?PHP echo $item->round; ?></td>
<td><?PHP echo $item->Circuit->circuitId; ?></td>
<td><?PHP echo $item->Circuit->Location->country; ?></td>
</tr>
<?php endforeach; ?>
Full Code:
<html>
<head>
<title>PHP</title>
</head>
<body>
<?php
$myData = file_get_contents("http://ergast.com/api/f1/current.json");
$myObject = json_decode($myData);
$myObjectMap = $myObject->MRData->RaceTable->Races;
?>
<table>
<thead>
<tr>
<td>Date</td>
<td>Time</td>
<td>Round</td>
<td>Circuit</td>
<td>Location</td>
</tr>
</thead>
<tbody>
<?php foreach($myObjectMap as $key => $item): ?>
<tr>
<td><?PHP echo $item->date; ?></td>
<td><?PHP echo $item->time; ?></td>
<td><?PHP echo $item->round; ?></td>
<td><?PHP echo $item->Circuit->circuitId; ?></td>
<td><?PHP echo $item->Circuit->Location->country; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</body>
</html>

The problem is that decoded array looks differently. Dump your encoded array before the loop to understand the right data structure or use JSON beautify service. Also use the right upper/lowercase to address properties. Updated loop may look like this:
<div class="data-table-wrapper">
<?php
$myData = file_get_contents("http://ergast.com/api/f1/current.json");
$myObject = json_decode($myData);
?>
<table class="data-table">
<thead>
<tr>
<td>Date</td>
<td>Time</td>
<td>Round</td>
<td>Circuit</td>
<td>Location</td>
</tr>
<?PHP
foreach($myObject->MRData->RaceTable->Races as $key=>$item){
?>
<tr>
<td><?PHP echo $item->date; ?></td>
<td><?PHP echo $item->time; ?></td>
<td><?PHP echo $item->round; ?></td>
<td><?PHP echo $item->Circuit->circuitName; ?></td>
<td><?PHP echo $item->Circuit->Location->locality; ?></td>
</tr>
<?PHP
}
?>
</table>
</div>
</div>

Related

Parsing XML from a URL address and converting into a table using PHP

I'm trying to take the following xml from this url http://r7j8v4x4.map2.ssl.hwcdn.net/NOD_R.xml and make a table with the data. I've verified I can pull the data by using print_r($xml); but I can't get the data to dump into the table. This is what I've got so far, which is probably wrong. Can anyone help me out with the proper code to use?
<?php
$url = "http://r7j8v4x4.map2.ssl.hwcdn.net/NOD_R.xml";
$xml = simplexml_load_file($url);
?>
<table>
<thead>
<tr>
<col><span style="font-weight:bold">Day</span></col>
<col><span style="font-weight:bold">Time(Eastern)</span></col>
<col><span style="font-weight:bold">Reservoir Elev. (behind dam)*</span</col>
<col><span style="font-weight:bold">Tailwater Elev. (below dam)*</span></col>
<col><span style="font-weight:bold">Avg Hourly Discharge*</span></col>
</tr>
</thead>
<tbody>
<?php foreach ($xml->RESULTSET->ROW as $obs) :?>
<tr>
<td><?php echo $obs->obs_day; ?></td>
<td><?php echo $obs->obs_hr; ?></td>
<td><?php echo $obs->upstream_elev; ?></td>
<td><?php echo $obs->downstream_elev; ?></td>
<td><?php echo $obs->avg_hourly_discharge; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
As the XML has 2 <RESULTSET> elements, the RESULTSET->ROW was just picking the first one. So change that to RESULTSET[1]->ROW and you will get the data your after.
You also need to ensure that you use the same case for each element name...
<?php foreach ($xml->RESULTSET[1]->ROW as $obs) :?>
<tr>
<td><?php echo $obs->OBS_DAY; ?></td>
<td><?php echo $obs->OBS_HR; ?></td>
<td><?php echo $obs->UPSTREAM_ELEV; ?></td>
<td><?php echo $obs->DOWNSTREAM_ELEV; ?></td>
<td><?php echo $obs->AVG_HOURLY_DISCHARGE; ?></td>
</tr>
<?php endforeach; ?>

Is windows.setTimeout function getting executed in following code?

I want 3 elements of array to be displayed before setTimeout(30000) function and 3 elements after it;so that there is gap of 30 seconds between both the loops.But I am not getting the desired result.Following is the code:
<?php
$info=array(array("name"=>"abc",
"phone"=>"12345"),
array("name"=>"pqr",
"phone"=>"23456"),
array("name"=>"def",
"phone"=>"34567"),
array("name"=>"asd",
"phone"=>"45678"),
array("name"=>"ghj",
"phone"=>"56789"),
array("name"=>"jkl",
"phone"=>"67566"),
);
echo ( $info[0]["name"]);
?>
<body>
<table>
<tr>
<th>id</th>
<th>Name</th>
<th>phone<th>
</tr>
<?php
$i=1;
foreach ($info as $data) {
if($i==4)
{
break;
}
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $data["name"]; ?></td>
<td><?php echo $data["phone"]; ?></td>
</tr>
<?php $i++;}?>
<script>
window.setTimeout(30000);
</script>
<?php
for($j=3;$j<6;$j++)
{
?>
<tr>
<td><?php echo $j+1; ?></td>
<td><?php echo $info[$j]["name"]; ?></td>
<td><?php echo $info[$j]["phone"]; ?></td>
</tr>
<?php } ?>
</table>

Display JSON info in a PHP table

I am trying to display JSON content in a PHP table but its not working. I can't figure out what I should change.
Here is my code:
<html>
<head>
<title>Download</title>
</head>
<body>
<?php
$myData = file_get_contents("https://youtubetoany.com/#api/json/videostreams/VEou0QBeHlk");
$myObject = json_decode($myData);
$myObjectMap = $myObject->vidInfo;
?>
<table>
<thead>
<tr>
<td>Url</td>
<td>Size</td>
<td>Quality</td>
<td>Type</td>
</tr>
</thead>
<tbody>
<?php foreach($myObjectMap as $key => $item): ?>
<tr>
<td><?PHP echo $item->dloadUrl; ?></td>
<td><?PHP echo $item->rSize; ?></td>
<td><?PHP echo $item->round; ?></td>
<td><?PHP echo $item->quality; ?></td>
<td><?PHP echo $item->ftype; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</body>
</html>
This is what I get in my browser:
Url Size Quality Type
Your source has some javascript attached. You need to get rid of that:
$myData = file_get_contents("https://youtubetoany.com/#api/json/videostreams/VEou0QBeHlk");
// get the substring from start til the first occurence of "<script"
$myRealData = substr($myData,0,strpos($myData,"<script"));
$myObject = json_decode($myRealData);
BUT this source doesn't seem to be made to be grabbed. So I won't rely on that source or that it'll stay the way you find it now.
I just found probably why its not working. The link returns a JavaScript attached to the bottom of the JSON. So here is my solution.
<html>
<head>
<title>Download</title>
</head>
<body>
<?php
$myData = file_get_contents("https://youtubetoany.com/#api/json/videostreams/VEou0QBeHlk");
// This up to the last occurrence of the "}"
$json_block = substr($myData, 0, strripos($myData, "}"));
$myObject = json_decode($json_block);
$myObjectMap = $myObject->vidInfo;
?>
<table>
<thead>
<tr>
<td>Url</td>
<td>Size</td>
<td>Quality</td>
<td>Type</td>
</tr>
</thead>
<tbody>
<?php foreach($myObjectMap as $key => $item): ?>
<tr>
<td><?PHP echo $item->dloadUrl; ?></td>
<td><?PHP echo $item->rSize; ?></td>
<td><?PHP echo $item->round; ?></td>
<td><?PHP echo $item->quality; ?></td>
<td><?PHP echo $item->ftype; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</body>
</html>

I got Error Notice: Array to string conversion in my code php

hi guys i got problem in my code
i don't know where come form this my code
<h1><?php echo $item["title"]; ?> </h1>
<table>
<tr>
<th>Catageory</th>
<td><?php echo $item["category"]; ?></td>
</tr>
<tr>
<th>Genre</th>
<td><?php echo $item["genre"]; ?></td>
</tr>
<tr>
<th>Format</th>
<td><?php echo $item["format"]; ?></td>
</tr>
<tr>
<th>Year</th>
<td><?php echo $item["year"]; ?></td>
</tr>
<?php
if(strtolower($item["category"]) == "books"){
?>
<tr>
<th>Authors</th>
<td><?php echo $item["authors"]; ?></td>
</tr>
<tr>
<th>Publisher</th>
<td><?php echo $item["publisher"]; ?></td>
</tr>
<tr>
<th>ISBN</th>
<td><?php echo $item["isbn"]; ?></td>
</tr>
<?php } ?>
And the error message,
and Iam sure all code are same i don't what's problem !!
As per your image, you are trying to print string on line 58, but you are getting array from your resultant query. So try to var_dump($yourvar) and check that are you getting required string or an array
You are trying to echo an array variable which is not right. Use the below debug code and you have to parse the array however you want.
Authors -> <?php echo "<pre>";print_r($item["authors"]);echo "<pre>"; ?>
my problem is solved
by replace
<td><?php echo $item["authors"]; ?></td>
to
<td><?php echo implode(",", $item["authors"]); ?></td>

Working with multiple xml with same format

I am working on a xml to table project which would use PHP.
<? $xml = new SimpleXMLElement('http://example.com/genXML.php?product=388', 0, TRUE);
?>
<table>
<thead>
<tr>
<th>Symbol</th>
<th>Name</th>
<th>Price</th>
<th>Change</th>
<th>Percentage</th>
<th>High</th>
<th>Low</th>
</tr>
</thead>
<tbody>
<?php foreach ($xml->product as $check) :?>
<tr>
<td><?php echo $check->symbol; ?></td>
<td><?php echo $check->name->chinese; ?></td>
<td><?php echo $check->price; ?></td>
<td><?php echo $check->change; ?></td>
<td><?php echo $check->pct_change; ?></td>
<td><?php echo $check->high; ?></td>
<td><?php echo $check->low; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
As product will have their own unique code, I would like the programe to show all the details in one table.
Is it possible to use MYSQL Database to store the product code that i need to ref. and show them out on a single web page? Thanks!

Categories