foreach not working for some reason

Hossam Ahmed

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.

Image Here

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 } ?>
Ivan Velichko

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

SortProperties not working for some reason

From Dev

key listener not working for some reason

From Dev

key listener not working for some reason

From Dev

my simple intrest calculator is not working for some reason

From Dev

The code isn't working for some reason

From Dev

Text-align center not working for some reason?

From Dev

for some reason, document.getElementById().innerHTML is not working?

From Dev

HashMap.containsKey not working for some reason

From Dev

Div onclick dialog open not working for some reason

From Dev

The which() function in R is not working for some reason

From Dev

Phalcon - Lazy loading is not working in Models for some strange reason

From Dev

I'm trying to Rename a File using Java But It is Not Working for Some Reason

From Dev

C# Delegates. Casting not working for some reason

From Dev

express js route is not working, getting a 404 for some reason

From Dev

gem install pg -v '0.17.1' not working for some reason

From Dev

PHP foreach stopped working after some time

From Dev

Comboboxes are linked for some reason

From Dev

Replace ' with \' for some reason puts \\'

From Dev

On click isn't working for some reason .. where did I go wrong?

From Dev

Installed libinput-gestures to have mac-like gestures but it is not working for some reason

From Dev

Instantiation not working for a threading reason

From Dev

Component sharing variable for some reason

From Dev

onfling() not being called for some reason

From Dev

onfling() not being called for some reason

From Dev

iTextSharp for some reason is not writing to MemoryStream

From Dev

Text is always bold for some reason

From Dev

Simple program crashes for some reason

From Dev

Python asyncio stuck for some reason

From Dev

Mongoose model not saving for some reason

Related Related

  1. 1

    SortProperties not working for some reason

  2. 2

    key listener not working for some reason

  3. 3

    key listener not working for some reason

  4. 4

    my simple intrest calculator is not working for some reason

  5. 5

    The code isn't working for some reason

  6. 6

    Text-align center not working for some reason?

  7. 7

    for some reason, document.getElementById().innerHTML is not working?

  8. 8

    HashMap.containsKey not working for some reason

  9. 9

    Div onclick dialog open not working for some reason

  10. 10

    The which() function in R is not working for some reason

  11. 11

    Phalcon - Lazy loading is not working in Models for some strange reason

  12. 12

    I'm trying to Rename a File using Java But It is Not Working for Some Reason

  13. 13

    C# Delegates. Casting not working for some reason

  14. 14

    express js route is not working, getting a 404 for some reason

  15. 15

    gem install pg -v '0.17.1' not working for some reason

  16. 16

    PHP foreach stopped working after some time

  17. 17

    Comboboxes are linked for some reason

  18. 18

    Replace ' with \' for some reason puts \\'

  19. 19

    On click isn't working for some reason .. where did I go wrong?

  20. 20

    Installed libinput-gestures to have mac-like gestures but it is not working for some reason

  21. 21

    Instantiation not working for a threading reason

  22. 22

    Component sharing variable for some reason

  23. 23

    onfling() not being called for some reason

  24. 24

    onfling() not being called for some reason

  25. 25

    iTextSharp for some reason is not writing to MemoryStream

  26. 26

    Text is always bold for some reason

  27. 27

    Simple program crashes for some reason

  28. 28

    Python asyncio stuck for some reason

  29. 29

    Mongoose model not saving for some reason

HotTag

Archive