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

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

Related

If row status is 1 show the approved status in the table

I want to output statement you are approve if the row status = 1, the data can be output now i just want the statement saying approve.
here's my code
<div class="row">
<div class="col-md-12">
<table class="table table-striped">
<tr>
<th>Nominee Name</th>
<th>Year</th>
<th>Department</th>
<th>StudentID</th>
<th>Position</th>
<th>View File</th>
<th>Nominee Photo</th>
<th>Nominated by</th>
<th>status</th>
</tr>
<?php
foreach ($nominees as $noms) {
?>
<tr>
<td><?php echo $noms['name']; ?></td>
<td><?php echo $noms['year']; ?></td>
<td><?php echo $noms['department']; ?></td>
<td><?php echo $noms['studentid']; ?></td>
<td><?php echo $noms['position']; ?></td>
<td></td>
<td><img src="<?php echo base_url(); ?>uploads/<?php echo $noms['photo']; ?>" style='width:50px;height:50px;'></td>
<td><?php echo $noms['nomname']; ?></td>
<?php if ($noms['approve'] == 1) { ?>
<td>Approve</td>
<?php } else { ?>
<td>pending</td>
<?php } ?>
</tr>
<?php } ?>
</table>
</div>
</div>
If the $nominees array returns the stays, then you can do like this
<?php if ( $nom['status'] == 1) { ?>
<td>Approve</td>
<?php }else{ ?>
<td>pending</td>
<?php }?>

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>

using variable from array outside of foreach loop

Im trying to display variables from an array. I am using a foreach loop, however I need to display $order['campaign_name'] before the loop so that it only shows up once. How can I do this? If I change it to $orders['campaign_name'] I get an undefined index error.
<div class="table-responsive">
<table class="table" id="component-table">
<?php if ($orders) { ?>
<?php foreach ($orders as $order) { ?>
<thead>
<tr>
<td colspan=100%><h3><?php echo $order['campaign_name']; ?></h3></td>
</tr>
</thead>
<tbody>
<tr class="campaign-list" id="campaign-list">
<td><?php echo $order['component_name']; ?></td>
<td><?php echo $order['component_owner']; ?></td>
<td><?php echo $order['component_date']; ?></td>
<td><?php echo $order['campaign_code']; ?></td>
</tr>
<?php } ?>
<?php } else { ?>
<tr>
<td class="text-center" colspan="8"><?php echo $text_no_results; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
You are trying to get those value which is not exist before loop. You are directly calling VALUE.
Easily just put index value behind tha array
echo $orders[0]['campaign_name']
It will print your value.
You would need need to know which index of the array you want to show, but if you want to show the first index of the array you can use $orders[0]['component_name'].
<div class="table-responsive">
<table class="table" id="component-table">
<?php if ($orders) { ?>
<thead>
<tr>
<td colspan=100%><h3><?php echo $orders[0]['campaign_name']; ?></h3></td>
</tr>
</thead>
<tbody>
<?php foreach ($orders as $order) { ?>
<tr class="campaign-list" id="campaign-list">
<td><?php echo $order['component_name']; ?></td>
<td><?php echo $order['component_owner']; ?></td>
<td><?php echo $order['component_date']; ?></td>
<td><?php echo $order['campaign_code']; ?></td>
</tr>
<?php } ?>
</tbody>
<?php } else { ?>
<tbody>
<tr>
<td class="text-center" colspan="8"><?php echo $text_no_results; ?></td>
</tr>
</tbody>
<?php } ?>
</table>
</div>
Your campaign name continues to echo out because you have it inside of your foreach loop; if you want it to display only once, make sure to place it above your foreach loop.
Here's a less cluttered example to learn from:
<table>
...
<?php
if ($orders):
echo $order['campaign_name'];
foreach($orders as $order):
echo '...'; // other components
endforeach;
else:
echo $text_no_results;
endif;
?>
...
</table>

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>

dompdf page break with codeigniter and foreach loop

I am using codeigniter and dompdf to generate a pdf document.
the document is generated with a foreach loop. each loop creates a few table rows. I want to have a pagebreak between the rows of each loop.
my relevent codeigniter view syntax is:
<table width=100%>
<?php foreach($summary as $summary){ ?>
<tr class="firstrow">
<td colspan="10">
<?php echo "<b>Customer</b>: <font color='blue'>".$summary->caccountname; ?>
<?php echo "</font> <b>Order Number</b>: <font color='blue'>".$summary->OrderNum; ?>
<?php echo "</font> <b>Sales rep</b>: <font color='blue'>".$summary->RepName; ?>
<?php echo "</font> <b>Due Date</b>: <font color='blue'>".substr($summary->DueDate,0,10); ?>
</td>
</tr>
<tr>
<td colspan="10"><hr>
</td>
</tr>
<tr>
<td colspan="10">
<table class="sortable" width="90%">
<tr><td ><b>Product</td><td ><b>Ordered</td><td ><b>Processed</td><td ><b>Outstanding</td><td ><b>Dispatched</td><td ><b>Available (incl FJ)</td><td ><b>Available FJ</td></tr>
<?php foreach($$linedetails as $linedetails){ ?>
<tr>
<td><?php echo $linedetails->Product; ?></td>
<td><?php echo $linedetails->cubicvolume; ?></td>
<td><?php echo $linedetails->Processed; ?></td>
<td><?php echo $linedetails->Outstanding; ?></td>
<td><?php echo $linedetails->Dispatched; ?></td>
<td><?php echo $linedetails->TotalVolume; ?></td>
<td><?php echo $linedetails->FJVolume; ?></td>
</tr>
<?php } ?>
</table>
</td>
</tr>
<?php } ?>
</table>
I have given the first tr a class of firstrow, I would like a page break to appear before this line every time.
As such I have applied the following style sheet in my page header:
<style>
#media print
{
.firstrow {page-break-before:always}
}
</style>
This doesnt work.
How can I acheive my desired result of placeing a page break before each loop firstrow TR?
Thanks as always
To make Pagebreak work with tr, write a line of css for tr
tr { page-break-inside:avoid; page-break-after:auto }
Try above code.
If not working, then add
table { page-break-inside:auto }
Try this, I am doing reports exactly same way
<?php
$nbsp5 = " ";
foreach($summary as $summary) { ?>
<table width="100%" style="page-break-after:always;">
<tr>
<td colspan="10">
<?php echo "<b>Customer</b>: <font color='blue'>".$summary->caccountname; ?>
<?php echo "</font>".$nbsp5 ." <b>Order Number</b>: <font color='blue'>".$summary->OrderNum; ?>
<?php echo "</font>".$nbsp5 ." <b>Sales rep</b>: <font color='blue'>".$summary->RepName; ?>
<?php echo "</font>".$nbsp5 ." <b>Due Date</b>: <font color='blue'>".substr($summary->DueDate,0,10); ?>
</td>
</tr>
<tr>
<td colspan="10"><hr></td>
</tr>
<tr>
<td colspan="10">
<table class="sortable" width="90%">
<tr>
<td><b>Product</b></td>
<td><b>Ordered</b></td>
<td><b>Processed</b></td>
<td><b>Outstanding</b></td>
<td><b>Dispatched</b></td>
<td><b>Available (incl FJ)</b></td>
<td><b>Available FJ</b></td>
</tr>
<?php foreach($$linedetails as $linedetails){ ?>
<tr>
<td><?php echo $linedetails->Product; ?></td>
<td><?php echo $linedetails->cubicvolume; ?></td>
<td><?php echo $linedetails->Processed; ?></td>
<td><?php echo $linedetails->Outstanding; ?></td>
<td><?php echo $linedetails->Dispatched; ?></td>
<td><?php echo $linedetails->TotalVolume; ?></td>
<td><?php echo $linedetails->FJVolume; ?></td>
</tr>
<?php } ?>
</table>
</td>
</tr>
</table>
<?php } ?>
(first <table> tag moved inside loop)
Please try this answer for both header and footer.

Categories