Lamez Posted August 18, 2008 Share Posted August 18, 2008 Is there a way where the user can input a number, and that is how many images come up? example: the user inputs 100 in a text field, then the script loads up 100 images. Now I am just using images for an example. How would I do that, a for loop, and if so can I get an example, I am not good with loops. Quote Link to comment Share on other sites More sharing options...
revraz Posted August 18, 2008 Share Posted August 18, 2008 http://www.tizag.com/phpT/forloop.php Quote Link to comment Share on other sites More sharing options...
Lamez Posted August 18, 2008 Author Share Posted August 18, 2008 That kinda helps. I still do not understand it. Quote Link to comment Share on other sites More sharing options...
revraz Posted August 18, 2008 Share Posted August 18, 2008 In the example in the link for ( $counter = 10; $counter <= 100; $counter += 10) you are doing the code in this loop: starting your counter at 10, up to 100, incrementing by 10 Just play with it and change it and see what it does. You have to try things to learn them. Quote Link to comment Share on other sites More sharing options...
Lamez Posted August 18, 2008 Author Share Posted August 18, 2008 ya I was messing around with the code after I said that. It makes a lot more sense after you explained it. Thanks! Quote Link to comment Share on other sites More sharing options...
Lamez Posted August 18, 2008 Author Share Posted August 18, 2008 ok so I have another question I use this: <?php for ( $counter = 99; $counter <= 100; $counter += 10){ echo "<BR>YA YA<BR>"; } ?> but I only got one result. Why? Quote Link to comment Share on other sites More sharing options...
lemmin Posted August 18, 2008 Share Posted August 18, 2008 That loop increments $counter until it is equal to 100 and since you start $counter out at 99, it only takes one increment to get to 100. If you want 100 loops, set $counter to 1 at first. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted August 18, 2008 Share Posted August 18, 2008 Because you're starting the loop at 99 and ending the loop when the counter is greater than 100 -- which occurs at the start of the second loop. Ken Quote Link to comment Share on other sites More sharing options...
Lamez Posted August 18, 2008 Author Share Posted August 18, 2008 so I am understanding it now. would this get me 100 results? <?php for ( $counter = 1; $counter >= 0; $counter += 0){ echo "<BR>YA YA<BR>"; echo $counter; } ?> Quote Link to comment Share on other sites More sharing options...
revraz Posted August 18, 2008 Share Posted August 18, 2008 for ( $counter = 1; $counter <= 100; $counter++) ^^^ Start # ^^^^^^ End # Quote Link to comment Share on other sites More sharing options...
revraz Posted August 18, 2008 Share Posted August 18, 2008 Maybe this is easier http://us3.php.net/for Quote Link to comment Share on other sites More sharing options...
Lamez Posted August 18, 2008 Author Share Posted August 18, 2008 I read it, but it all says the same thing. I am trying to learn ok I am getting tons of results I used this code: <?php $num = 0; for ( $counter = 1; $counter = 5; /*$counter += 0*/){ $num+=1; echo $num."<br>"; } ?> and I am getting thousands and thousands results, and it is not stopping. Here is a link: http://links.krazypicks.com/test.php Quote Link to comment Share on other sites More sharing options...
revraz Posted August 18, 2008 Share Posted August 18, 2008 for ( $counter = 1; $counter <= 5; $counter++){ Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted August 18, 2008 Share Posted August 18, 2008 The simplest way to see this is as the need to start some variable, then increment it till it reaches a certain point, then the loop stops.. for($i = 0; $i < 100; $i++){ // do something.. } So bascially, we setup a variable ($i) and set it to zero: $i = 0; next, the loop is setup to have a stopping point: $i < 100; so think of this as being, while $i is less than 100, do the last bit, and that last bit is: $i++ which is the same as saying $i = $i + 1. So, while $i is less than 100 (99 or less obviously), do what is inside the curly braces and increment it by 1 and continue with the loop. This is a very common setup and works well. So say you only need to loop 50 times, juts change the < 100 to < 50. Incase you were wondering why $i typically is set to 0, it's just a way to make the limit (in the above code snippet, 100) nice and tidy.. If I started $i at 1, and I needed 100 loops, the limit would have to be set to 101. so keeping things starting at 0 is... simpler.. (indexed arrays also work this way.. they start at 0 and work upwards). Hope that helped. Cheers, NRG Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted August 18, 2008 Share Posted August 18, 2008 There are 3 arguments to the for loop 1) start value 2) end condition 3) increment you made an infinite loop Your start value is ok -- 1 Your end condition is not a condition but an assignment statement which is always true You don't have an increment. Your example correctly written: <?php $num = 0; for ($counter =1; $counter <= 5; $counter++) { $num++; $echo $num. '<br>'; } ?> Ken Quote Link to comment Share on other sites More sharing options...
Lamez Posted August 19, 2008 Author Share Posted August 19, 2008 Thank you guys, I understand it very clear now! Quote Link to comment Share on other sites More sharing options...
Lamez Posted August 19, 2008 Author Share Posted August 19, 2008 Your example correctly written: <?php $num = 0; for ($counter =1; $counter <= 5; $counter++) { $num++; $echo $num. '<br>'; } ?> Ken Ken you set echo as a variable. We all make mistakes! Quote Link to comment Share on other sites More sharing options...
Andy-H Posted August 19, 2008 Share Posted August 19, 2008 $echo??? Lmao <?php $counter = (int)$_POST['counter']; for ($count = 1; $count < $counter; $count++){ echo number_format($count)." <br />"; } echo "Finished loop!!!"; ?> Quote Link to comment Share on other sites More sharing options...
Lamez Posted August 19, 2008 Author Share Posted August 19, 2008 I made a little script using a form, like I wanted: http://links.krazypicks.com/test.html Quote Link to comment Share on other sites More sharing options...
Andy-H Posted August 19, 2008 Share Posted August 19, 2008 Input: 7 Output: Fatal error: Call to undefined function getFileExtension() in /mounted-storage/home48c/sub007/sc33591-LWQU/link_check/upload.php on line 26 Quote Link to comment 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.