-3

I'm getting "Trying to get property of non-object" while trying to this code.

<?php foreach($pages as $page=>$p): ?>
<h2 class="section-title text-center wow fadeInDown"><?php echo $page[0]->title;  ?></h2>
<p class="text-center wow fadeInDown"><?php echo strip_tags(html_entity_decode($page[0]->description)); ?></p>
<?php endforeach; ?>
A. Stha
  • 5
  • 1
  • 3

2 Answers2

1

You need change the variable $page (it´s the key) for the variable $p (The value) into the cicle.

Try this, maybe can help,

<?php foreach($pages as $page=>$p): ?>
<h2 class="section-title text-center wow fadeInDown"><?php echo $p->title;  ?></h2>
<p class="text-center wow fadeInDown"><?php echo strip_tags(html_entity_decode($p->description)); ?></p>
<?php endforeach; ?>
Mauricio Florez
  • 1,112
  • 8
  • 14
  • 2
    Please explain what you changed and how it solves the problem, don't just dump a bunch of code. – Barmar Mar 29 '16 at 04:53
0

Use below code. Here $pages is the object array and you have used foreach to get array values so in foreach you will get values in $p and your array key is $page. So use $p or $p[$page].

<?php foreach($pages as $page=>$p): ?>
<h2 class="section-title text-center wow fadeInDown"><?php echo $p->title;  ?></h2>
<p class="text-center wow fadeInDown"><?php echo strip_tags(html_entity_decode($p->description)); ?></p>
<?php endforeach; ?>
RJParikh
  • 4,096
  • 1
  • 19
  • 36