• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
friday afternoon question (i hope !!) foreach on view

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);

$data['path'] = $path;
$data['filename'] = $filename;


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="&lt;?php echo $path.$p ?&gt;" />
&lt;?php endforeach; ?&gt;
&lt;?php echo form_close(); ?&gt;

So my problem is that if i echo $p i get the word "array" rather than the relevant filename.
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
array(2) { [0]=> string(13) "gIMGP1741.JPG" [1]=> string(12) "IMGP1741.JPG" }

But i am stumped if i can see where the problem is ???
Thanks in advance

[eluser]Phil Sturgeon[/eluser]
Dump $p too, what does PHP think is in it?

[eluser]GSV Sleeper Service[/eluser]
I've got a feeling this will fix it
&lt;?php foreach($filename as $k => $v): ?&gt;
<img src="&lt;?php echo $path.$v ?&gt;" />
&lt;?php endforeach; ?&gt;
&lt;?php echo form_close(); ?&gt;


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.

[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.
foreach($filename as $p):

Without trying to push my luck, can you see any reason why only the first of 7 pics in that array would only display ??

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.