将相同的选项标签分配给多个选择标签的更有效方法


more efficient way of assigning same option tags to multiple select tags

目前我有多个具有几乎相同选项选项的选择标签,我现在所做的file_get_contents用于获取选项标签的值并将它们分配给选择标签。 但是我想创建一种更有效的方法,因为当我尝试在文件中为file_get_contents添加一个 if 条件以过滤掉一些应该只可用于某些选定的标签,它不起作用。所以我想知道最好的方法是什么?

基本上,我使用file_get_contents来获取包含基本选项标签的文件,并将它们分配给选择标签。

谢谢-魔术师

某些代码:

一些.php

<option>1</option>
<option>2</option>
... you get the picture

对于其他文件.php

<select>
$hello = file_get_contents("some.php");
echo $hello;
</select>

现在,当我在某些地方添加一个条件时.php即

如果($right=1){这仅适用于此页面}

/

/这不起作用,而是将其显示给所有选择标签。

在我看来

,file_get_contents用于从静态文件(如txt文件)中检索数据,将其存储到字符串中并进行进一步处理。

为了实现您要执行的操作,您需要使用 include 指令,因为您显然希望第一页中的变量可用于第二页中的 if 语句

正如手册所述,"当包含文件时,它包含的代码将继承包含发生的行的变量范围。调用文件中该行的任何可用变量都将在被调用文件中可用"

您需要告诉生成选项的文件您想要哪组选项,为此,请使用查询字符串。

所以,首先要有这个:

<!-- basic set of options -->
<select>
 $hello = file_get_contents("some.php?o=basic");
 echo $hello;
</select>
<!-- advanced set of options -->
<select>
 $hello = file_get_contents("some.php?o=advanced");
 echo $hello;
</select>
<!-- ... you get the picture ... -->

然后在 PHP 代码中检查此查询字符串变量,并根据它输出正确的选项:

echo "<option>1</option>"
echo "<option>2</option>"
if ($_GET['o']=="advanced")
{
    echo "<option>3</option>"
    echo "<option>4</option>"
    echo "<option>5</option>"
}
相关文章: