Jump to content

Loop The Loop


Recommended Posts

I haven't done a tutorial in a while, been really busy making a living.

 

But it's late and I've got time on my hands.

 

Let's talk about loops.

 

Loops are used in many programming languages to have the computer (server) go back to a prior portion of the code until some special trigger is... triggered.

 

There are lots of ways to do this in php

 

Here's a while loop:

$x=1;

while($x < 20)

{

print("$x<br>");

$x=$x+1;

}

Everything in the brackets is the loop. The print statement will execute each loop and then variable x will be increased by one. Once x is no longer less than 20, the loop will terminate and the script will move on. If the loop is improperly set up, the loop will go on forever (theoretically) and is called an infinite loop. These are bad. Luckily, the server is set to timeout after a while. But avoid writing infinite loops (if you can).

 

BAD (infinite loop)

while(1 = 1)

{

print("hello");

}

 

One will always equal one so there is no termination to the loop.

 

Another type of loop, and my preferred method, is the for loop.

 

for($x=1; $ <20; $x++)

{

print("$x<br>");

}

 

This does exactly the same thing as the other loop. It will print out a sequence of numbers up until x is no longer less than 20.

1

2

3

4

etc...

 

A couple of things to note before moving on to useful applications:

>if you don't declare the variable ($x=1 or whatever) then it's assumed to be zero unless an earlier part of your script assigned a number to the variable. When in doubt, declare the variable... even though it's not required.

>writing $x++ is shorthand for $x=$x+1;

>the for loop has the start, termination, and increments separated by semicolons not commas

 

On to useful...er stuff

Link to comment
Share on other sites

Table tricks with loops. Obviously I can't cover all the infinite varieties of uses here... but let's see if I can't give you something to play with.

 

What if you want to create a table with alternating row colors, but you don't know how long the table will be... why not? Well you might be pulling an unknown amount of data from a database.

 

So here we go... remember, you can start off a php page as html and then jump into php at any time.

 

We'll start with html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<title>Untitled Document</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>

 

<body>

Then we'll start our table... still in html (but can be done with php too)

 

<table width="100%" border="0" cellspacing="2" cellpadding="0">

 

Now php...

 

<?php

for($x=1; $x<10; $x++)

{

That's the start of our loop. Now for something new

 

if($x%2 == 0)

{

$bg = "#0000FF";

}

else

{

$bg = "#FFFFFF";

}

 

The whole bit with $x%2 gives us (the server actually) the remainder of dividing $x by 2. So 4%2 = 0 (no remainder) and 5%2 = 1 (1 left over after division). This has the effect of generating either a one or zero every time through the loop. And it alternates, which is very convenient.

 

The if statement read like "If this is true... do what's in between first { } otherwise either move on or do what's inside the else { } brackets."

 

So, we are effectively alternating the value of $bg... which will be our background color.

 

print("<tr bgcolor=\"$bg\"><td >$x</td></tr>");

}

?>

</table>

</body>

</html>

 

Okay. The home stretch. The rest of the code is a simple print statement. $bg is our alternating background color. The \ before the quotes tells the server "Not done yet. The quotation mark is actually part of the print statement... not a signal that the print statement is over."

 

A simple, one column table with alternating row colors.

Edited by surefire
Link to comment
Share on other sites

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.   Paste as plain text instead

  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...