SUN
SUN

Reputation: 973

PHP managing IF Else condition

I have a website in which sessions gets created as per query string parse to the URL. I am reading session value and accordingly want to change header of the website. Things working fine with below code but it is not going inside elseif condition. If I try to print session value it gives me correct echo but condition is not working properly. It works with If and else but not going inside ElseIf

<?php
    $clientID = "";
    $cid = $_GET['ciid'];
    $storeTitle = "";
    $storeDLogo = "";
    $storeGDlogo = "";
    $storeGMlogo = "";
    if (isset($_GET['ciid'])) {
        session_start();
        $_SESSION["mycid"] = $cid;
        $clientID = $_SESSION["mycid"];
    }


    //for FIEO
    if (isset($_SESSION["mycid"]) == "14"){
        $storeTitle = "Federation of Indian Exports Organization BrandSTORE";
        $storeDLogo = "/images/hid/figo-14.jpg";
        $storecolor1 = "#02ADF2"; //applied in header background
        $storecolor2 = "#FF9304"; //applied in mini header background 
        $storeGDlogo = "/images/hid/gl-14.jpg";
        $storeGMlogo = "/images/hid/gl-m-14.jpg";
    } elseif (isset($_SESSION["mycid"]) == "7"){ 
        $storeTitle = "Jet Airways BrandSTORE";
        $storeDLogo = "/images/jetAirwaysLogo.jpg";
        $storecolor1 = "#000"; //applied in header background
        $storecolor2 = "#FF9304"; //applied in mini header background 
        $storeGDlogo = "/images/globaJLinkerLogo.jpg";
        $storeGMlogo = "/images/globaJLinkerLogo.jpg";
    } elseif (isset($_SESSION["mycid"]) == 8){
        $storeTitle = "Jet Airways BrandSTORE";
        $storeDLogo = "/images/jetAirwaysLogo.jpg";
        $storeGDlogo = "/images/globaJLinkerLogo.jpg";
        $storeGMlogo = "/images/globaJLinkerLogo.jpg";
    }elseif (isset($_SESSION["mycid"]) == 9){
        $storeTitle = "Jet Airways BrandSTORE";
        $storeDLogo = "/images/jetAirwaysLogo.jpg";
        $storeGDlogo = "/images/globaJLinkerLogo.jpg";
        $storeGMlogo = "/images/globaJLinkerLogo.jpg";
    } elseif (isset($_SESSION["mycid"]) == 10){
        $storeTitle = "Jet Airways BrandSTORE";
        $storeDLogo = "/images/jetAirwaysLogo.jpg";
        $storeGDlogo = "/images/globaJLinkerLogo.jpg";
        $storeGMlogo = "/images/globaJLinkerLogo.jpg";
    } else{
       $storeDLogo = "/images/jetAirwaysLogo.jpg";
       $storeGDlogo = "/images/globaJLinkerLogo.jpg";
       $storeGMlogo = "/images/globaJLinkerLogo.jpg";
    }
?>

Upvotes: 0

Views: 22

Answers (2)

M.Hemant
M.Hemant

Reputation: 2355

try this,

if (isset($_SESSION["mycid"])) {
    switch ($_SESSION["mycid"]) {
        case 7:
            $storeTitle = "Jet Airways BrandSTORE";
            $storeDLogo = "/images/jetAirwaysLogo.jpg";
            $storecolor1 = "#000"; //applied in header background
            $storecolor2 = "#FF9304"; //applied in mini header background 
            $storeGDlogo = "/images/globaJLinkerLogo.jpg";
            $storeGMlogo = "/images/globaJLinkerLogo.jpg";
            break;
        case 8:
            $storeTitle = "Jet Airways BrandSTORE";
            $storeDLogo = "/images/jetAirwaysLogo.jpg";
            $storeGDlogo = "/images/globaJLinkerLogo.jpg";
            $storeGMlogo = "/images/globaJLinkerLogo.jpg";
            break;
        case 9:
            $storeTitle = "Jet Airways BrandSTORE";
            $storeDLogo = "/images/jetAirwaysLogo.jpg";
            $storeGDlogo = "/images/globaJLinkerLogo.jpg";
            $storeGMlogo = "/images/globaJLinkerLogo.jpg";
            break;
        case 10:
            $storeTitle = "Jet Airways BrandSTORE";
            $storeDLogo = "/images/jetAirwaysLogo.jpg";
            $storeGDlogo = "/images/globaJLinkerLogo.jpg";
            $storeGMlogo = "/images/globaJLinkerLogo.jpg";
            break;
        case 14:
            $storeTitle = "Federation of Indian Exports Organization BrandSTORE";
            $storeDLogo = "/images/hid/figo-14.jpg";
            $storecolor1 = "#02ADF2"; //applied in header background
            $storecolor2 = "#FF9304"; //applied in mini header background 
            $storeGDlogo = "/images/hid/gl-14.jpg";
            $storeGMlogo = "/images/hid/gl-m-14.jpg";
            break;
    }
} else {
    $storeDLogo = "/images/jetAirwaysLogo.jpg";
    $storeGDlogo = "/images/globaJLinkerLogo.jpg";
    $storeGMlogo = "/images/globaJLinkerLogo.jpg";
}

Here, Dont forget to pass default values, which variables are not set due to condition

Upvotes: 0

user4035
user4035

Reputation: 23719

You condition is incorrect, use conjunction:

if(isset($_SESSION["mycid"]) && $_SESSION["mycid"] == "14")
...
elseif(isset($_SESSION["mycid"]) && $_SESSION["mycid"] == "8")

More efficient would be to check isset in the outer if only once and then check values:

if(isset($_SESSION["mycid"]))
{
        if($_SESSION["mycid"] == "14")
        {
             ...    
        }
        elseif($_SESSION["mycid"] == "8")
        {
             ...
        }
        else
        {
             ...
        }
}
else
{
       //action for $_SESSION["mycid"] not set
}

Upvotes: 1

Related Questions