francescaarchie Posted January 13, 2011 Share Posted January 13, 2011 Hi, This is a portion of the code I am working on: if($access == 2) { if ($permre['pages'] == indi_add)// {echo "show page";} if ($permre['pages'] == mass_add)// {echo "show page";} if ($permre['pages'] == export)// {echo "show page";} } elseif ($access == 0) { if ($permre['pages'] == indi_add)// {echo "dont show page";} if ($permre['pages'] == mass_add)// {echo "dont show page";} if ($permre['pages'] == export)// {echo "dont show page";}** } I am creating a page where when a condition such as $access ==2 is executed it has multiple values. Some the values include: indi_add, mass_add and export. Then I want to use these values to create 3 conditions as shown above. However, when executing this code, only the first if statement is being executed and the rest aren't. Is there a away where I can make php check the 3 if statements at once and do the action requested?... Adding on, I cant use if(condition1 && condition2 && condition3) cause there is a possibility that one of the conditions may not be satisfied... So how do I go about executing all the statements even if the first statement if true or false? Any valid suggestions will be a great help..Thank You.. Additional Notes: Sorry for the question being not very clear. I am actually getting the values such as indi_add, mass_add and export from phpmyadmin database. The query goes something like this: "select pages from access where access = 2". Due to the query above I get the page values which are the ones mentioned above. The echo page is just an example. In my actual coding, if the statement for access == 2 if true, the result is that a particular page will be activated. I am trying to do something like access control. Also, behind the scene, access level for each page can be changed by the admin. Therefore, the query above may bring different results everytime the access level changes...Hope this helps to make the question more clear...therefore, Im hoping to find a way where I can execute the if conditions whenever they r true.... Quote Link to comment https://forums.phpfreaks.com/topic/224289-php%E2%80%A6multiple-if%E2%80%A6elseif-statementurgent-help-needed%E2%80%A6d%E2%80%A6/ Share on other sites More sharing options...
denno020 Posted January 13, 2011 Share Posted January 13, 2011 Well for starters, only one of your if's are going to execute because you're checking the exact same array element against 3 different values. It can only be one, hence only one of those if statements will run. If you want the possibility of all 3 running, then you will need to save the flag to allow it to be shown in a different array element, if that makes sense. Do you understand what your code is actually saying now? Denno Quote Link to comment https://forums.phpfreaks.com/topic/224289-php%E2%80%A6multiple-if%E2%80%A6elseif-statementurgent-help-needed%E2%80%A6d%E2%80%A6/#findComment-1158840 Share on other sites More sharing options...
francescaarchie Posted January 13, 2011 Author Share Posted January 13, 2011 well...i did observe that only the first if statement is executing. May I know what you mean by saving the flag to allow it to be shown in different array elements?...Does it mean to create an array list? Quote Link to comment https://forums.phpfreaks.com/topic/224289-php%E2%80%A6multiple-if%E2%80%A6elseif-statementurgent-help-needed%E2%80%A6d%E2%80%A6/#findComment-1158856 Share on other sites More sharing options...
denno020 Posted January 13, 2011 Share Posted January 13, 2011 Flag was a bad term.. What you should do is have two more array elements names pages2 and pages 3 (and rename the first to pages1 for consistency). You will then have: if ($permre['pages1'] == indi_add)// {echo "show page";} if ($permre['pages2'] == mass_add)// {echo "show page";} if ($permre['pages3'] == export)// {echo "show page";} Although on second thought, looking at that, you could just make them a boolean in your table, and check to see if it's a 1, in which case you would show the page... Example: if ($permre['indi_add'] == 1)// {echo "show page";} if ($permre['mass_add'])// {echo "show page";} if ($permre['export'])// {echo "show page";} First thing you will notice is that in the second and third iff statements I've removed the '== 1'. If the result is only going to be a 1 or 0 (which is how you will set up your table), then if, for example, $permre['export'] == 0, then the if statement will read as: if(0){ echo "show page"; } A 0 in the condition means false, so the code will not execute. It's up to you how you want to code it, you can leave the '== 1' in there so it's obvious to anyone reading your code exactly what you're doing. So what you need to do with your table is to add 3 columns, with type ENUM, and options of '0','1'. If I've missed the mark with what you actually want, let me know, otherwise I hope that helps you . Denno Quote Link to comment https://forums.phpfreaks.com/topic/224289-php%E2%80%A6multiple-if%E2%80%A6elseif-statementurgent-help-needed%E2%80%A6d%E2%80%A6/#findComment-1158917 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.