friday afternoon question (i hope !!) foreach on view - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: friday afternoon question (i hope !!) foreach on view (/showthread.php?tid=14998) |
friday afternoon question (i hope !!) foreach on view - El Forum - 01-23-2009 [eluser]tim1965[/eluser] I am trying to display some uploaded images in a page. I have a controller that correctly pulls back the filenames and the correct path to the directory. I then pass these through to the view. relevant controller code $path = './system/pictures/'.$propid.'/'; $filename = get_filenames($path); var_dump($filename); $data['path'] = $path; $data['filename'] = $filename; $this->load->view('propreg/f_propreg4_imagegallery_v1',$data); I then have a simple view to display the path name and filename and i loop thru this to get all files in this directory. <?php foreach($filename as $p): ?> <img src="<?php echo $path.$p ?>" /> <?php endforeach; ?> <?php echo form_close(); ?> So my problem is that if i echo $p i get the word "array" rather than the relevant filename. Quote:./system/pictures/3/Array Quote:So i am obviously messing the foreach up somewhere and it is getting "array" from the $filename first five characters. If i dump the array for $filename i get Quote:array(2) { [0]=> string(13) "gIMGP1741.JPG" [1]=> string(12) "IMGP1741.JPG" } Quote: But i am stumped if i can see where the problem is ??? Thanks in advance friday afternoon question (i hope !!) foreach on view - El Forum - 01-23-2009 [eluser]Phil Sturgeon[/eluser] Dump $p too, what does PHP think is in it? friday afternoon question (i hope !!) foreach on view - El Forum - 01-23-2009 [eluser]GSV Sleeper Service[/eluser] I've got a feeling this will fix it Code: <?php foreach($filename as $k => $v): ?> friday afternoon question (i hope !!) foreach on view - El Forum - 01-23-2009 [eluser]tim1965[/eluser] GSV Thanks it is now picking up the filename correctly. If you have time could you explain what i was doing incorrectly. Thanks and have a great weekend. friday afternoon question (i hope !!) foreach on view - El Forum - 01-23-2009 [eluser]GSV Sleeper Service[/eluser] get_filenames() was returning an array of arrays. so your original foreach was walking through your array of arrays and plucking out a single array entry one at a time. each of those array entries contained a key (eg [0]) and a value (eg "gIMGP1741.JPG") I suggest you follow pyromaniacs advice and try the following to get a better idea of what was going on. Code: <?php friday afternoon question (i hope !!) foreach on view - El Forum - 01-23-2009 [eluser]tim1965[/eluser] Without trying to push my luck, can you see any reason why only the first of 7 pics in that array would only display ?? |