Welcome Guest, Not a member yet? Register   Sign In
foreach problem
#1

[eluser]tim1965[/eluser]
Hello. I have a view that loads the contents of an image directory, with images held within a specific directory based on an id. I create the path to the directory and run a get_filename to give me all the files in the directory. I then pass these to my view with the attached array
Quote:
array(2) { ["filename"]=> array(2) { [0]=> string(16) "XnN75WCkyzR6.gif" [1]=> string(16) "y5JSvVG5qQFr.gif" } ["path"]=> string(29) "./system/pictures/3/pictures/" }
Quote:
As you can see "filename" contains two files and "path" contains the path to the directory the files are in.

When i create my foreach loop i use the follwoing in my view
<?php
foreach($filename as $k => $v): { ?>
<li><img src="&lt;?php echo base_url().$path.$v; ?&gt;"></li>
&lt;?php } ?&gt;
&lt;?php endforeach; ?&gt;
So it should be looping through each iteration of $filename and then i create an img src for each element in $filename. I use $path and append the filename to pull back and display the image.

So my problem is that it is only displaying the first image and non of the others. The values are correct in my array but it doesnt seem to be executing the loop.
Any help appreciated.
#2

[eluser]srisa[/eluser]
I'm not sure if that is the problem, but remove the &lt;?php endforeach; ?&gt; line. You are mixing two kinds of syntax here.
#3

[eluser]umefarooq[/eluser]
hi your problem is solved dont put start and ending {} for the foreach loop if you are using either use this

[/code]
&lt;?php
foreach($filename as $k => $v): ?&gt;
<li><img src="&lt;?php echo base_url().$path.$v; ?&gt;"></li>
&lt;?php endforeach; ?&gt;
Code:
or use this
&lt;?php
foreach($filename as $k => $v) { ?&gt;
<li><img src="&lt;?php echo base_url().$path.$v; ?&gt;"></li>
&lt;?php } ?&gt;
[code]

every thing will be fine.
#4

[eluser]tim1965[/eluser]
Hi thanks for replying. I tried both the methods you recommended. These do correctly generate two pathhs and displays the path in the view. However if i then try to use these with either "img src" or ci "img", i am only generating 1 picture rather than the two that should be displayed. Any further help you could give would be much appreciated.
#5

[eluser]tim1965[/eluser]
Please excuse my stupidity. It is in fact working correctly. Its just the second image was displaying off the screen (ouch !!). Many thnks for your help.




Theme © iAndrew 2016 - Forum software by © MyBB