Solved

PHP snippet change not working

Posted on 2011-03-03
4
231 Views
Last Modified: 2012-06-21
I am using the following PHP snippet to loop through a directory of images and display thumbnails.  The script is currently hard-coded to display up to 10 images.  I am changing the value where you see 10 to increase to 20 photos.  I figured this was a simple change, but even when I change all instances of 10 to 20 it STILL only shows 10 images?  What am I missing here?

									<?
									$image = "<br>";
									$img_cnt = 1;
									$image .= "<a href=/feeds/sancap/rets_images/$MLNumber.jpg rel=\"enlargeimage\" rev=\"targetdiv:loadarea\"><img src=/feeds/sancap/rets_images/$MLNumber.jpg alt='' width='100' height='75' border='0' /></a>&nbsp;";
									for ($c=1;$c<10;$c++) {
										if ($c<10)
											$c_ext = "0".$c;
										else
											$c_ext = $c;

										if (file_get_contents("http://www.mydomain.com/feeds/sancap/rets_images/{$MLNumber}_{$c_ext}.jpg"))
											$image .= "<a href=/feeds/sancap/rets_images/{$MLNumber}_{$c_ext}.jpg rel=\"enlargeimage\" rev=\"targetdiv:loadarea\"><img src=/feeds/sancap/rets_images/{$MLNumber}_{$c_ext}.jpg alt='' width='100' height='75' border='0' /></a>&nbsp;";
										else
											$c=12;

										$img_cnt++;
										if ($img_cnt == 5) {
											$image .= "<br>";
											$img_cnt = 0;
										}

									}

									?>   

Open in new window

0
Comment
Question by:pda4me
  • 2
4 Comments
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 35029054
This script, as it stands, will bomb out if it fails to find one image in the sequence so if you have missed named an image that could stop the script working.
0
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 250 total points
ID: 35029096
Also this line

else
     $c=12;

looks like it is meant to force a loop exit. It would be better to make this independent of the number of loop iterations by using break instead. Change it like so

else
     break;



0
 
LVL 4

Assisted Solution

by:Tomun
Tomun earned 250 total points
ID: 35036799
You only need to change the FIRST instance of 10 in the 'for' statement.

 if you changed the 'if' statement too it would have failed to find images numbered 10 or higher.

Do make bportlock's suggested change and use break.
0
 

Author Closing Comment

by:pda4me
ID: 35182673
Thanks!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question