开发者

PHP multiple if...elseif statement

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 can't use if(condition1 && condition2 && condition3) because 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? I am actually getting the values such as indi_add, mass_add and exp开发者_如何学编程ort 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 every time the access level changes. I'm hoping to find a way where I can execute the if conditions whenever they are true.


You say "it has multiple values". What has? because this block:

if ($permre['pages'] == indi_add)// {echo "show page";}
if ($permre['pages'] == mass_add)// {echo "show page";}
if ($permre['pages'] == export)// {echo "show page";}

Will have only 1 hit. The var. $permre['pages'] can have only one value. A different one each time you run the script, true, but per run it can't have "indi_add" AND "mass_add" at the same time.

Also: wat is indi_add. Shouldn't that be "indi_add", if its a string or $indi_add if it's a variable?


I would use a switch instead of all the if statments

switch ($i) {
    case 0:
        echo "i equals 0";
        break;
    case 1:
        echo "i equals 1";
        break;
    case 2:
        echo "i equals 2";
        break;
}


I'd go for something like this:

switch($access) {
  case 2:
    switch($premre['pages']) {
      case "indi_add":
      case "mass_add":
      case "export":
      echo "show page";
      break;
    }
    break;
  default:
      echo "not show page";
      break;
}

If you need something individual to happen for each of the statements just add it to their case, then break it. Same goes with default, if you need something specific to happen just replace it with a similar case as the first one.

Edit: example with adding code to each case by request of OP

switch($access) {
  case 2:
    switch($premre['pages']) {
      case "indi_add":
        echo "something happens here when indi_add is run";
        doSomething();  //Runs a function for indi_add
        break;
      case "mass_add":
        echo "something happens here when mass_add is run";
        doSomethingElse();  //Runs another function for indi_add
        break;
      case "export":
        echo "show page for export";
      break;
    }
    break;
  default:
      echo "not show page";
      break;
}


Perhaps I misunderstand, but a single variable cannot have multiple values.

It can, however, contain multiple values if encoded in some way. In this particular case you're probably looking for bitmasks/flags.

First define the "values", making sure not to overlap bits patterns (unless this allowed in your system of values):

define('indi_add', 0x0001);
define('mass_add', 0x0002);
define('export', 0x0004);

Now you can combine these into a variable using bitwise OR operator |:

$permre['pages'] = indi_add | mass_add;

Finally, use bitwise AND operator & to check if the variable contains one of the flags:

if ($permre['pages'] & indi_add)// 
    {echo "show page";}

if ($permre['pages'] & mass_add)// 
    {echo "show page";}

if ($permre['pages'] & export)// 
    {echo "show page";}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜