Invalid argument supplied for foreach() on iterating json array - php

I have this json:
[{"nombre":"Example1","idcomplejo":"1","canchafk":"1","cantidadturnos":"35"},
{"nombre":"Example2","idcomplejo":"3","canchafk":"6","cantidadturnos":"29"},
{"nombre":"Example3","idcomplejo":"4","canchafk":"7","cantidadturnos":"6"},
{"nombre":"Example4","idcomplejo":"5","canchafk":"8","cantidadturnos":"4"},
{"nombre":"Example5","idcomplejo":"5","canchafk":"9","cantidadturnos":"2"}]
I wanna show in my table into my HTML page, but I have this error:
Invalid argument supplied for foreach()
This is the part of code where I getting the error:
<table cellpadding="0" cellspacing="0">
<thead>
<tr>
<th>ID Complejo></th>
<th># Cancha></th>
<th>Cantidad Turnos></th>
</tr>
</thead>
<tbody>
<?php foreach ($cantidadturnos as $turno): ?>
<tr>
<td><?= h($turno->idComplejo) ?></td>
<td><?= h($turno->canchaFK) ?></td>
<td><?= h($turno->cantidadTurnos) ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
the var_dump of cantidadturnos is the json that i put above.
Thanks for help

The json is in the form of a JavaScript object. PHP cannot read it so it needs to parse the json with json_decode(), like this:
$cantidadturnos = json_decode('[{"nombre":"Example1","idcomplejo":"1","canchafk":"1","cantidadturnos":"35"},
{"nombre":"Example2","idcomplejo":"3","canchafk":"6","cantidadturnos":"29"},
{"nombre":"Example3","idcomplejo":"4","canchafk":"7","cantidadturnos":"6"},
{"nombre":"Example4","idcomplejo":"5","canchafk":"8","cantidadturnos":"4"},
{"nombre":"Example5","idcomplejo":"5","canchafk":"9","cantidadturnos":"2"}]');
Now $cantidadturnos is in the form of an array the foreach can work with like this:
foreach ($cantidadturnos as $turno) {
// do something
}

Related

Php variable in a html table

I'm trying to display an html table in which I'd like to show some php variables.
My code is the following:
<?php
$title = the_title();
?>
<table>
<thead>
<tr>
<th>Name</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $title?></td>
</tr>
</tbody>
</table>
In the output I can see the word "Name" as expected but it doesn't print the $title variable (there is a empty space instead).
I've already read some of the questions on the site but they doesn't help me because they suggest to write the same code that I wrote.

print invoice data blank php foreach loop error

I want to print invoice data. but some error only one image print. how to print all data in an invoice I am using PHP 7.2 and loop foreach please help me...
error->Warning: Invalid argument supplied for foreach() in D:\xammp\htdocs\tam\admin\invoice.php on line 133
PHP GET id wise invoice data script
<?php
require 'setting/config.php';
$id=$_GET['id'];
$query="select * from orders where id='$id'";
$galrun=mysqli_query($conn, $query);
$result=mysqli_fetch_assoc($galrun);
$total=$result['total'];
$array[0]=$result['image'];
$array[1]=$result['productName'];
$array[2]=$result['product_qty'];
$array[3]=$result['salseprice'];
$resultdata= implode(",", $array);
?>
HTML invoice
<table class="table">
<thead>
<tr>
<th>Sr. No.</th>
<th>image</th>
<th>Description</th>
<th>Qty</th>
<th>Amount</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<?php
foreach ($resultdata as $item){
?>
<tr>
<td> ? </td>
<td><img src="image/product/<?php echo $item[0] ; ?>" style="width:100px;height:50px;"></td>
<td><?php echo $item[1] ; ?></td>
<td><?php echo $item[2] ; ?></td>
<td><?php echo $item[3] ; ?></td>
<td><?php echo $total ; ?</td>
</tr>
<?php }?>
</tbody>
</table>
You try to loop through $resultdata, but it is a string, because of this part:
$resultdata= implode(",", $array);
PHP implode() returns a string.

how to use a variable as a folder name in path [duplicate]

I want to have a link to another page, with particular subject, How can I pass the id ?
<table class="table table-hover">
<thead>
<tr>
<th>Subject</th>
<th>Add Topic</th>
</tr>
</thead>
<tbody>
<tr>
<?php foreach($subjects as $sub):?>
<td><?php echo $sub->subject;?></td>
<td>Add Topic</td>
</tr>
<?php endforeach;?>
</tbody>
</table>
You still need to echo it out.
<?php foreach($subjects as $sub): ?>
<tr>
<td><?php echo $sub->subject ?></td>
<td>Add Topic</td>
</tr>
<?php endforeach; ?>
Please try the following:
<table class="table table-hover">
<thead>
<tr>
<th>Subject</th>
<th>Add Topic</th>
</tr>
</thead>
<tbody>
<?php foreach($subjects as $sub):?>
<tr>
<td><?php echo $sub->subject;?></td>
<td>Add Topic</td>
</tr>
<?php endforeach;?>
</tbody>
</table>
and then on the page : approve.php
<?php
$subjectId = $_GET['id'];
?>
$subjectId will give you the corresponding subject id with which you can move forward with the functionality.
Note: foreach should start either outside <tr> and end outside </tr> or it can be inside <tr> </tr>
You need to enclose your variable in a PHP tag:
<?php foreach($subjects as $sub):?>
<tr>
<td><?php echo $sub->subject;?></td>
<td>Add Topic</td>
</tr>
<?php endforeach;?>
There is also a short form echo tag enabled on most PHP servers <?= $variable ?>
On the subsequent page you retrieve the parameter from the GET array:
$subject = $_GET['id'];
If you're passing this value to the database you should do some validation:
if ($_GET['id']) { // check parameter was passed
$subject = int_val($_GET['id']) // cast whatever was passed to integer
} else {
// handle no subject case
}
Yes you can, it will be considered as GET. as for how to pass it.
Edit the following:
<td>Add Topic</td>
This is the part:
<?=$sub->id?>
You closed php tag when u started adding html therefore open it again to echo php vars.

iterate over array in php

Ok, so I've read loads of articles and I think I'm at risk of duplicating, but cant work this one out.
I have an array that is being returned by a PHP function, I've called it getLeague();
The structure of the array is:
body[0]->position
body[0]->teamname
body[0]->points
and obviously the results increment from 0 -16 as that's the amount of teams in my league.
I'm trying to tabulate the array by calling getLeague() and iterating over the returned array to print into a table.
I'm trying at the minute to work out the basic for each loop, after that I'll shoehorn it into a table. Can you help me with the foreach? I've tried:
<table class="table table-striped">
<thead>
<tr>
<th>Position</th>
<th>Team</th>
<th>Points</th>
</tr>
</thead>
<tbody>
<?php
$rows = getLeague();
foreach ($rows as $row):
?>
<tr>
<td><?= $row->body->position; ?></td>
<td><?= $row->body->teamname; ?></td>
<td><?= $row->body->points; ?></td>
</tr>
<? endforeach ?>
</tbody>
</table>
Any help appreciated.
Without seeing more on that data structure, I can't say for certain, but I think you want:
foreach ($rows->body as $row):
And:
$row->position

PHP passing variable id through href

I want to have a link to another page, with particular subject, How can I pass the id ?
<table class="table table-hover">
<thead>
<tr>
<th>Subject</th>
<th>Add Topic</th>
</tr>
</thead>
<tbody>
<tr>
<?php foreach($subjects as $sub):?>
<td><?php echo $sub->subject;?></td>
<td>Add Topic</td>
</tr>
<?php endforeach;?>
</tbody>
</table>
You still need to echo it out.
<?php foreach($subjects as $sub): ?>
<tr>
<td><?php echo $sub->subject ?></td>
<td>Add Topic</td>
</tr>
<?php endforeach; ?>
Please try the following:
<table class="table table-hover">
<thead>
<tr>
<th>Subject</th>
<th>Add Topic</th>
</tr>
</thead>
<tbody>
<?php foreach($subjects as $sub):?>
<tr>
<td><?php echo $sub->subject;?></td>
<td>Add Topic</td>
</tr>
<?php endforeach;?>
</tbody>
</table>
and then on the page : approve.php
<?php
$subjectId = $_GET['id'];
?>
$subjectId will give you the corresponding subject id with which you can move forward with the functionality.
Note: foreach should start either outside <tr> and end outside </tr> or it can be inside <tr> </tr>
You need to enclose your variable in a PHP tag:
<?php foreach($subjects as $sub):?>
<tr>
<td><?php echo $sub->subject;?></td>
<td>Add Topic</td>
</tr>
<?php endforeach;?>
There is also a short form echo tag enabled on most PHP servers <?= $variable ?>
On the subsequent page you retrieve the parameter from the GET array:
$subject = $_GET['id'];
If you're passing this value to the database you should do some validation:
if ($_GET['id']) { // check parameter was passed
$subject = int_val($_GET['id']) // cast whatever was passed to integer
} else {
// handle no subject case
}
Yes you can, it will be considered as GET. as for how to pass it.
Edit the following:
<td>Add Topic</td>
This is the part:
<?=$sub->id?>
You closed php tag when u started adding html therefore open it again to echo php vars.

Categories