string is not getting exploded - php

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>

Related

How to decode JSON using PHP in three listing columns?

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.

Foreach loop doesn't work but it gets 2 items from my array?

i made this foreach loop but it just doesn't show me anything on the screen doesn anybody see what the problem is?
There're 2 items of the array $data['page']['children']
<?php foreach ($data['page']['childeren'] as $news): ?>
<div class="donerenNews">
<div class="doneren-image">
<img class="group list-group-image doneren-image" src="<?php echo $site_url ?>assets/img/placeholderSubPage.png" alt="">
</div>
<div class="caption doneren-tekst-left">
<h1>
<?php echo $news['title'] ?>
</h1>
<p>
<?php echo $news['description'] ?>
</p>
</div>
</div>
<?php endforeach; ?>
it looks like you put childreren instead of children in
foreach ($data['page']['childeren']
so change it to
foreach ($data['page']['children']

Trying to get property of non-object in Codeigniter?

My controller code just generate ten copies of the same data. And I put the data in an array of array.Controller Code Below:
for($i=0;$i < $this->per_page;$i++)
{
$temp['title'] = "test";
$temp['link'] = "localhost/cib/index.php";
$temp['month'] = '4';
$temp['day'] = '21';
$temp['authorlink'] = "localhost/cib/index.php/author";
$temp['author'] = "Charles";
$temp['content'] = "tetestersafaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
$results[] = $temp;
}
$data['main_content'] = 'report_list';
$data['posts'] = $results;
$this->load->view('include/templates', $data);
And I show the data generated by controller in the view page report_list, the code of report_list is
<div id="bg_top">
</div>
<div class = "container" id="content">
<div class="row-fluid">
<div class="span9" id="content_left">
<h2>解题报告列表</h2>
<link href="<?php echo base_url('assets/css/style.css') ?>" rel="stylesheet">
<?php if(!empty($posts) && is_array($posts)):?>
<?php foreach($posts as $post):?>
<div class="entry">
<h2 class="entrytitle"><?php echo anchor($post->link, $post->title)?><span></span></h2>
<div class="entrymeta1">
<div class="meta-date">
<span class="meta-month"><?php echo $post->month?></span>
<span class="meta-day"><?php echo $post->day?></span>
</div>
<span class="meta-author"><?php echo anchor($post->authorlink, $post->author)?></span>
<span class="meta-category"></span>
<span class="meta-comment"></span>
<div class="clear"></div>
</div><!-- [entrymeta1] -->
<div class="entrybody">
<p><?php echo $post->content;?><?php echo anchor($post->link, '阅读全文')?></p>
</div>
<div class="entrymeta3">
</div>
</div><!-- [entry] -->
<?php endforeach;?>
<?php endif;?>
</div>
<div class="span3">
<?php echo "hello world2";?>
</div>
</div>
</div>
The question is where am wrong? why is there errors "Trying to get property of non-object"?
I tried to print the data in the controller, it works quite well.
foreach($results as $post)
{
foreach($post as $key=>$value)
{
echo $key.':'.$value;
echo '<br/>';
}
}
You need to print the variables in the view file in array style like this:
<?php echo anchor($post['link'], $post['title'])?>
Still you can always print the array to view the structure:
<?php echo "<pre>";print_r($posts);echo "</pre>";
My assumption is that
You are expecting $posts as array of objects. But in $posts in some loaction there is no object with property month, day etc. So just before the for loop you should look the type of $posts by
var_dump($posts)
This will give you the clear picture about your error
Change all the variables $post->link to array $post['link']
$post->month
$post->day
every fields that are using in template.

Blog page - display latest post top of the page

What I need to do that my latest posts go automatically to top of the page? Now latest post go bottom of the ealier posts.
php:
<div id="container">
<div id="blog">
<div class="grid_9 float-left">
<?php foreach($posts->results as $post): ?>
<div class="post box_shadow">
<h2><?php echo $post->title ?></h2>
<small><?php echo date('d-m-Y',strtotime($post->date)) ?></small>
<p><?php echo Str::limit_word($post->content, 40); ?></p>
<?php echo Lang::line('home.blog_read_more', array(), $lang)->get() ?>
</div>
<?php endforeach ?>
<?php echo $posts->links()?>
</div>
</div>
<div id="footer" class=" box-shadow">
<?php echo stripcslashes($setting->footer)?>
</div>
</div>
I am still newbie so could you give advice what line I need modified or add and where?
If it is exactly the opposite of what your looking for try running array_reverse on it before hitting the loop.
That's how your array is sorted. options: reverse the array or do a for loop starting from the last index going to 0.
Something similar to below:
`$posts->results` has all of your posts
Replace the foreach with a for.
$count = count($posts->results);
for($i = $count-1; $i <= 0; $i--) {
$post = $posts->results[$i];
//display post
}
The simplest but probably less efficient way is to reverse the array right before the foreach loop.

On every third iteration in PHP

I would like to output some specific HTML on the third iteration of a loop in PHP. Here is my code:
<?php foreach ($imgArray as $row): ?>
<div class="img_grid"><?= $row ?></div>
<?php endforeach; ?>
On the third iteration of this loop, Instead of displaying:
<div class="img_grid"><?= $row ?></div>
I would like to display:
<div class="img_grid_3"><?= $row ?></div>
I would like to end up with this if my array looped 8 times:
<div class="img_grid">[some html]</div>
<div class="img_grid">[some html]</div>
<div class="img_grid_3">[some html]</div>
<div class="img_grid">[some html]</div>
<div class="img_grid">[some html]</div>
<div class="img_grid_3">[some html]</div>
<div class="img_grid">[some html]</div>
<div class="img_grid">[some html]</div>
Thanks
Assuming $imgArray is an array and not an associative array (i.e. it has numeric indices), this is what you want:
<?php foreach($imgArray as $idx => $row): ?>
<?php if($idx % 3 == 2): ?>
<div class="img_grid_3"><?php echo $row; ?></div>
<?php else: ?>
<div class="img_grid"><?php echo $row; ?></div>
<?php endif; ?>
<?php endforeach; ?>
You could tighten it up a bit like this:
<?php foreach($imgArray as $idx => $row):
if($idx % 3 == 2) {
$css_class = 'img_grid_3';
} else {
$css_class = 'img_grid';
}
?>
<div class="<?php echo $css_class; ?>"><?php echo $row; ?></div>
<?php endforeach; ?>
Or even more (some folks would just go with a ternary conditional inline in the HTML), but the law of diminishing returns kicks in eventually with regard to readability. Hopefully this gives you the right idea, though.

Categories