Jump to content

Prints out three images instead of two? - for loop?


Adrienk

Recommended Posts

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?

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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];
}
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.