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";
}
Related
I have this code in PHP
<?php foreach($this->group('filter') as $i => $fields): ?>
<?php $newField = $this->field('name#'.$i)->value(); ?>
<?php $finalvar = explode(",", $newField); ?>
<a data-filter=<?php foreach($finalvar as $fletter) : ?>".filter_<?php echo standardize($fletter); ?>">
<?php endforeach; ?>
<?php if($this->field('icon#'.$i)->value()): ?><i class="<?php echo $this->field('icon#'.$i)->value(); ?>"></i><?php endif; ?>
<span class="name"><?php if($this->field('label_items#'.$i)->value()): ?><?php echo $this->field('label_items#'.$i)->value(); ?><?php else: ?><?php echo $this->field('name#'.$i)->value(); ?><?php endif; ?>
</span>
</a>
<?php endforeach; ?>
PHP code works but I have a problem on the HTML code. The generated HTML code is this:
<a data-filter=".filter_country">
".filter_capital;
<span class="name">Australia</span>
</a>
My goal is to implement this one:
<a data-filter=".filter_country,.filter_capital"
<span class="name">Australia</span>
</a>
I know it's something in front of me but I cannot see it. Does anyone have an idea concerning the syntax?
Check your syntax:
<?php foreach ($this->group('filter') as $i => $fields) : ?>
<?php $newField = $this->field('name#' . $i)->value(); ?>
<?php $finalvar = explode(",", $newField); ?>
<a data-filter="
<?php
$i = 1;
foreach ($finalvar as $fletter) { ?>
.filter_<?php echo standardize($fletter); ?>
<?php if ($i < count($fletter)) { ?>, <?php } ?>
<?php $i++; } ?>
">
<?php if ($this->field('icon#' . $i)->value()) : ?><i class="<?php echo $this->field('icon#' . $i)->value(); ?>"></i><?php endif; ?>
<span class="name"><?php if ($this->field('label_items#' . $i)->value()) : ?><?php echo $this->field('label_items#' . $i)->value(); ?><?php else : ?><?php echo $this->field('name#' . $i)->value(); ?><?php endif; ?>
</span>
</a>
<?php endforeach; ?>
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; ?>
This is the loop I want to limit this to 2 outputs and create another loop for further outputs please help.. I am a beginner
Enter code here
<?php if ($informations) { ?>
<div class="column">
<h3>
<?php echo $text_information; ?>
</h3>
<ul>
<?php foreach ($informations as $information){ ?>
<li><a href="<?php echo $information['href']; ?>">
<?php echo $information['title']; ?>
</a></li>
<?php } ?>
</ul>
</div>
<?php } ?>
Try this. .
<?php if ($informations) { ?>
<div class="column">
<h3><?php echo $text_information; ?></h3>
<ul>
<?php
$i=1;
foreach ($informations as $information){
if($i==2)
{
break;
}
?>
<li><?php echo $information['title']; ?></li>
<?php
$i++;
} ?>
</ul>
</div>
<?php } ?>
For other values . .
<?php if ($informations) { ?>
<div class="column">
<h3><?php echo $text_information; ?></h3>
<ul>
<?php
$i=1;
foreach ($informations as $information){
if($i<2)
{
continue;
}
?>
<li><?php echo $information['title']; ?></li>
<?php
$i++;
} ?>
</ul>
</div>
<?php } ?>
Bit vague but I guess you are looking for array_chunk,
$new_array = array_chunk($informations, 2, true);
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.
I'm relatively new to php and I've been trying all day long to get this to work. I have a multiple array and want to echo each one in a specific format, and in groups.
So i've gone through stackoverflow and found this help:
<? foreach(array_chunk($projetos, 6) as $curta ) { ?>
<li style='display:block'>
<div class="film"> <?php echo $curta[0]['titulo']; ?></div>
<div class="film"> <?php echo $curta[1]['titulo']; ?></div>
<div class="film"> <?php echo $curta[2]['titulo']; ?></div>
<div class="film"> <?php echo $curta[3]['titulo']; ?></div>
<div class="film"> <?php echo $curta[4]['titulo']; ?></div>
<div class="film"> <?php echo $curta[5]['titulo']; ?></div>
</li>
<? }; ?>
And this returns what I want but the last items of the array doesnt fill up to 6 and creates 2 extras empty divs and messes up the design.
This is a single example of the array i have:
<?php
$projetos = array (
"ugm" => array (
"id" => "ugm",
"titulo" => "Una Guerra Más",
"video" => "imagem",
"videoid" => "",
"height" => "$video_height_wide",
"sinopse" => "Um soldado moribundo deseja enviar sua última carta. Curta indisponível por exibição em festivais. Feito em parceria com a Universidad del Cine e LightBox Studios.",
"elenco" => "Ignacio J. Durruty - Rodrigo Soler - Ulisses Levanavicius - Aron Matschulat Aguiar",
"idioma" => "Inglês - Português",
"camera" => "Sony EX1",
"formato" => "HD",
"duracao" => "9'55''",
"ano" => "2012",
"tipo" => "Curta",
"credito" => "Direção - Edição - Produção - Roteiro",
), (...)
I want to be able to edit just one div that will be the master for the others... and using the implode I've read on another question but didn't worked to echo the strings I wanted..
Would please someone help out?
thanks in advance!
<?php foreach(array_chunk($projetos, 6) as $curta ) { ?>
<li style='display:block'>
<?php foreach($curta as $detail) { ?>
<div class="film"> <?php echo $detail['titulo']; ?></div>
<?php } ?>
</li>
<? }; ?>
Why not use a loop to iterate over $curta?
<? foreach(array_chunk($projetos, 6) as $curta ) { ?>
<li style='display:block'>
<? foreach($curta as $c) { ?>
<div class="film"><? echo $c['titulo']; ?></div>
<? } ?>
</li>
<? }; ?>
but the last items of the array doesnt fill up to 6
As this is going to happen most of the time, you can't assume that every chunk has 6 elements, so you'll have to iterate over the chunk:
<? foreach(array_chunk($projetos, 6) as $curta ) { ?>
<li style='display:block'>
<? foreach($curta as $c) { ?>
<div class="film"> <?php echo $c['ugm']['titulo']; ?></div>
<? }; ?>
</li>
<? }; ?>
This way you are sure to display no empty divs.
This lines:
<div class="film"> <?php echo $curta[0]['titulo']; ?></div>
should be like this:
<div class="film"> <?php echo $curta[0]['ugm']['titulo']; ?></div>
that should do what you want.