Reputation: 21
I've this part of my script which shows last offers my website is given but for some reason it shows the same offer 3 times as image showing.
This is the code coming with the script
<?php foreach($specialoffers as $offer) if ($tmp++ < 3) { ?>
<div class="col-md-4">
<div class="col-md-12">
<?php if($specialoffers[0]->price > 0){ ?> <span class="wow bounce ttu"><?php echo trans('0536');?> <b><?php echo trans('0388');?></b> <small><?php echo $specialoffers[0]->currCode;?></small> <?php echo $specialoffers[0]->currSymbol; ?><?php echo $specialoffers[0]->price;?></span>
<?php } ?>
<div class="imgLodBg">
<div class="load"></div>
<a href="<?php echo base_url(); ?>offers">
<img data-wow-duration="0.2s" data-wow-delay="1s" class="wow fadeIn" src="<?php echo $specialoffers[0]->thumbnail;?>">
</a>
</div>
</div>
<div class="col-md-12">
<h3 class="offh3"><strong class="ttu"><?php echo character_limiter($specialoffers[0]->title,30);?></strong></h3>
<p class="offp">
<?php echo character_limiter($specialoffers[0]->desc,240);?>
<a href="<?php echo base_url(); ?>offers" >
Read More
</a>
</p>
</div>
</div>
<?php } ?>
and when I tried the below changes the page crashed and I got this error
[12-Aug-2018 13:48:09 UTC] PHP Fatal error: Cannot use object of type stdClass as array in /home/bannabooking/public_html/beta/themes/default/views/home/index.php on line 152
Changed Code:
<?php foreach($specialoffers as $offer) if ($tmp++ < 3) { ?>
<div class="col-md-4">
<div class="col-md-12">
<?php if($offer[0]->price > 0){ ?> <span class="wow bounce ttu"><?php echo trans('0536');?> <b><?php echo trans('0388');?></b> <small><?php echo $offer[0]->currCode;?></small> <?php echo $offer[0]->currSymbol; ?><?php echo $offer[0]->price;?></span>
<?php } ?>
<div class="imgLodBg">
<div class="load"></div>
<a href="<?php echo base_url(); ?>offers">
<img data-wow-duration="0.2s" data-wow-delay="1s" class="wow fadeIn" src="<?php echo $offer[0]->thumbnail;?>">
</a>
</div>
</div>
<div class="col-md-12">
<h3 class="offh3"><strong class="ttu"><?php echo character_limiter($offer[0]->title,30);?></strong></h3>
<p class="offp">
<?php echo character_limiter($offer[0]->desc,240);?>
<a href="<?php echo base_url(); ?>offers" >
Read More
</a>
</p>
</div>
</div>
<?php } ?>
Upvotes: 0
Views: 108
Reputation: 6709
Inside of the foreach () {}
you already have individual $offers
objects. So, instead of accessing them via $offer[0]->price
, you need directly access each $offer->price
, $offer->desc
, etc.
In general, the foreach () {}
approach looks like this:
$offers = [new Offer('a'), new Offer('b')];
foreach ($offers as $offer) {
// $offer here sequentially corresponds
// to each element of the $offers array
echo $offer->price;
}
Another way to accomplish iteration over $offers
array may look like this:
$offers = [new Offer('a'), new Offer('b')];
for ($i = 0; $i < array_length($offers); $i++) {
echo $offers[$i]->price;
}
So, you just need to stick with one of these approaches. And usually foreach
looks more accurate.
Upvotes: 2