Previous --> Page 5 |This is Page 6 | Next --> Page 7 (not posted yet)

Using For Loops

The syntax for a for loop is the same as many programming languages:

$loopLimit = 10;
for($someVariable = 0; $someVariable < $loopLimit; $someVariable++)
{ /* lines of code */ }

THE DECLARATION/INITIALIZATION : $someVariable = 0
This is where you create a variable and give it a starting value.
It happens BEFORE the loop is entered.

THE CONDITION: $someVariable < $loopLimit
This is evaluated as a boolean (true/false). If the condition is true, loop execution begins/continues.
It happens BEFORE the loop is entered (and then again after each loop.)

THE INCREMENT: $someVariable++
This code changes the value of the looping variable. It can go up or down and by any amount.
++ means to add one, -- means to subtract one.
You could also explicitly write $someVariable = $someVariable + 1;
A shorter way to update a variable is $someVariable += 5; (Meaning add 5 to that variable.)
You could also use -=, *=, /=, and %= (remainder)
This happens AFTER the last line within the loop.

PUTTING IT TO PRACTICE - Copy these blocks of code and try them!

The code below will print 20 numbers, one to a line.

$stopNum = 20;
for($x = 1; $x <= $stopNum; $x++)
{
  echo "Line $x:<br>";
}

The code below will print x's on each line, with the number of x's on each line the same as the line number.

$stopNum = 20;
for($x = 1; $x <= $stopNum; $x++)
{
  echo "Line $x: ";
  for($j = 1; $j <= $x; $j++)
    echo "x";
  echo "<br>";
}

Note that if braces are not included with the for loop, the for loop will consider the IMMEDIATE line of code below it to be part of the loop.

ASSIGNMENT 6 - Dynamically Generating an HTML Table with a user determined number of rows and columns
php6.php

Your task is to create a form that asks the user for the number of rows and columns he/she would like in the table. Your program should then create the table, labeling each <td> starting with the second one in the first row, "Column #" (put the actual number) and labeling the first <td> in each row, "Row #" (put the actual number). Lastly, it should alternate each cell to resemble a checkerboard (either white or black background.)

A table with 4 rows and 5 columns would look like:

  Column 1 Column 2 Column 3 Column 4
Row 1        
Row 2        
Row 3        

THE DESIGN PROCESS
Up until now, we have created forms that call a different page for processing. It is typically more efficient and easier if the form is processed on the same page it exists. One reason is simply because of the page look - rather than coding the same layout twice, it only must be done once, and we can use variables based upon the form to determine what information to display.

Our goal is to have the FORM call the page it is on as the ACTION. One could always type the filename explicitly, for example in this case:

<FORM NAME='f1' METHOD='POST' ACTION='php6.php'> but if you change the filename, you need to also change it here. PHP allows us to do this dynamically.

PHP has certain SERVER variables that tell us helpful information. You can see a list of them here.

$_SERVER['PHP_SELF'] returns the name of the page, relative to the document root.
For this very page, it is: /classes/pfti/php/page6.php

Therefore, you would want to include ACTION='<?php echo $_SERVER['PHP_SELF']; ?>' as part of the FORM tag. It is also a good habit (especially with critical information) to include a HIDDEN field that will trigger if the form should be processed. It may look like this:

<INPUT TYPE='hidden' NAME='processPage' VALUE='yes'>

So how will all this work?

At the VERY top of the page, before you even have <HTML>, you should have the following:

<?php

// the IF statement below will determine if the form is ready for processing
$showResults = false; // this variable can be used in the BODY to toggle what appears
if(isset($_POST['processPage']) && $_POST['processPage'] == "yes")
{

  $showResults = true;
  $numRows = $_POST['numRows'];
  $numCols = $_POST['numCols'];

}

?>

<HTML>
<!-- Additional code below -->

IN THE BODY section, have the following:

<?php


if(!$showResults) // same as if($showResults == false) (display the form)
{
?>

<!-- Write the FORM in HTML here -->
<?php
}
else // display the table!
{
// HERE IS WHERE ALL THE MAGIC HAPPENS!
}

?>

HOW TO MAKE THE TABLE
Remember table basics: You need a TABLE tag, TR for each row and TD for each cell. BGCOLOR is an attribute of TD for its background color. To generate a grid, we need two nested for loops, one for each row, the other for each column. For the initial construction (before making the checkerboard appearance), there is not a lot involved. You may begin it as follows:

echo "<TABLE CELLPADDING='3' CELLSPACING='2' BORDER='1'>";
for($row = 0; $row < $numRows; $row++)
{
  echo "<TR>";
  for($col = 0; $col < $numCols; $col++)
  {
    echo "<TD>";
    // you will put some information here SOMETIMES
    echo "</TD>";
  }
  echo "</TR>";
}
echo "</TABLE>";	 

The only thing you need to do is come up with the IF statements for the inner for loop that will print the information needed (at the appropriate time.)

To make the checkerboard appearance, you need to edit the TD tag located in the inner for loop to toggle between WHITE and BLACK. You can create a variable here to help you determine which one to use.