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:
| PHP-Code: |
if (!isset($_POST['page'])) { |
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:
| PHP-Code: |
if (!isset($_POST['page'])) { |
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:
| PHP-Code: |
if (!isset($_POST['page'])) { |
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.
No comments:
Post a Comment