Jump to content

ELSE vs TWO "if" statements


StevenOliver

Recommended Posts

If order is finalized, I want Customer to start over from the "very beginning" if they want to create another order.

The "very beginning" is the page that has the Submit Button.

Without exception, all of these versions work. Which one is the best? (and which one is the worst? ?)

1.)
if(isset($_POST["SubmitButton"])) {
$_SESSION["order_is_finalized"]=null;
}
if(isset($_SESSION["order_is_finalized"])){
include('order_page.php');
exit;
}

-vs-

2.)
if(isset($_SESSION["order_is_finalized"])){
if(isset($_POST["SubmitButton"])) {
$_SESSION["order_is_finalized"]=null;
} else {
include('order_page.php');
exit;
}
}

-vs-

3.)
if(isset($_POST["SubmitButton"])) {
$_SESSION["order_is_finalized"]=null;
} elseif (isset($_SESSION["order_is_finalized"])) {
include('order_page.php');
exit;
}

-vs-

4.)
if(isset($_POST["SubmitButton"])) {
$_SESSION["order_is_finalized"]=null;
} else {
if (isset($_SESSION["order_is_finalized"])) {
include('order_page.php');
exit;
}
}

Thank you!!

Edited by StevenOliver
Link to comment
Share on other sites

What is best is based on what you want your script to do.  That being said, I would probably start with the session conditional first.

if(isset($_SESSION["order_is_finalized"])){
    if(isset($_POST["SubmitButton"])) {
        $_SESSION["order_is_finalized"]=null;
    } else {
        include('order_page.php');
        exit;
    }
}


 

Link to comment
Share on other sites

6 minutes ago, NotionCommotion said:

What is best is based on what you want your script to do.  That being said, I would probably start with the session conditional first.

Thank you!! Sometimes it's hard to articulate exactly what I want... but here goes: If the order is finalized, I don't want customer hitting back button to create a new order or modify their order. Rather, I want them to start fresh from the beginning. 

The way I know that "order is finalized" I create $_SESSION["order_is_finalized"] and set it to TRUE. 

The way I know that Customer is "starting fresh from the beginning" is if they click the initial Start Button (  $_POST["SubmitButton"] ).

Since all 4 versions work, I can't seem to figure out which is the most efficient. They all seem to work equally well. Unless you have a better idea, I'll use the "session conditional" version that you suggested.

Link to comment
Share on other sites

Your not going to get the results you are looking for.  The server will not know whether the user submitted the form using the submit button or clicked the back button which caused it to be submitted.  There are most likely better ways to do this, but something like the following might work.
 

<?php
session_start();

function display($counter)
{
    $_SESSION["counter"]=$_SESSION["counter"]+1;
    //create your page and include $counter in a hidden input field.
}

if(!isset($_SESSION["counter"])) {
    $_SESSION["counter"]=0;
}
if(isset($_POST["SubmitButton"])) {
    if(isset($_POST["counter"])) {
        if($_POST["counter"]==$_SESSION["counter"]) {
            //Process your order            
        }
        else {
            display();
        }
    }
    else {
        echo('missing counter error');
    }
}
else {
    display();
}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.