我正在尝试使用这些if / else if语句来显示这些php页面。 if / elseif语句允许显示php页面。数据存储在mysql中。我们如何得到它,如果它已经显示它只进入一次?谢谢。我希望你能提供帮助。对不起,这有点令人困惑。我最近刚学英语。 谢谢。
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";
}
答案 0 :(得分:2)
类似的东西:
$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.";
}
}
如果找不到页面,我不确定你想要做什么;或者后续记录是重复的。
答案 1 :(得分:1)
您需要使用==而不是= 我不知道声明的右边是什么,例如政治。如果它不是变量,那么你应该把它放在引号中。
像这样的东西
if ($result_array[0] == "Politics") {
require 'news/political.php';
}else ...
答案 2 :(得分:1)
看起来你正在迭代一系列选项,包括一堆设置文件?
我会尝试以下内容:
switch( TRUE )
{
case in_array("Politics", $result_array):
require 'news/political.php';
break;
case in_array("Gossip", $result_array):
require 'news/celebgossib';
break;
// etc.
}
答案 3 :(得分:0)
如何使用switch语句?
switch $result_array[0] {
case 'Politics': include('politics.php'); break;
case 'This': include(...); break;
case 'That': include(....); break;
default: include('default.php'); break;
}
对于使用简单“todo”部分的loooong if / then / else测试集,switch语句是理想的。