Hello< i am having an issue with IF statements displaying the right data. Im selecting from my databse the userlevel an depending on the number access they have it will show verbage as opposed to the number. Easy enough a simple IF statment and bam. SO I test it with level 3 hey it works great let me test it with level 9, hey it showing me as level 3. Imnot sure waht I am missing but for some reason I cannot get this to work right. ANyone see what I am misisng? Thank you

<?PHP
//Checks user level and displays access level to website
$access = $session->username;

if ($access = 9)
    {
        $access =' Founder';
    }
if ($access = 8)
    {
        $access =' Admin';
    }
if ($access = 7)
    {
        $access =' Air Staff';
    }
if ($access = 6)
    {
        $access =' MAJCOM/CC';
    }
if ($access = 5)
    {
        $access =' Wing/CC';
    }
if ($access = 4)
    {
        $access =' Squadron/CC ';
    }
if ($access = 3)
    {
        $access =' Student ';
    }
if ($access = 2)
    {
        $access =' Pilot ';
    }

if ($access = 1)
    {
        $access =' CRAF Member';
    }


?>

Recommended Answers

All 2 Replies

3 things...

when using single = you are assigning that value to the variable...
To do a comparison you need to use == instead of =

if ($access = 9)    should   be   if ($access == 9)

I'm not sure why you would use the same variable for the comparison and the assignment, but maybe you have a good reason for that...

And the third thing is have you ever thought about using a switch / case as opposed to all of the if statements?

example:

switch ($access){
    case "9":
        $access2 =' Founder';
        break;
    case "8":
        $access2 =' Admin';
        break;
    case "7":
        $access2 =' Air Staff';
        break;
    case "6":
        $access2 =' MAJCOM/CC';
        break;
    case "5":
        $access2 =' Wing/CC';
        break;
    case "4":
        $access2 =' Squadron/CC ';
        break;
    case "3":
        $access2 =' Student ';
        break;
    case "2":
        $access2 =' Pilot ';
        break;
    case "1":
        $access2 =' CRAF Member';
        break;

    default:
    $access2 =' CRAF Member';
}

The default is so that if you don't have an initial value in the $access, then you will still always have a value in $access2

Hope that helps

Douglas

Great thank you, I did indeed try to use CASE structure and I also tried the == but got things mixed up and did it backwards. and yep i just messed up with the assignments. Seemsto be working as I new it was just something I was overlooking. Yet again nothing but postive stuff from Daniweb, Thanks you!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.