if, elseif, else

I'm building a photography site using the test server xamp and am trying to use an 'if, elseif, else' block of code to generate alternative navigations depending on login details....

The code is posted below but here is a brief 'in english' description of what I'm trying to do

-select all the data from tb_login (f_id, f_user, f_pass)
-if the person browsing is not a member / not logged in > print the visitors navigation
-if the person browsing logs in > print the members navigation
-if the id (f_id) of the person logging in is equal to 1 > pint the administrative navigation

At the moment when you first visit the site the visitors navigation loads.
When you login in with a members id details the members navigation loads.
When you login with the intended administrator login details the members navigation, not the administrative navigation, loads.

The select all query from the login table seems to working. A connection to the relivant database is made in the head section of the page already

I have a feeling there is something wrong with how I'm using the if functions and / or how I'm odering them but I don't have a clue what...

Replies To: if, elseif, else

Re: if, elseif, else

Well syntax is good assuming you actually successfully created $row. I don't see where you are actually creating that. You should have a statement like ...

$row = mysql_fetch_array($result);

... in there somewhere and I don't know if you just left it out or not. So assuming you did this part correctly, the only way that you would be skipping over the admin login is if the session variable was not set and $row[0] was not set to 1.

So before this if statement, print out the value of $row[0] and see what is actually in there. My guess is that it is either empty (because you didn't create the array correctly from the database results) or it is not the value you expect (the value 1).

I am focusing on the $row because you said that you get the members navigation when you login as an admin so that means that your session variable is in fact set, but that your $row[0] is not equal to 1. So check out the value of $row[0] and see what value it is.

If you continue to have problems let us know and also show us more of the code where you go from database result to $row variable. As well as the value of $row[0] prior to the if statement.

Re: if, elseif, else

just below the select query that is included in the snippet of code I posted above.

Now what happens is when either a members login details are used or the administrators login details are used, the admin navigation loads.

At the moment the login table only contains the details of the administrator and one member.
There are only four fields in the login table
f_id (which is an auto increment interger)
f_user (the users name)
f_pass (their password)
f_time
The administrator was the first record that I created in the login table so it is my understanding that when they login $row[0], the f_id field is equal to 1. When I browse the login table in phpMyAdmin the the number in the the administrators f_id field is 1

QUESTION 1
Should the admin navigation test require there to be a match to the admin password as well as a test to see if $row[0] is equal to 1. If so how would I add the first if test:

if (isset ($_SESSION['name'])&& $row[0] == 1)

The page structure is such that the login div is inside the bannerWrapper with the main div beneath.
I placed a print_r($row) statement in main div.

QUESTION 2
The results of the print_r() statement are making me begin to wonder if the navigation is behaving the way it is because there is something wrong with my session code. I've never used session code before so am very uncertain about it...
For some reason the session isn't dying properly - when I log out as administrator the navigation goes back to the visitors option but the print_r($row) still displays the administrators details in the main div.
When I proceed to login as a member the print_r($row) results are still the administrators.
When I close the site down and go back to it the administrators details are still displayed in the main div.

Re: if, elseif, else

Posted 02 March 2009 - 10:48 AM

the way I see it and I cld be wrong here is that if your var is equal to 1 or not equal to one ends your if statement right there either the one or the other which leaves your else hanging with nowhere to go, you need two absolute conditionals and an alternative

Re: if, elseif, else

I'm building a photography site using the test server xamp and am trying to use an 'if, elseif, else' block of code to generate alternative navigations depending on login details....

The code is posted below but here is a brief 'in english' description of what I'm trying to do

-select all the data from tb_login (f_id, f_user, f_pass)
-if the person browsing is not a member / not logged in > print the visitors navigation
-if the person browsing logs in > print the members navigation
-if the id (f_id) of the person logging in is equal to 1 > pint the administrative navigation

At the moment when you first visit the site the visitors navigation loads.
When you login in with a members id details the members navigation loads.
When you login with the intended administrator login details the members navigation, not the administrative navigation, loads.

The select all query from the login table seems to working. A connection to the relivant database is made in the head section of the page already

I have a feeling there is something wrong with how I'm using the if functions and / or how I'm odering them but I don't have a clue what...

Re: if, elseif, else

I'm building a photography site using the test server xamp and am trying to use an 'if, elseif, else' block of code to generate alternative navigations depending on login details....

The code is posted below but here is a brief 'in english' description of what I'm trying to do

-select all the data from tb_login (f_id, f_user, f_pass)
-if the person browsing is not a member / not logged in > print the visitors navigation
-if the person browsing logs in > print the members navigation
-if the id (f_id) of the person logging in is equal to 1 > pint the administrative navigation

At the moment when you first visit the site the visitors navigation loads.
When you login in with a members id details the members navigation loads.
When you login with the intended administrator login details the members navigation, not the administrative navigation, loads.

The select all query from the login table seems to working. A connection to the relivant database is made in the head section of the page already

I have a feeling there is something wrong with how I'm using the if functions and / or how I'm odering them but I don't have a clue what...

Now, I could be wrong, but shouldn't two statments in a if statement look like this:

if (isset ($_SESSION['name'])&& ($row[0] == 1)){

Am I right?

I think the logic is flawed if it's equal to 1 but not equal to 1 the second statement means it can be equal to anything other than one which means that the else statment is the same as the !== statement

And this is what this code is doing at the moment
- when not logged in OR when you log out, the visitors navigation kicks in.
- when you are logged in with the current admin details OR with a members login details the admin navigation kicks in.
- the members navigation DOESN'T ever kick in.

Kiwi2 said

Quote

the way I see it and I cld be wrong here is that if your var is equal to 1 or not equal to one ends your if statement right there either the one or the other which leaves your else hanging with nowhere to go, you need two absolute conditionals and an alternative...... I think the logic is flawed if it's equal to 1 but not equal to 1 the second statement means it can be equal to anything other than one which means that the else statment is the same as the !== statement

Maybe I still can't see the wood for the leaves but I was under the impression that I had given 2 absolute conditions and an alternative in the existing if, esleif, if structure, hence:

if the condition for the admin nav is that a session has started and the login details are those of the administrators only

elseif the condition for the members nav is that a session has started and the login details are those of any members but not the administrators

else the condition for the visitors nav is that a session has not started, the person is not logged in,

Has anyone got any more feed-back about why no distinction is currently being made between the admin login and members login?

Re: if, elseif, else

Posted 10 March 2009 - 09:22 AM

Hi all,

When in class last night I found out where my logic was wrong.....
Firstly I needed a WHERE clause in the select all query on the login table.
Secondly I needed to adjust the syntax in the elseif from !==1 to !=1

I know Kiwi2 suggested my logic was wrong earlier in this thread but I have to admit I'm not that strong yet on reading and understanding all of what php logic does and therefore how to rectify any missing bits..... This is also why I will wait until I'm certain on all the pages / includes the site will contain before I get to the bottom of no2pencil's suggested if else layout.