Akira Dawson
Akira Dawson

Reputation: 1267

Order status logic in PHP

having some issues displaying the correct order status. What I want to achieve is to let the user know if their order is:

No order, Interested in making an order, Refunded, Dispatched, Payment Received, Free of Charge, Order Placed.

So far I have:

if($no_order){
    $status = "No Order";
}
else{
    if($interested){
        $status = "Interested";
    }
    if($refunded){
        $status = "Refunded";
    }
    etc..
}

My problem is I start to get issues when I start adding more if's inside the else statement. I have tried elseif but didn't have much luck. Is there an alternate solution to check the order status? instead of using if/else statement? Many thanks!

Upvotes: 0

Views: 1696

Answers (2)

erik258
erik258

Reputation: 16304

Putting your test conditions in an array is the cleanest way.
This code works for me. Here, I'm defining an array $ordered_statuses which can hold both the name of the variable to check and an object capable of performing the check and providing the appropriate text (if the check succeeds), in the order of precedence as you describe above. Then I iterate over it and set $status to the correect value.

Basically what we're doing here is using polymorphism to allow the behavior of an object ( specifically, checkValue) to vary while keeping a consistent interface. In fact, using a PHP interface for the base class would be applicable - but that is an exercise for the reader.

"Variable variable names" are a bit messy - probably there is a better way to do this, depending on the conaining code, but I think this does what you're looking for.

<?php
class statusCheck {
    function __construct($text){
        $this->text = $text;
    }
    // this can be overridden to 
    // provide different tests.
    function checkValue( $value ){
        return $value ? $this->text : false;   
    }
}
// here's an example of overriding for a more complicated check
class interestCheck extends statusCheck{
    function checkValue($value){
        // maybe interest has to be high enough?
        if( $value > 5 ) 
            return $this->text;
        else
            return false;
    }
}

$ordered_statuses = Array(
    "no_order" => new statusCheck("No Order"),
    "interested" => new interestCheck("Interested"),
    "refunded" =>  new statusCheck("Refunded" )
);
$interested = 7;
//$refunded = true; // should take precedence
$status = 'None';
foreach( $ordered_statuses as $status_name=>$test){
    $text = $test->checkValue( $$status_name); 
    if( $text != false ) 
        $status = $text;
}
echo "$status\n";

Upvotes: 2

Satish Sharma
Satish Sharma

Reputation: 9635

you can place them in a array like this

$ORDER_STATUS = array(
                    0 => "No Order",
                    1 => "Interested",
                    2 => "Refunded",
                    3 => "Payment Received",
                    4 => "Free of Charge",
                    5 => "Order Placed"

                 );

now for every status will be index of your array.

example 1

if $status=2 you can track it directly like $ORDER_STATUS[$status] which will be Refunded

example 2

if $status=5 you can track it directly like $ORDER_STATUS[$status] which will be Order Placed

Upvotes: -1

Related Questions