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; THE
DECLARATION/INITIALIZATION : $someVariable = 0 THE
CONDITION: $someVariable < $loopLimit THE
INCREMENT: $someVariable++ |
|||||||||||||||||||||||
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 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:
THE DESIGN PROCESS 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. 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:
IN THE BODY section, have the following:
HOW TO MAKE THE 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. |