#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0

    Page modes and flow


    Can anyone tell what is the best practice for the flow of a page with multiple 'modes' that perfroms functions in the 'background' aswell as out putting data to the browser. As a rough example of what I'm talking about see the following two very rough code examples;

    if (isset($_GET[mode]))
    $mode = $_GET[mode];

    <?php
    if (isset($mode))
    {
    if ($mode == "add")
    {
    $result = $number1 + $number2;
    header('Location: page.php?mode=outputaddition');
    }
    else if ($mode == "subtract")
    {
    $result = $number1 - $number2;
    header('Location: page.php?mode=outputsubtraction');
    }
    }
    ?>

    <BODY>
    <?php if ($mode == "outputaddition") echo "You added two numbers and the result is: $result"; ?>
    <?php if ($mode == "outputsubtraction") echo "You subtracted two numbers and the result is: $result"; ?>
    </BODY>


    and also;

    if (isset($_GET[mode]))
    $mode = $_GET[mode];

    <?php
    if (isset($mode))
    {
    if ($mode == "add")
    {
    $result = $number1 + $number2;
    $mode = "outputaddition";
    }
    else if ($mode == "subtract")
    {
    $result = $number1 - $number2;
    $mode = "outputsubtraction";
    }
    }
    ?>

    <BODY>
    <?php if ($mode == "outputaddition") echo "You added two numbers and the result is: $result"; ?>
    <?php if ($mode == "outputsubtraction") echo "You subtracted two numbers and the result is: $result"; ?>
    </BODY>


    Is it better to change the page mode mid script leaving the old mode in the URL and let it run on down the script or to refresh the page with the new mode included in the URL so it bypasses everything except what it's supposed to.
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,663
    Rep Power
    1945
    When you post code, please use the code tags.

    First, all php code should be placed inside the <?php ?> tag

    In this case, it looks like you could omit the header call and instead create an output variable:
    PHP Code:
    <?php
    $output 
    "no mode selected or mode is unknown";
    if (isset(
    $_GET[mode])) {

        
    $mode $_GET[mode];

        if (isset(
    $mode))
        {
            if (
    $mode == "add")
            {
                
    $result $number1 $number2;
                
    $output "You added two numbers and the result is: $result"
            }
            else if (
    $mode == "subtract")
            {
                
    $result $number1 $number2;
                
    $output "You subtracted two numbers and the result is: $result"
            }
        }
    }

    ?>

    <BODY>
    <?php echo $output?>
    </BODY>
    [EDIT]
    I had seen the two example as the same.
    My example is similar to the second example.
    Last edited by MrFujin; April 8th, 2013 at 07:09 AM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0
    When you post code, please use the code tags.
    Yeah sorry about that.

    it looks like you could omit the header call and instead create an output variable
    That was a very basic example of the out put I'd be working with. In reality one output could be a full page of text or it could be a HTML form. I suppose I could have two URLs pointing to different include files and depending on the first mode, have the output variable set to the appropriate URL and statement which echos an include for whatever file URL output is set to.

    I was just wondering is it the done thing to let the script flow down like that or use redirects. Maybe its just up to the individual.
  6. #4
  7. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,663
    Rep Power
    1945
    Originally Posted by lunacyfoundme
    That was a very basic example of the out put I'd be working with. In reality one output could be a full page of text or it could be a HTML form. I suppose I could have two URLs pointing to different include files and depending on the first mode, have the output variable set to the appropriate URL and statement which echos an include for whatever file URL output is set to.

    I was just wondering is it the done thing to let the script flow down like that or use redirects. Maybe its just up to the individual.
    read my EDIT text.

    You are correct that there is no directly correct or wrong method. You have to look at the specific needs and then find the method/solution that fits best to that case.

    Take notice, that when you call header, the value in $result is lost if you don't forward it in the URL or save it in the SESSION register.

IMN logo majestic logo threadwatch logo seochat tools logo