Reputation: 321
I am building an app with CodeIgniter. I have a situation in which I need to display user submitted images for each trip. images get uploaded, but when its a turn to display them, my foreach won't show anything. Can you please help me cope this situation? here is my view:
<?php
if(count($photos) > 0) {
foreach($photos as $photo_strip){ ?>
<div>
<span class="title"><?= $photo_strip['trip']->title ?></span>
<span class="upload" data-id="<?= $photo_strip['trip']->id ?>">Upload Photos</span>
<div class="image-box">
<ul>
<?php
foreach($photo_strip['photos'] as $photo) { ?>
<li>
<img src="<?= $photo->image ?>" alt=""/>
</li>
<?php } ?>
</ul>
</div>
<?php } ?>
<?php } else { ?>
<div class="content">
<div class="no-data">
No Images Uploaded Yet
</div>
</div>
<?php } ?>
When I print_r($trip_section);
it shows me the whole array, but when I do the same print_r
or var_dump
inside the second foreach loop, it show nothing, that whole section is blank. Is there anything wrong I am doing? Is there any other way I can do this?
Thank you so much.
here is the var_dump for both arrays: $photos:
array(1) { [0]=> array(3) { ["trip"]=> object(stdClass)#52 (28) { ["id"]=> string(1) "2"
["title"]=> string(23) "My Beach Trip"
["image"]=> string(36) "5D2EEC96513F83C760A4EEF2DFF637CA.jpg"
["thumbnail"]=> string(36) "A4786CA32EE9D2E3FB05B7A58C577B68.jpg" ["type"]=> string(4) "trip"
["country"]=> string(1) "1" ["city"]=> string(1) "1" ["status"]=> string(1) "1"
["featured"]=> string(1) "0" ["venue"]=> string(34) "cleansing" ["description"]=> string(475) "my dicription"
["vacancy"]=> string(1) "7" ["minimum_attendee"]=> string(1) "2" ["map_link"]=> string(0) ""
["internal_status"]=> string(1) "2"
["price"]=> string(3) "105"
["slug"]=> string(23) "my-trip" ["start_time"]=> string(8) "00:00:00"
["end_time"]=> string(8) "00:00:00" ["timezone"]=> string(1) "0"
["trip_date"]=> string(19) "0000-00-00 00:00:00"
["create_date"]=> string(19) "2015-10-14 04:09:22" ["modify_date"]=> string(19) "2015-10-28 14:32:35" ["supplier_id"]=> string(1) "2" ["cancellation_policy"]=> string(2) "24" ["lat_long"]=> string(30) "(30.160477, 10.20730500000002)"
["countryName"]=> string(20) "My country"
["cityName"]=> string(5) "my city" }
["photos"]=> array(0) { }
["trip_date"]=> object(stdClass)#53 (5) { ["id"]=> string(2) "23"
["tid"]=> string(1) "2" ["start_time"]=> string(8) "15:00:00"
["end_time"]=> string(8) "19:30:00"
["trip_date"]=> string(19) "2015-10-22 00:00:00" } } }
array(3) { ["trip"]=> object(stdClass)#52 (28) {
["id"]=> string(1) "2" ["title"]=> string(23) "My trip"
["image"]=> string(36) "5D2EEC96513F83C760A4EEF2DFF637CA.jpg"
["thumbnail"]=> string(36) "A4786CA32EE9D2E3FB05B7A58C577B68.jpg"
["type"]=> string(4) "tour" ["country"]=> string(1) "1"
["city"]=> string(1) "1" ["status"]=> string(1) "1"
["featured"]=> string(1) "0" ["venue"]=> string(34) "cleansing"
["description"]=> string(475) "my description"
["vacancy"]=> string(1) "7" ["minimum_attendee"]=> string(1) "2"
["map_link"]=> string(0) "" ["internal_status"]=> string(1) "2"
["price"]=> string(3) "105" ["slug"]=> string(23) "my-trip"
["start_time"]=> string(8) "00:00:00"
["end_time"]=> string(8) "00:00:00" ["timezone"]=> string(1) "0"
["tour_date"]=> string(19) "0000-00-00 00:00:00"
["create_date"]=> string(19) "2015-10-14 04:09:22"
["modify_date"]=> string(19) "2015-10-28 14:32:35" ["supplier_id"]=> string(1) "2"
["cancellation_policy"]=> string(2) "24" ["lat_long"]=> string(30) "(30.160477, 10.20730500000002)"
["countryName"]=> string(20) "My country"
["cityName"]=> string(5) "my city" }
["photos"]=> array(0) { } ["tour_date"]=> object(stdClass)#53 (5) {
["id"]=> string(2) "23" ["tid"]=> string(1) "2"
["start_time"]=> string(8) "15:00:00" ["end_time"]=> string(8) "19:30:00"
["trip_date"]=> string(19) "2015-10-22 00:00:00" } }
Upvotes: 3
Views: 610
Reputation: 2566
Use this working code sample at http://phpfiddle.org/ to help debug your code. I suspect your array is not structured how you think. Withoutseeing the actual var_dump
for $photos
or $photo_strip
I can't really weigh in anymore. This has nothing to do with Code Igniter.
<?php
$photos = array(array("trip" => (object)array("title"=>"Beach", "id"=>1), "photos" => array((object)array("image"=>"http://placehold.it/350x150"))));
if(count($photos) > 0) {
foreach($photos as $photo_strip){ ?>
<div>
<span class="title"><?= $photo_strip['trip']->title ?></span>
<span class="upload" data-id="<?= $photo_strip['trip']->id ?>">Upload Photos</span>
<div class="image-box">
<ul>
<?php
foreach($photo_strip['photos'] as $photo) { ?>
<li>
<img src="<?= $photo->image ?>" alt=""/>
</li>
<?php } ?>
</ul>
</div>
<?php } ?>
<?php } else { ?>
<div class="content">
<div class="no-data">
No Images Uploaded Yet
</div>
</div>
<?php } ?>
EDIT:
Ok looking at your var_dump
note that ["photos"]=> array(0) { }
. Your photos array is empty. So basically foreach($photo_strip['photos'] as $photo)
is doing nothing because their are no photos. This code is working as intended; if you had photos they would be shown. Your problem is probably back in the controller or model where you add in the photos. To debug, manually add some photos to the photo array and see if it shows up in your view. After it does, then figure out how to correctly tie everything.
Upvotes: 1