Jess McKenzie
Jess McKenzie

Reputation: 8385

foreach loop not showing data

I am having a strange issue with the output of data for my foreach loop. The loop is not showing the data for the $image->thumbname,$description or $id but the print_r($get_images); works fine

Print_r Output:

Array ( [0] => 
    Array ( [id] => 1 
            [description] => testing 
            [imagename] => test.jpg 
            [thumbname] => test_thumb.jpg 
) )

View:

<?php if(is_array($get_images)): ?>
    <? print_r($get_images); ?>
        <?php foreach($get_images as $image): ?>
        <img src ="<?=base_url()?>includes/uploads/gallery/thumbs/<?=$image->thumbname?>" alt="<?= $image->description?>"> <a href="deleteimage/<?=$image->id?>">Delete</a>
        <?php print_r($image); ?>
    <?php endforeach; ?>
<?php endif; ?>

Upvotes: 0

Views: 185

Answers (3)

pickypg
pickypg

Reputation: 22332

That should be $image["thumbname"] and not $image->thumbname.

Upvotes: 1

Till
Till

Reputation: 22408

You have an array from what I can tell:

$image['thumbname']

Upvotes: 1

Galen
Galen

Reputation: 30170

youre using -> to access the array contents youre supposed to use []

<?php if(is_array($get_images)): ?>
    <? print_r($get_images); ?>
        <?php foreach($get_images as $image): ?>
        <img src ="<?=base_url()?>includes/uploads/gallery/thumbs/<?= $image['thumbname'] ?>" alt="<?= $image['description'] ?>"> <a href="deleteimage/<?= $image['id'] ?>">Delete</a>
        <?php print_r($image); ?>
        <?php endforeach; ?>
<?php endif; ?>

Upvotes: 2

Related Questions