Retrieving json child with php (inside another foreach) - php

I'm triying to retrieve some data from a json file, in this case the json file have a child call "photo" and that child storage several file names, I need to fetch those file names inside an outer foreach. Here is my code:
My JSON file looks like this:
{
"nigiri": [{
"code": "NS-1",
"title": "Maguro",
"description": "6pc tuna",
"price": "$10.00",
"photo": ["HD-21-a.jpg", "HD-21-b.jpg", "HD-21-c.jpg", "HD-21-c.jpg"]
}, {
"title": "Scottish",
"code": "NS-2",
"price": "$9.50",
"photo": ["HD-21-a.jpg", "HD-21-b.jpg", "HD-21-c.jpg", "HD-21-c.jpg"],
"description": "6pc salmon"
}, {
"title": "Buri",
"code": "NS-3",
"price": "$10.00",
"photo": "NS-3.jpg",
"description": "6pc Hamachi"
}]
}
And my PHP file looks like this:
<?php
$getfile = file_get_contents('backend/menu.json');
$jsonfile = json_decode($getfile);
$type = htmlentities($_GET["type"]);
if(empty($_GET['type']))
{
header('Location: index.php');
exit;
}
?>
// This way I only get the first value
<?php foreach ($jsonfile->$type as $index => $obj): ?>
<h2>Name of the plate: <?php echo $obj->title; ?></h2>
<img src="img/plates/thumbs/<?php echo $obj->photo; ?>" alt="<?php echo $obj->title; ?>">
<?php endforeach; ?>
// I want to retrieve the others values from the "photo" child but still inside the previous foreach. I've try this:
<?php foreach ($jsonfile->$type as $index => $obj): ?>
<h2>Name of the plate: <?php echo $obj->title; ?></h2>
<?php foreach ($jsonfile->$type->photo as $index => $photos): ?>
<img src="img/plates/thumbs/<?php echo $photos->photo; ?>" alt="<?php echo $obj->title; ?>">
<?php endforeach; ?>

Sometimes it's best just to stay in PHP rather than going in and out of HTML:
foreach ($jsonfile->$type as $obj) {
echo "<h2>Name of the plate: $obj->title</h2>\n";
$photos = is_array($obj->photo) ? $obj->photo : array($obj->photo);
foreach ($photos as $photo)
echo "<img src=\"img/plates/thumbs/$photo\" alt=\"$obj->title\">\n";
}
Note that since $photo may not be an array, we need to check first before we attempt to iterate it, and if it isn't, make it into one.
Output (for your sample data, with $type = "nigiri"):
<h2>Name of the plate: Maguro</h2>
<img src="img/plates/thumbs/HD-21-a.jpg" alt="Maguro">
<img src="img/plates/thumbs/HD-21-b.jpg" alt="Maguro">
<img src="img/plates/thumbs/HD-21-c.jpg" alt="Maguro">
<img src="img/plates/thumbs/HD-21-c.jpg" alt="Maguro">
<h2>Name of the plate: Scottish</h2>
<img src="img/plates/thumbs/HD-21-a.jpg" alt="Scottish">
<img src="img/plates/thumbs/HD-21-b.jpg" alt="Scottish">
<img src="img/plates/thumbs/HD-21-c.jpg" alt="Scottish">
<img src="img/plates/thumbs/HD-21-c.jpg" alt="Scottish">
<h2>Name of the plate: Buri</h2>
<img src="img/plates/thumbs/NS-3.jpg" alt="Buri">

Your mean is you want output photo if you have a lot photos?
<?php foreach ($jsonfile->$type as $index => $obj): ?>
<h2>Name of the plate: <?php echo $obj->title; ?></h2>
<?php if (is_array($obj->photo)): ?>
<?php foreach ($jsonfile->$type->photo as $index => $photos): ?>
<img src="img/plates/thumbs/<?php echo $photos->photo; ?>" alt="<?php echo $obj->title; ?>">
<?php endforeach; ?>
<?php else ?>
<img src="img/plates/thumbs/<?php echo $obj->photo; ?>" alt="<?php echo $obj->title; ?>">
<?php endif; ?>
<?php endforeach; ?>

You must use nested loops. Something like that:
<?php
$getfile = file_get_contents('backend/menu.json');
$jsonfile = json_decode($getfile);
$type = htmlentities($_GET["type"]);
if(empty($_GET['type']))
{
header('Location: index.php');
exit;
}
?>
<?php foreach ($jsonfile->$type as $index => $obj): ?>
<h2>Name of the plate: <?php echo $obj->title; ?></h2>
<?php if(is_array($obj->photo)): ?>
<?php foreach ($obj->photo as $photo): ?>
<img src="img/plates/thumbs/<?php echo $photo; ?>" alt="<?php echo $obj->title; ?>">
<?php endforeach; ?>
<?php else: ?>
<img src="img/plates/thumbs/<?php echo $obj->photo; ?>" alt="<?php echo $obj->title; ?>">
<?php endif; ?>
<?php endforeach; ?>

Related

PHP: Foreach with "if-else" statement

I'm doing some changes on an online store that is running OpenCart 2.2.
When browsing below the categories on the left side, there are 2 carousels that show different promotions. I want to modify it that before the second carousel there is a description text. To do that I should edit the template file. And here is where I get lost..
This is the code in the template file:
<div id="banner<?php echo $module; ?>" class="owl-carousel">
<?php foreach ($banners as $banner) { ?>
<div class="item">
<?php if ($banner['link']) { ?>
<img src="<?php echo $banner['image']; ?>" alt="<?php echo $banner['title']; ?>" class="img-responsive" />
<?php } else { ?>
<img src="<?php echo $banner['image']; ?>" alt="<?php echo $banner['title']; ?>" class="img-responsive" />
<?php } ?>
</div>
<?php } ?>
</div>
From I see in the browser inspector and in my case, this code generates 2 banners with the ids - "banner0" and "banner1". The description text should go at the top of the code (right before ). If I use a simple paragraph, it gets displayed twice - above each banner.. How should I change it so that it will display the paragraph only above the second banner (id - banner1)?
I was thinking about an if-else statement, but I'm not sure if that will work... Could anyone help out a bit? My knowledge in PHP isn't much... :S
Thanks in advance!
Best regards,
Tsvetko Krastev
Then you need to know that this is the second time round the foreach loop. So change the foreach to include the index like this, and add a test inside the loop for $i == 1
<div id="banner<?php echo $module; ?>" class="owl-carousel">
<?php //foreach ($banners as $banner) {
foreach ($banners as $i => $banner) {
?>
<div class="item">
<?php if ($banner['link']) {
if ( $i == 1 ) {
echo 'YOUR HTML CONTAINING SOME TEXT HERE';
}
?>
<img src="<?php echo $banner['image']; ?>" alt="<?php echo $banner['title']; ?>" class="img-responsive" />
<?php } else { ?>
<img src="<?php echo $banner['image']; ?>" alt="<?php echo $banner['title']; ?>" class="img-responsive" />
<?php } ?>
</div>
<?php } ?>
</div>
If I get the code right, $module is 0/1 (if id's get values banner0 and banner1), then this should work:
<div id="banner<?php echo $module; ?>" class="owl-carousel">
<?php foreach ($banners as $banner) { ?>
<?php if ($module == "1") { ?>
<p>Your description</p>
<?php } ?>
<div class="item">
<?php if ($banner['link']) { ?>
<img src="<?php echo $banner['image']; ?>" alt="<?php echo $banner['title']; ?>" class="img-responsive" />
<?php } else { ?>
<img src="<?php echo $banner['image']; ?>" alt="<?php echo $banner['title']; ?>" class="img-responsive" />
<?php } ?>
</div>
<?php } ?>
</div>
Thank you so very much guys!!! Tried both versions and they throw a error, but looking more carefully into the code and both your solutions, I came up with this:
<div id="desc<?php echo $module; ?>">
<?php if ($module == 1) { ?>
<p>Description</p>
<?php } ?>
<div id="banner<?php echo $module; ?>" class="owl-carousel">
<?php foreach ($banners as $banner) { ?>
<div class="item">
<?php if ($banner['link']) { ?>
<img src="<?php echo $banner['image']; ?>" alt="<?php echo $banner['title']; ?>" class="img-responsive" />
<?php } else { ?>
<img src="<?php echo $banner['image']; ?>" alt="<?php echo $banner['title']; ?>" class="img-responsive" />
<?php } ?>
</div>
<?php } ?>
</div>
</div>
Thank you very much again for the quick responses and the help! :) :3
Best regards,
Tsvetko Krastev

php json print array within array

I have this json item in my json file
{
"results": [
{
"title": "Brick Mansions",
"released": "2014",
"restricted": "12 \u00e1ra",
"imdb": "6.0\/10 5,532 atkv.",
"imdbLink": "http:\/\/www.imdb.com\/title\/tt1430612",
"image": "http:\/\/kvikmyndir.is\/images\/poster\/9260_500.jpg",
"showtimes": [
{
"theater": "Laugar\u00e1sb\u00ed\u00f3",
"schedule": [
"20:00",
"22:00"
]
}
]
}
I would like to print theater and times under schedule with php.
I have printed everything else. like so... bare in mind that $movys is the object that contains the json content.
<?php foreach($movys->results as $movie): ?>
<h2> <?php echo $movie->title; ?> </h2>
<div class="posters"><img src="<?php echo $movie->image; ?>" alt="Plakat fyrir <?php echo $movie->title; ?>"></div> <ul>
<li><p> <?php echo $movie->released; ?> </p></li>
<li><p> <?php echo $movie->restricted; ?> </p></li>
<li><p> <?php echo $movie->imdb; ?> </p></li>
<li><p> <?php echo $movie->imdbLink; ?> </p></li>
</ul>
?????? <?php foreach($movys->results{"showtimes"} as $hallo): ?>
?????? <h2><?php echo $hallo->theater; ?></h2>
?????? <?php endforeach; ?>
<?php endforeach; ?>
<?php
foreach($movie->showtimes as $showtimes)
{
echo $showtimes->theater . ":\n";
foreach ( showtimes->schedule as $schedule )
echo "> " . $schedule . "\n";
}

Show articles in view

I have a question:
I make a pre for news:
Array
(
[0] => Array
(
[name] => tag
[id] => 57
[title] => Article1,Article2,Article3
[views] => 53,54,58
[smallimage] => Koala-08.jpg,Jellyfish-08.jpg,Mountain-08.jpg
[date] => 2014-05-07 09:21:58,2014-05-08 09:24:38,2014-05-08 14:36:40
)
)
How to create the foreach in view to show 1 title 1 views and 1 date...I create an foreach but show first all titles,all views,all dates;
My foreach:
<?php if ($news):?>
<?php foreach($news as $n):?>
<p>Tag:<?php echo $n['name'] ?></p>
<div class="container">
<img src="<?php echo config_item('content_folder');?>news/small/<?php echo $n['smallimage']; ?>" alt="">
<?php echo $n['title'] ?><br/>
<?php echo $n['views'] ?>
<?php endforeach ?>
<?php endif; ?>
With this foreach I get: Article1,Article2,Article3 53,54,58...I want to get Article1 53,Article2 54, Article3 58....Help me please
Try this:
<?php
$news = array(array( "tag",
57,
"Article1,Article2,Article3",
"53,54,58",
"Koala-08.jpg,Jellyfish-08.jpg,Mountain-08.jpg",
"2014-05-07 09:21:58,2014-05-08 09:24:38,2014-05-08 14:36:40"
) );
?>
<?php if ($news){?>
<p>Tag:<?php echo $news[0][0]; ?></p>
<div class="container">
<img src="<?php echo config_item('content_folder');?>news/small/<?php echo $news[0][4]; ?>" alt="">
<?php $views = explode(',',$news[0][3]);?>
<?php $article = explode(',',$news[0][2]);?>
<?php for($i=0;$i<count($article);$i++){
echo $article[$i].'-'.$views[$i];
} ?>
</div>
<?php }?>
I don't know where is "config_item('content_folder')" function result.Try this. And let me know whether it is works or not. Feel free to ask help.
<?php foreach($news as $n):?>
<?php $titles = explode(",", $n['title']); ?>
<?php $smallimages = explode(",", $n['smallimage']); ?>
<?php $views= explode(",", $n['views']); ?>
<p>Tag:<?php echo $n['name'] ?></p>
<div class="container">
<img src="<?php echo config_item('content_folder');?>news/small/<?php echo smallimages[0]; ?>" alt="">
<?php echo title[0] ?><br/>
<?php echo views[0] ?>
<img src="<?php echo config_item('content_folder');?>news/small/<?php echo smallimages[1]; ?>" alt="">
<?php echo title[1] ?><br/>
<?php echo views[1] ?>
<img src="<?php echo config_item('content_folder');?>news/small/<?php echo smallimages[2]; ?>" alt="">
<?php echo title[2] ?><br/>
<?php echo views[2] ?>
<?php endforeach ?>
<?php endif; ?>
I am assuming that for example 'Article1,Article2,Article3' is a string, so you would have to use:
$n['title'] = explode(',',$n['title'])
To make it into an array. Same goes for views, smallimage and date. Here's an example on how to apply that:
<p>Tag:<?php echo $n['name'] ?></p>
<div id="container">
<?php
$n['title'] = explode(',',$n['title'])
$n['views'] = explode(',',$n['views'])
$n['smallimage'] = explode(',',$n['smallimage'])
foreach ($n['title'] as $key=>$value) {
echo "<img src='" . config_item('content_folder') . "news/small" . $n['smallimage'][$key] . "' alt='' />";
echo $n['title'][$key] . "<br />";
echo $n['views'][$key]
}
?>
</div>
You would place this in the existing foreach-loop.

Get an array and display it php

We are bulding a simple module in php, but we have a error that produces a bad encoding for html page :
<?php
$ids = array('363', '367', '366', '365','364','371','370','456','461');
?>
<div class="Staff">
<?php foreach ($ids as $ids) {
$user = Foundry::user($ids);
}?>
<?php foreach ($ids as $user => $ids) { ?>
<img alt="<?php echo $user->getName();?>" src="<?php echo $user->getAvatar();?>" data-popbox="module://easysocial/profile/popbox" data-popbox-position="top-left" data-user-id="<?php echo $user ?>" />
<?php echo $user->getName();?>
<?php}?>
</div>
Any Idea? , thanks as always for the help.
It should like below:
<?php foreach ($ids as $id) {
$user = Foundry::user($id);
?>
<img alt="<?php echo $user->getName();?>" src="<?php echo $user->getAvatar();?>" data-popbox="module://easysocial/profile/popbox" data-popbox-position="top-left" data-user-id="<?php echo $user ?>" />
<?php echo $user->getName();?>
<?php } ?>
have you tried something like this:
<?php
$ids = array('363', '367', '366', '365','364','371','370','456','461');
?>
<div class="Staff">
<?php foreach ($ids as $id) { ?>
$user = Foundry::user($id);
<img alt="<?php echo $user->getName();?>" src="<?php echo $user->getAvatar();?>" data-popbox="module://easysocial/profile/popbox" data-popbox-position="top-left" data-user-id="<?php echo $user ?>" />
<?php echo $user->getName();?>
<?php}?>
</div>

Output image src with SimpleXML

I'm trying to output an image with SimpleXML, but the image tag doesn't appear in the source code.
Can anyone help me outpout this image:
Here's my XML and code:
<?php foreach($xml->Event as $event) { ?>
<li>
<a href="<?php echo $event->link; ?>">
<?php if ($event->Media['url'] == !null) { ?>
<img src="<?php echo $event->Media['url'];?>" alt="<?php echo $event->title;?> thumbnail" />
<?php } ?>
<h3><?php echo $event->title; ?></h3>
<p><strong><?php echo $event->beginDate; ?> at <?php echo $event->beginTime; ?></strong></p>
<p><?php echo $event->location; ?></p>
</a>
</li>
<?php } ?>
Your issue is here:
<?php if ($event->Media['url'] == !null) { ?>
<img src="<?php echo $event->Media['url'];?>" alt="<?php echo $event->title;?> thumbnail" />
<?php } ?>
You're trying to access url as though it were an attribute, you need to access it as a child element by using ->url instead.
<?php if ($event->Media->url != null) { ?>
<img src="<?php echo $event->Media->url;?>" alt="<?php echo $event->title;?> thumbnail" />
<?php } ?>
EDIT: By the way, == !null works as you expect, but != null is a bit friendlier and less confusing
Your if statement is incorrect. It should be:
if ($event->Media['url'] != null)

Categories