Welcome Guest, Not a member yet? Register   Sign In
friday afternoon question (i hope !!) foreach on view
#1

[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="&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.
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
#2

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

[eluser]GSV Sleeper Service[/eluser]
I've got a feeling this will fix it
Code:
&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;
#4

[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.
#5

[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:
&lt;?php
foreach($filename as $p):
var_dump($p);
endforeach;
?&gt;
#6

[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 ??




Theme © iAndrew 2016 - Forum software by © MyBB