PHP问题:如何修复这些if/elseif语句 [英] PHP Question: How to fix these if/elseif statements

查看:90
本文介绍了PHP问题:如何修复这些if/elseif语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用这些if/else if语句来显示这些php页面. if/elseif语句允许显示php页面.数据存储在mysql中.我们如何获得它,以便如果它已经显示过,它只能输入一次?谢谢你.希望您能提供帮助.抱歉,这有点令人困惑.我最近才学英语. 谢谢.

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";
                                                            }

推荐答案

类似的东西

$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.

这篇关于PHP问题:如何修复这些if/elseif语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆