开发者

PHP Question: How to fix these if/elseif statements

I am trying to use these if/else if statements to display these php pages. The if/elseif statements allow for the php page to show up. The data is stored in the mysql. How do we get it so that if its already being displayed it only enters once? Thank you. I hope you can help. Sorry this is a little confusing. I开发者_开发百科 just learned English recently. Thank you.

if ($result_array[0] = Politics) {
        require 'news/political.php';
    } elseif ($result_array[0] = Gossip) {
        require 'news/celebgossib';
    }   elseif ($result_array[0] = Entertainment) {
            require 'news/entertainment.php';
        }   elseif ($result_array[0] = Finance) {
                require 'news/finance.php';

            }   elseif ($result_array[0] = Health) {
                    require 'news/health.php';
                }   elseif ($result_array[0] = Leisure) {
                        require 'news/leisure.php';
                    }   elseif ($result_array[0] = Sports) {
                            require 'news/sports.php';
                        }   elseif ($result_array[0] = Tech) {
                                require 'news/tech.php';
                            }   elseif ($result_array[0] = World) {
                                    require 'news/world.php';
                                } else {
                                    echo "There is no interests in your database";
                                }

                                if ($result_array[1] = Politics) {
                                    require 'news/political.php';
                                } elseif ($result_array[1] = Gossip) {
                                    require 'news/celebgossib';
                                }   elseif ($result_array[1] = Entertainment) {
                                        require 'news/entertainment.php';
                                    }   elseif ($result_array[1] = Finance) {
                                            require 'news/finance.php';

                                        }   elseif ($result_array[1] = Health) {
                                                require 'news/health.php';
                                            }   elseif ($result_array[1] = Leisure) {
                                                    require 'news/leisure.php';
                                                }   elseif ($result_array[1] = Sports) {
                                                        require 'news/sports.php';
                                                    }   elseif ($result_array[1] = Tech) {
                                                            require 'news/tech.php';
                                                        }   elseif ($result_array[1] = World) {
                                                                require 'news/world.php';
                                                            } else {
                                                                echo "There is no interests in your database";
                                                            }


Something like:

$pages = array(
  'Politics' => 'political',
  'Gossip' => 'celebgossib',
   ...
);

$used = array();

for ($i = 0; $i < 2; ++$i)
{
   if (array_key_exists($result_array[$i], $pages)
   {
      if (!array_key_exists($result_array[$i], $used))
      {
         # only display this section once
         include 'news/'.$pages[$result_array[$i]].'.php';
         $used[$result_array[$i]] = true;
      }
   }
   else
   {
      echo "Nothing to see here.";
   }
}

I'm not sure exactly what you want to do if the page isn't found; or if subsequent records are duplicates.


You need to use == instead of = I don't know what the right side of the statement is, for example Politics. If its not a variable then you should put it in quotes.

Something like this

if ($result_array[0] == "Politics") {
        require 'news/political.php';
    }else ...


It looks like you're iterating over an array of options, and including a bunch of set files?

I would try something like the following:

switch( TRUE )
{
    case in_array("Politics", $result_array):
        require 'news/political.php';
        break; 

    case in_array("Gossip", $result_array):
        require 'news/celebgossib';
        break;

    // etc.
}


How about using a switch statement?

switch $result_array[0] {
     case 'Politics':     include('politics.php'); break;
     case 'This': include(...); break;
     case 'That': include(....); break;
     default: include('default.php'); break;
}

for a loooong set of if/then/else tests with simple "todo" sections, a switch statement is ideal.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜