Display JSON info in a PHP table - php

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>

Related

How to print out two queries in one table?

I have two queries in a form. I print out the results like this:
<table>
<thead>
<tr>
<th>Heading1</th>
<th>Heading2</th>
<th>Heading3</th>
<th>Heading4</th>
</tr>
</thead>
<tbody>
<?php foreach ($result1 as $row1) : ?>
<tr>
<td><?php echo escape($row1["Column1"]); ?></td>
<td><?php echo escape($row1["Column2"]); ?></td>
<td><?php echo escape($row1["Column3"]); ?></td>
<?php endforeach; ?> </tr>
<tr> <?php foreach ($result2 as $row2) : ?>
<td><?php echo escape($row2["Column4"]); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
I want to present each result in a different column. But the result from the second query ("Column4") seems to be presented in a second table (??). It's not echoed next to the other columns, but below:
Current Output
How can I fix this issue?
You have to change
<?php endforeach; ?> </tr>
<tr> <?php foreach ($result2 as $row2) : ?>
into
<?php endforeach; ?>
<?php foreach ($result2 as $row2) : ?>
You have begun another row ('tr' tag closed and re-opened).
Try this fix...
I have removed the things that are causing it to echo on next line
Now this will work...
<table>
<thead>
<tr>
<th>Heading1</th>
<th>Heading2</th>
<th>Heading3</th>
<th>Heading4</th>
</tr>
</thead>
<tbody>
<?php foreach ($result1 as $row1) : ?>
<tr>
<td><?php echo escape($row1["Column1"]); ?></td>
<td><?php echo escape($row1["Column2"]); ?></td>
<td><?php echo escape($row1["Column3"]); ?></td>
<?php endforeach; ?>
<?php foreach ($result2 as $row2) : ?>
<td><?php echo escape($row2["Column4"]); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>

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; ?>

Display json content in php table

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>

Parse error: syntax error, unexpected end of file in [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
I am getting the "Parse error: syntax error, unexpected end of file in C:\xampp\htdocs\football\index.php on line 123" error on my webserver (using XAMPP). My code is the following:
<?php
include 'FootballData.php';
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>phplib football-data.org</title>
<link href="./css/bootstrap.min.css" type="text/css" rel="stylesheet" />
</head>
<body>
<div class="container">
<div class="page-header">
<h1>Showcasing some library functions...</h1>
</div>
<?php
// Create instance of API class
$api = new FootballData();
// fetch and dump summary data for premier league' season 2015/16
$soccerseason = $api->getSoccerseasonById(398);
echo "<p><hr><p>"; ?>
<h3>Fixtures for the 1st matchday of <? echo $soccerseason->payload->caption; ?></h3>
<table class="table table-striped">
<tr>
<th>HomeTeam</th>
<th></th>
<th>AwayTeam</th>
<th colspan="3">Result</th>
</tr>
<?php foreach ($soccerseason->getFixturesByMatchday(1) as $fixture) { ?>
<tr>
<td><? echo $fixture->homeTeamName; ?></td>
<td>-</td>
<td><? echo $fixture->awayTeamName; ?></td>
<td><? echo $fixture->result->goalsHomeTeam; ?></td>
<td>:</td>
<td><? echo $fixture->result->goalsAwayTeam; ?></td>
</tr>
<? } ?>
</table>
<?
echo "<p><hr><p>";
// fetch all available upcoming fixtures for the next week and display
$now = new DateTime();
$end = new DateTime(); $end->add(new DateInterval('P7D'));
$response = $api->getFixturesForDateRange($now->format('Y-m-d'), $end->format('Y-m-d'));
?>
<h3>Upcoming fixtures next 7 days</h3>
<table class="table table-striped">
<tr>
<th>HomeTeam</th>
<th></th>
<th>AwayTeam</th>
<th colspan="3">Result</th>
</tr>
<?php foreach ($response->fixtures as $fixture) { ?>
<tr>
<td><? echo $fixture->homeTeamName; ?></td>
<td>-</td>
<td><? echo $fixture->awayTeamName; ?></td>
<td><? echo $fixture->result->goalsHomeTeam; ?></td>
<td>:</td>
<td><? echo $fixture->result->goalsAwayTeam; ?></td>
</tr>
<? } ?>
</table>
<?
echo "<p><hr><p>";
// search for desired team
$searchQuery = $api->searchTeam(urlencode("Real Madrid"));
// var_dump searchQuery and inspect for results
$response = $api->getTeamById($searchQuery->teams[0]->id);
$fixtures = $response->getFixtures('home')->fixtures;
?>
<h3>All home matches of Real Madrid:</h3>
<table class="table table-striped">
<tr>
<th>HomeTeam</th>
<th></th>
<th>AwayTeam</th>
<th colspan="3">Result</th>
</tr>
<?php foreach ($fixtures as $fixture) { ?>
<tr>
<td><? echo $fixture->homeTeamName; ?></td>
<td>-</td>
<td><? echo $fixture->awayTeamName; ?></td>
<td><? echo $fixture->result->goalsHomeTeam; ?></td>
<td>:</td>
<td><? echo $fixture->result->goalsAwayTeam; ?></td>
</tr>
<? } ?>
</table>
<?
echo "<p><hr><p>";
// fetch players for a specific team
$team = $api->getTeamById($searchQuery->teams[0]->id);
?>
<h3>Players of <? echo $team->_payload->name; ?></h3>
<table class="table table-striped">
<tr>
<th>Name</th>
<th>Position</th>
<th>Jersey Number</th>
<th>Date of birth</th>
</tr>
<? foreach ($team->getPlayers() as $player) { ?>
<tr>
<td><? echo $player->name; ?></td>
<td><? echo $player->position; ?></td>
<td><? echo $player->jerseyNumber; ?></td>
<td><? echo $player->dateOfBirth; ?></td>
</tr>
<? } ?>
</table>
</div>
</body>
</html>
How can I solve this?
See alternative PHP syntax. You cannot use it like this <?php foreach ($fixtures as $fixture) { ?> <?php } ?> rather <?php foreach ($fixtures as $fixture): ?> <?php endforeach; ?>
You should avoid this {?> and this: <?php}
You shouldn't put brackets directly close to the open/close php tag, but it separate with a space: { ?> <?php { also avoid <? and use <?php

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