Jump to content

cbox


Beginning PHP - Lesson 9 - Loops and POST


fattwam

Recommended Posts

Posted

Using Control Structures and Superglobals together

 

 

Today we are going to make a page that is TOTALY dynamic and will only display something after a condition is met. But first I want to do a small warn-up exercise to get you thinking in loops! lol. Open up your text editor and type the following PHP code into it:

 

 

 

 if (!isset($_POST['page'])) {
   $page_num = 0;
   }
if (isset($_POST['page'])) {
   $page_num = $_POST['page'];
   }

$page_num++;  
   
echo "</pre><form method="post">"
   .""
   ."</form>";<br

 

 

Save the file as "simplepost.php", upload it to your web server, and then run it. you will see a button that increases by "1" every time you click it! Now lets go over the PHP code. The first "if" statement tests to see if you have NOT pasted a variable to the PHP file. If "TRUE", you have NOT passed a variable called "page" to the PHP script it will make a variable called "$page_num" and set it equal to "0".

 

This second "if" statement checks to see if you HAVE passed a variable to this file. If you have passed the variable "page" to this file - it makes a new variable called "$page_num" and sets it equal to the posted variable "page". ($page_num = $_POST['page']). Remember that because the first if statement is FALSE the variable ($page_num = 0)was NEVER MADE.

 

After we get the variable "$page_num", we increase it by "1" ($page_num++) and stick it into our HTML form that posts back to this PHP file!

 

 

 

 

Well, that was pretty easy; lets try something a little harder. Such as a file that is a little smarter as to what it outputs and when. Open up your text editor and type the following code into it:

 

 

 

 

 if (!isset($_POST['page'])) {
$page_num = 0;
echo "</pre><form method="post">"
   .""
   ."</form>";<br>}<br><br><br>elseif (isset($_POST['page'])) { <br>   if ($_POST['page'] < 5) {<br>       $page_num = $_POST['page'];<br>       $page_num++;<br>       echo "<form method="post">"
           .""
           ."</form>";<br>   }<br>   elseif ($_POST['page'] == 5) {<br>       echo "You made it to page 5!";<br>   }<br>}<br><br

 

 

 

Save it as "postloop.php" (with the quotes) and upload it to your web server and run it. You will see a button that has a number in it that starts at "0" and goes up by one each time you click it until you get to the number "5" at which point you see the message: "You made it to page 5!". Now here is the PHP code with a description of each part:

 

 

 

 

 

 

 if (!isset($_POST['page'])) { 
/* IF there was nothing posted to the page (aka. first visit) do this: */

$page_num = 0; 
//make a variable called $page_num and set it to ZERO 

echo "</pre><form method="post">"
   .""
   ."</form>";<br>/* make a HTML form that "POSTS" the value of "$page_num" to itsself . Notice that the name of the "input" button in the form is "page" and the value is the value of $page_num.<br><br>If you look at the top "if" statment, it says "if $_POST['page'] is NOT set" - that is the same name as the "input" button (name="page"). So next time this file runs it will ignore this "if" statment because it will be FALSE. (Something will be posted named "page")*/<br>}<br><br><br><br><br>elseif (isset($_POST['page'])) { <br>/* if something named "page" was posted do this: (AKA 2nd to last visit)*/<br><br>   if ($_POST['page'] < 5) { <br>   /* if the valued of "page" is less than five do this: (aka 2nd to 5th visit)*/<br>   <br>       $page_num = $_POST['page']; <br>       // put the posted value into a var named $page_num<br>       <br>       $page_num++;<br>       // increase $page_num by one<br>       <br>       echo "<form method="post">"
           .""
           ."</form>";<br>       /* print the form with the value of $page_num now increased by one */<br>   }<br>   elseif ($_POST['page'] == 5) {<br>   /* if the valued of "page" is equal to five do this statement. (This is the last page you see (aka page 6)) */<br>   <br>       echo "You made it to page 5!";<br>   }<br>}<br><br

 

 

 

As over whelming as this PHP code may seem, it really is easier than it looks if you don't let it scare you. There are only four "if/elseif" statements in this code and if you look at each one individually you should have no problem understanding what they do.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...