PHP Question: How to fix these if/elseif statements

前端 未结 4 879
别跟我提以往
别跟我提以往 2021-01-23 16:30

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

相关标签:
4条回答
  • 2021-01-23 17:03

    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 ...
    
    0 讨论(0)
  • 2021-01-23 17:06

    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.

    0 讨论(0)
  • 2021-01-23 17:15

    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 讨论(0)
  • 2021-01-23 17:18

    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.
    }
    
    0 讨论(0)
提交回复
热议问题