Jump to content

[SOLVED] For Loop?


Lamez

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

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.