Adrienk Posted April 23, 2012 Share Posted April 23, 2012 So I have a folder with two images. What happens with the code on pastebin is that it goes through checks the directory, checks for files in the folder with the extension.jpg and then prints them out into a image tag. I understand the for loop is probably a no no in php and we should use for each, but I like this way better >_>. Aside from that, this whole this works, it goes through gets the images. Problem? It prints the first image twice and the second once. So: $this->get_images_in_dir[0] results in 2 images (the image printed twice) where as $this->get_images_in_dir[1] results in one. why? It should only print it once. like: $this->get_images_in_dir[0] results in image1.jpg $this->get_images_in_dir[1] results in image2.jpg instead of image1.jpg, image1.jpg, img2.jpg Ideas? Quote Link to comment https://forums.phpfreaks.com/topic/261484-prints-out-three-images-instead-of-two-for-loop/ Share on other sites More sharing options...
Adrienk Posted April 23, 2012 Author Share Posted April 23, 2012 There is no edit button? Any ways I just realized the link was broken for the pastebin.... So http://pastebin.ca/2139415 Any ways, OP still counts. you'd just have to uncomment out the echo. How ever I have come across an interesting problem...you see here how I am returning this all to an array? (essentially this function returns an array of all the images in said directory) Well if I use that method and walk through the returned array on the front end I get i instead of image1.jpg How ever if I just do an echo on this method I get back image1.jpg instead of image1.jpgimage2.jpg which is insanly odd....Im thinking its my loop. because if I echo out images as you see in the class then I get image1.jpgimage1.jpgimage2.jpg and if I return this function and walk through it I get i also why is there no edit button - or am I blind >.> (edit button for the OP) Quote Link to comment https://forums.phpfreaks.com/topic/261484-prints-out-three-images-instead-of-two-for-loop/#findComment-1339894 Share on other sites More sharing options...
batwimp Posted April 23, 2012 Share Posted April 23, 2012 Are there only two images in that directory? If so, add a third image, run your code, and tell us what happens. Quote Link to comment https://forums.phpfreaks.com/topic/261484-prints-out-three-images-instead-of-two-for-loop/#findComment-1339925 Share on other sites More sharing options...
nafetski Posted April 23, 2012 Share Posted April 23, 2012 A couple things... First, I really like using PHP's glob() function. Great tutorial on how it works can be found at http://net.tutsplus.com/tutorials/php/quick-tip-loop-through-folders-with-phps-glob/ It looks like you are counting prematurely. change while($file = readdir($handler)){ if($file != "." && $file != ".."){ $this->get_images_in_dir[] = $file; $count_images = count($this->get_images_in_dir); for($i = 0; $i<$count_images; $i++){ if(substr(strrchr($this->get_images_in_dir[$i],'.'),1)=="jpg"){ //echo "<img src='".get_template_directory_uri().'/images/headerimages/'.$this->get_images_in_dir[$i]."' width='100' height='100' />"; return $this->return_images_got = $this->get_images_in_dir[$i]; } } } } To while($file = readdir($handler)) { if($file != "." && $file != "..") { $this->get_images_in_dir[] = $file; } } $count_images = count($this->get_images_in_dir); for($i = 0; $i<$count_images; $i++) { if(substr(strrchr($this->get_images_in_dir[$i],'.'),1)=="jpg") { //echo "<img src='".get_template_directory_uri().'/images/headerimages/'.$this->get_images_in_dir[$i]."' width='100' height='100' />"; return $this->return_images_got = $this->get_images_in_dir[$i]; } } Quote Link to comment https://forums.phpfreaks.com/topic/261484-prints-out-three-images-instead-of-two-for-loop/#findComment-1339950 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.