Jump to content

  •  

  • iBotModz CBox


    Photo

    Beginning PHP - Lesson 9 - Loops and POST


    This topic has been archived. This means that you cannot reply to this topic.
    No replies to this topic

    #1 fattwam

    fattwam

      General Grade 2

    • Sub-Admins

    • 3,979 posts
    • Joined: 18-August 07

    Posted 27 March 2008 - 08:17 PM

    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 "
    "
    .""
    ."
    ";
    ?>



    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 "
    "
    .""
    ."
    ";
    }


    elseif (isset($_POST['page'])) {
    if ($_POST['page'] < 5) {
    $page_num = $_POST['page'];
    $page_num++;
    echo "
    "
    .""
    ."
    ";
    }
    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:






     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 "
    "
    .""
    ."
    ";
    /* 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 "
    "
    .""
    ."
    ";
    /* 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.