Echoing nested JSON into PHP - php

I have a JSON feed which I am parsing via PHP. I am having issues getting some nested elements to echo which I would appreciate some assistance on.. I've looked at loads of related posts here but cant seem to get the logic to work on my specific JSON feed. Could someone advise what I am doing wrong?
JSON feed is here > https://api.lever.co/v0/postings/leverdemo?skip=1&limit=3&mode=json
The elements, I am struggling to parse are the "categories" parent and child nodes of "team", "location" and "commitment".
I was thinking this would work - but it does not...
<?php
$url = 'feed.json';
$data = file_get_contents($url);
$characters = json_decode($data, true);
?>
<table>
<tbody>
<tr>
<th>Job title</th>
<th>Team</th>
<th>Location</th>
<th>Commitment</th>
<th>DescriptionPlain</th>
<th>applyUrl</th>
</tr>
<?php foreach ($characters as $character) : ?>
<tr>
<td> <?php echo $character['text']; ?> </td>
<td> <?php echo $character['categories'][2]['team'] ?></td>
<td> <?php echo $character['categories'][2]->team ?></td>
<td> <?php echo $character['categories'][1]->location ?></td>
<td> <?php echo $character['categories'][0]->commitment ?></td>
<td> <?php echo $character['descriptionPlain']; ?> </td>
<td> <?php echo $character['applyUrl']; ?> </td>
</tr>
<?php endforeach; ?>
</tbody>
Note, its just the categories children that fail to echo? Also noticed that if I use the full url in the $url variable it all fails? But from local it works??
Any ideas??? Thanks!

It should be:
<td> <?php echo $character['categories']["team"] ?></td>
<td> <?php echo $character['categories']["location"] ?></td>
<td> <?php echo $character['categories']["commitment"] ?></td>
instead. The numeric keys are not present on the array in the data. Also "categories" is not an object, so you cannot use the arrow (->) notation.

EDIT
You get an error because you are trying to access an object, actually you have an array, here is the solution hope it helps :
<?php
$url = 'https://api.lever.co/v0/postings/leverdemo?skip=1&limit=3&mode=json';
$data = file_get_contents($url);
$characters = json_decode($data, true);
$nb = count($characters);
?>
<table>
<tbody>
<tr>
<th>Job title</th>
<th>Team</th>
<th>Location</th>
<th>Commitment</th>
<th>DescriptionPlain</th>
<th>applyUrl</th>
</tr>
<?php while($nb > 0){
$nb--;
$nb_lists = count($characters[$nb]['lists']);
?>
<tr>
<?php
while($nb_lists > 0){
$nb_lists--;
?>
<td> <?php if(isset($characters[$nb]['lists'][$nb_lists]['text'])){ echo $characters[$nb]['lists'][$nb_lists]['text'];} ?> </td>
<?php } ?>
<td> <?php if(isset($characters[$nb]['categories']['team'])){echo $characters[$nb]['categories']['team'];} ?></td>
<td> <?php if(isset($characters[$nb]['categories']['team'])){echo $characters[$nb]['categories']['team'];} ?></td>
<td> <?php if(isset($characters[$nb]['categories']['location'])) {echo $characters[$nb]['categories']['location'];} ?></td>
<td> <?php if(isset($characters[$nb]['categories']['commitment'])){ echo $characters[$nb]['categories']['commitment'];} ?></td>
<td> <?php if(isset($characters[$nb]['descriptionPlain'])){echo $characters[$nb]['descriptionPlain']; }?> </td>
<td> <?php if(isset($characters[$nb]['applyUrl'])){echo $characters[$nb]['applyUrl'];} ?> </td>
</tr>
<?php } ?>
</tbody>

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>

How to pull each game value if main value if true JSON in PHP

PHP File for getting json value
$url = 'demo.json'; // path to your JSON file
$data = file_get_contents($url); // put the contents of the file into a variable
$characters = json_decode($data, true); // decode the JSON feed
<table>
<tbody>
<tr>
<th>id</th>
</tr>
<?php foreach ($characters['events'][0]['offers'][0]['outcomes'] as $character) : ?>
<tr>
<td> <?php echo $character['id']; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
Thank you for the solutions.
only need to show data if main value is true from each game. Thanks Dhamo
Check Main Conditions In for Loop
<?php
foreach ($characters['events'] as $events){
foreach ($events['offers'] as $offers){
if($offers['main']==true || $offers['main']==1){
foreach ($offers['outcomes'] as $character) : ?>
<tr>
<td> <?php echo $character['id']; ?></td>
<td> <?php echo $character['label']; ?></td>
<td> <?php echo $character['line']; ?></td>
<td> <?php echo $character['oddsAmerican']; ?></td>
<td> <?php echo $character['participant']; ?></td>
</tr>
<?php endforeach;
}
}
}?>

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>

How To Control Looping HTML Table PHP

So, i've table like in picture
I want put looping row person on left side to singgle row select box on right side, like by color. And this is a code table right side.
<table>
<tr>
<td>No.</td>
<td>A</td>
<td>B</td>
<td>C</td>
</tr>
<?php if(!empty($query)) { $number=1 ; foreach($query as $row) { ?>
<tr>
<td>
<?php echo $number++ ?>.</td>
<td>
<?php echo $row->column_a ?></td>
<td>
<?php echo $row->column_b ?></td>
<td>
<?php echo $row->column_c ?></td>
</tr>
<?php }} ?>
</table>
How to control looping iteration, to make this is happen?
Tanks a lot.
My friend just help to fix my problem,
<table>
<tr>
<td>No.</td>
<td>A</td>
<td>B</td>
<td>C</td>
</tr>
<?php if(!empty($query)) { $number=1;$count=0; foreach($query as $row) { ?>
<?php if($count % 3 == 0){ ?>
<tr>
<td>
<?php echo $number++ ?>.</td>
<?php } ?>
<td>
<?php echo $row->column_a ?></td>
<td>
<?php echo $row->column_b ?></td>
<td>
<?php echo $row->column_c ?></td>
<?php $count++; if($count % 3 == 0){ ?>
</tr>
<?php }}} ?>
</table>

how to store json_encode results in a table

I have a database which works by displaying first and last_names of all employees in the database, i can display it but when it displays, it's not formatted. I want to try and put the results in a table but I'm not sure how I would.
I thought I would have to echo json_encode(echo.<td>$posts</td>) or something like that
<?php foreach($query as $row): ?>
<tr>
<td>
<?php $arr = array(
'first_name' => $row->first_name,
'last_name' => $row->last_name,
); ?>
<?php $posts[] = $arr;?>
</tr>
<?php endforeach; ?>
<?php echo json_encode($posts);?>
This is how its displayed now
[{"first_name":"Georgi","last_name":"Facello"},
{"first_name":"Georgi","last_name":"Atchley"}]
Nothing is written between your <tr> and such .. you are just assigning to posts and then printing it out as JSON after the fact which makes no sense.
<?php foreach... ?>
<tr>
<td>
<?php echo $row->first_name ?>
</td>
<td>
<? php echo $row->last_name ?>
</td>
</tr>
<?php endforeach ?>
You're gonna have to build the table 'by hand'. Like this:
<table>
<?php foreach($query as $row): ?>
<tr>
<td>
<?php echo $row->first_name; ?>
</td>
<td>
<?php echo $row->last_name; ?>
</td>
</tr>
<?php endforeach; ?>
</table>
<table>
<thead>
<th> First Name </th>
<th> Last Name </th>
</thead>
<tbody>
<?php foreach($query as $row): ?>
<tr>
<td> <?php echo $row->first_name ?> </td>
<td> <?php echo $row->last_name ?> </td>
</tr>
<?php endforeach; ?>
</tbody>
</table>

Categories