Code2Design.com

User login

The Layout

Programming

Graphic Design

Resources

Navigation

C2D Projects

Unsystematic Affiliates

ITexperts Central GameXe Flash Valley Capital Tutorials 

Change Language

Who's online

There are currently 0 users and 7 guests online.

Lesson 9 - Loops and POST

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:

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

$page_num++; 
   
echo 
"<form method=\"post\">"
   
."<input type=\"Submit\" name=\"page\" value=\"$page_num\">"
   
."</form>";
?>

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
if (!isset($_POST['page'])) {
$page_num 0;
echo 
"<form method=\"post\">"
   
."<input type=\"Submit\" name=\"page\" value=\"$page_num\">"
   
."</form>";
}


elseif (isset(
$_POST['page'])) { 
    if (
$_POST['page'] < 5) {
       
$page_num $_POST['page'];
       
$page_num++;
        echo 
"<form method=\"post\">"
           
."<input type=\"Submit\" name=\"page\" value=\"$page_num\">"
           
."</form>";
    }
    elseif (
$_POST['page'] == 5) {
        echo 
"You made it to page 5!";
    }
}

?>

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
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 "<form method=\"post\">"
   
."<input type=\"Submit\" name=\"page\" value=\"$page_num\">"
   
."</form>";
/* 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.

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")*/
}




elseif (isset(
$_POST['page'])) { 
/* if something named "page" was posted do this: (AKA 2nd to last visit)*/

   
if ($_POST['page'] < 5) { 
   
/* if the valued of "page" is less than five do this: (aka 2nd to 5th visit)*/
   
       
$page_num $_POST['page']; 
       
// put the posted value into a var named $page_num
       
       
$page_num++;
       
// increase $page_num by one
       
       
echo "<form method=\"post\">"
           
."<input type=\"Submit\" name=\"page\" value=\"$page_num\">"
           
."</form>";
       
/* print the form with the value of $page_num now increased by one */
   
}
    elseif (
$_POST['page'] == 5) {
   
/* if the valued of "page" is equal to five do this statement. (This is the last page you see (aka page 6)) */
   
       
echo "You made it to page 5!";
    }
}

?>

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.


Submitted by David on November 9, 2006 - 6:10pm.
printer friendly version

error

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

$page_num++; 
   
echo 
"<form method=\"post\">"
   
."<input type=\"Submit\" name=\"page\" value=\"$page_num\">"
   
."</form>";
?>

When I typed this in and ran it from the browser it said:

Parse error: parse error, unexpected T_ECHO in C:\wamp\www\learningphpfinally\simplepost.php on line 11

What is wrong here I checked it over and over and don't see what I have done wrong unless it is wrong from above.


It is right

I just ran the code and it works fine...

So since the error is from "line 11" you could try changing the code to this:

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

$page_num++; 
 
//Move the "."
echo "<form method=\"post\">".
   
"<input type=\"Submit\" name=\"page\" value=\"$page_num\">".
   
"</form>";
?>

However, I would go over the code very carefully and make sure there are no lose " " (spaces) in your code.


New Movies

will you be makeing more movies they are great im not the best at learning by reading this has tought me alot allready thanks


Nice

Hope you are thinking about making more videos, they are much easier to understand than reading this, which is fine by me though but much easier with videos. Good job man.

Regards,
iBye


Great Great Work!

i have to learn PHP coding up to.. tommorrow, you are a true life saver, your lessons has taken me 20 huge steps forward, the only thing i will be getting tough work with is the fact there are no more video lessons in lesson 9 and on.

but again.. a HUGE thanks!

Moshe.


Thanks

And yes I am planing on making more videos in the near future. However, I am currently working on a big OS PHP project that will be the worlds largest living tutorial (seriously) - so you'll have to wait a little longer for more videos. ;)


Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <br> <br /> <h3>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use BBCode tags in the text, URLs will be automatically converted to links
More information about formatting options



Like what you see?

Why not add more? C2D is looking for other Christian Web Masters who would like to help write articles for this site. If you have expericance in FLASH, CSS/HTML, PHP/MySQL, PhotoShop/GIMP, Blender, Javascript, or just General Design - our users would love to hear what you have to say. Contact Us

delicious   digg   reddit   magnoliacom   newsvine   furl   google   yahoo   technorati