How to decode JSON using PHP in three listing columns? - php

I'm new using JSON and PHP.
I have a big list of products in JSON that is being decoded through PHP and I need this list to be splited in 3 columns. How can I do this?
Here is the code I'm using now:
<div class="col-md-4">
<div class="section-title">
<ul class="dados">
<?php
$url = 'http://myurl/consult';
$data = file_get_contents($url);
$characters = json_decode($data);
foreach ($characters as $character) {
?>
<li class="nomes-ies"><?php echo $character->nome . '</li>'; } ?>
</ul>
</div>
</div>

You could use array_chunk() to split your array in three elements. Then, use a foreach on this array, around the <div class="col-md-4">:
<?php
$url = 'http://myurl/consult';
$data = file_get_contents($url);
$characters = json_decode($data);
$cols = array_chunk($characters, ceil(count($characters) / 3));
foreach ($cols as $data) {
?>
<div class="col-md-4">
<div class="section-title">
<ul class="dados">
<?php foreach ($data as $character) { ?>
<li class="nomes-ies"><?php echo $character->nome ?></li>
<?php } ?>
</ul>
</div>
</div>
<?php } // end foreach $cols ?>
Note that the number of elements used in array_chunk() must be computed with ceil() to avoid to get 4 columns. The last column could have less elements than the other.

Related

Print the specific values from the array

I get the result from my database i this type of array format.Now i want to print the question,answer and category name from the array what I do?
<?php $v1 = '';
foreach($var as $data){
?>
<div class="faqHeader"> <?php echo $data['category_name'];?> </div>
<div class="panel-group" id="<?php echo $data->title;?>">
I tried these cod but not get the answer.In $var i get the all the array value
Things to consider:-
1.Your sub-array is again an array so you need to use foreach() on sub-array too. (So basically two foreach())
2.You need to close divs as well as foreach() loops too.
So code need to be like below:-
<?php
foreach($var as $data){
foreach($data['data'] as $dat){
?>
<div class="faqHeader"> <?php echo $dat['category_name'];?>
<div class="panel-group" id="<?php echo $dat['questions'];?>"><?php echo $dat['questions'];?></div>
<div class="panel-group" id="<?php echo $dat['answer'];?>"><?php echo $dat['answer'];?></div>
</div>
<?php } }?>
you can use two loops to simplify your task:-
foreach($var as $index=>data){
echo $data['name']; // this will be your category name
foreach($data as $questionIndex=>$questionData){
echo $questionData['question'];
echo $questionData['answer'];
}
}
Since your array contains multiple data and data contains multiple values itself, you need two foreaches here:
foreach($var as $item) {
foreach($item['data'] as $info) {
var_dump($info);
}
}
Please try this code
<?php
$v1 = '';
foreach($var as $data){
?>
<div class="faqHeader"> <?php echo $data['data'][0]['category_name'];?> </div>
<div class="panel-group" id="<?php echo $data['name'];?>">
<?php
}
?>
You don't have any 'title' index in your array so you may change it to 'name'

string is not getting exploded

Here is my code where explode function is not getting correctly.code looks like this
<div id="demo-1" data-zs-src='[<?php foreach( $slides as $slide ){?>"<?php echo base_url();?>uploads/<?php echo $slide->image;?>"<?php }?>]' data-zs-overlay="dots">
<div class="demo-inner-content">
<h1><span>Tasty</span> & <span>Healthy</span></h1>
<p>For those who have taste for life.</p>
</div>
my result looks like this
<div id="demo-1" data-zs-src='["http://localhost/fahiz_kitchen/uploads/upload-file1496902770.jpg""http://localhost/fahiz_kitchen/uploads/upload-file1496901910.gif""http://localhost/fahiz_kitchen/uploads/upload-file1496901900.jpg""http://localhost/fahiz_kitchen/uploads/upload-file1496901887.jpg"]' data-zs-overlay="dots">
<div class="demo-inner-content">
<h1><span>Tasty</span> & <span>Healthy</span></h1>
<p>For those who have taste for life.</p>
</div>
</div>
i want to get comma in between the result that means my result should be like this
<div id="demo-1" data-zs-src='["http://localhost/fahiz_kitchen/uploads/upload-file1496902770.jpg",
"http://localhost/fahiz_kitchen/uploads/upload-file1496901910.gif",
"http://localhost/fahiz_kitchen/uploads/upload-file1496901900.jpg",
"http://localhost/fahiz_kitchen/uploads/upload-file1496901887.jpg"
plz use this;
<?php foreach( $slides as $slide ){
$images= base_url()."uploads/". $slide->image;
var_dump($images);
$arr= explode(',',$images);
print_r($arr);
}
?>
Chnage it like.
$data = [<?php
foreach( $slides as $slide ){
?>"<?php echo base_url();?>uploads/<?php echo $slide->image;?>",
<?php }?>];
$finaldata = rtrim($data, ',');
<div id="demo-1" data-zs-src="'.$finaldata.'" data-zs-overlay="dots">
<div class="demo-inner-content">
<h1><span>Tasty</span> & <span>Healthy</span></h1>
<p>For those who have taste for life.</p>
</div>
You are starting php inside already started it.
After doing some changes i got the result and the code used for that is as follows
<?php foreach ( $slides as $slide ) {
$data[] = $images='"'.base_url()."uploads/". $slide->image.'"';
$y=implode(',',$data);
}?>
<div id="demo-1" data-zs-src='[<?php echo $y;?>]' data-zs-overlay="dots">
<div class="demo-inner-content">
<h1><span>Tasty</span> & <span>Healthy</span></h1>
<p>For those who have taste for life.</p>
</div>

PHP Foreach with two or more arrays

How to use Foreach on two or more php arrays?
My arrays are $boardType, $hotelPrice and $hotelName
Solution is to add $index in the other arrays as suffix
<?php foreach ($hotelName as $index => $name):?>
<div>
<p><?php echo $name;?></p>
<p><?php echo $hotelPrice[$index];?></p>
<p><?php echo $boardType[$index];?></p>
</div>
<?php endforeach?>
This should work for you:
(Here I just go through all 3 arrays with array_map() an print them in the structure you want)
<?php
array_map(function($v1, $v2, $v3){
echo "<div>";
echo "<p>$v1</p>";
echo "<p>$v2</p>";
echo "<p>$v3</p>";
echo "</div>";
}, $boardType, $hotelPrice, $hotelName);
?>
Example input/output:
$boardType = [1,2,3];
$hotelPrice = [4,5,6];
$hotelName = [7,8,9];
<div>
<p>1</p>
<p>4</p>
<p>7</p>
</div>
<div>
<p>2</p>
<p>5</p>
<p>8</p>
</div>
<div>
<p>3</p>
<p>6</p>
<p>9</p>
</div>
If all arrays have exactly the same size you could use each to iterate through the other arrays at the same time as $hotelName:
<?php foreach ($hotelName as $index => $name):?>
$price = each($hotelPrice);
$boardType = each($boardType);
<div>
<p><?php echo $name;?></p>
</div>
<?php endforeach?>
However, in that case it would probably being better to have just a single array containing all the data.

Creating a single var for a foreach loop

i am trying to get my list of tags but using a foreach so that i can put each tag into a list element.
What i used to do was:
$tagsArray = "these,are,some,tags"
// Explode the tags
$tagsArray = explode(",",$tags);
<ul>
<?php
// Create a list for the tags
foreach($tagsArray as $var)
{
echo "<li>$var</li>";
}
?>
</ul>
However, i now need to display this array inside an echo as well as other items.
Example:
<?php
echo "
<div id='editInfomation'>
<div id='Title'>This is a title</div>
<div id='tags'>
<ul>$tags</ul>
</div>
</div>
";
?>
How should i create a $tags variable that will contain all the exploded tags but in list element tags? Thanks.
This is what you need.
$tagsArray = "these,are,some,tags";
$tags = "<li>".implode("</li>,<li>",explode(",",$tagsArray ))."</li>";
And you simply print
<?php
echo "
<div id='editInfomation'>
<div id='Title'>This is a title</div>
<div id='tags'>
<ul>$tags</ul>
</div>
</div>
";
?>
I'm not 100% sure on what you're asking, but:
How should i create a $tags variable that will contain all the exploded tags but in list element tags?
You can push each element into an array, formatted:
$output = array();
foreach($tagsArray as $var) {
$output[] = "<li>$var</li>";
}
... then:
echo "<ul>" . implode($output) . "</ul>";

Echo array value in different sections

I want to echo a value of an array in a way that the first 4 would echo in a section and the rest (can be 5 or more) in a different section. For example:
<?php $items = $kStore->getItems(); ?>
<div id="section1">
<?php foreach($items as $item) {
if($count <= 4){
?>
<h1><?php echo $item->getId(); ?></h1>
<?php } }?>
</div>
<div id="section2">
<?php foreach($items as $item) {
if($count > 4){
?>
<h1><?php echo $item->getId(); ?></h1>
<?php } } ?>
</div>
I imagine it has to be something 'like that' but dont know how to get the count. Also, I don't want to go through the array twice, I feel it's not the fastest way to do it. Any thoughts? Thanks!
You can use array_slice to offset the start and limit the length of the section of the array you iterate over:
<div id="section1">
<?php foreach(array_slice($items, 0, 4) as $item) { … } ?>
</div>
<div id="section2">
<?php foreach(array_slice($items, 4) as $item) { … } ?>
</div>
Update
If $items is not an array, but merely a non-array object that implements Iterator, you can't use this approach, but you can probably use LimitIterator instead.
<?php
$sectionOneItems = new LimitIterator($items, 0, 4);
$sectionTwoItems = new LimitIterator($items, 4);
?>
<div id="section1">
<?php foreach($sectionOneItems as $item) { … } ?>
</div>
<div id="section2">
<?php foreach($sectionTwoItems as $item) { … } ?>
</div>
If this, too, does not work, then some more introspection of the object being iterated over is required.
i think this function might be useful for you
array_chunk ().http://www.php.net/manual/en/function.array-chunk.php

Categories