目前我有多个具有几乎相同选项选项的选择标签,我现在所做的是使用file_get_contents来获取选项标签的值并将它们分配给选择标签。但是我想创建一种更有效的方法来实现这一点,因为当我试图在文件中为file_get_contents添加一个if条件来过滤掉一些额外的选项标签,这些标签应该仅对某些选择标签可用时它不起作用。所以我想知道最好的办法是什么?
基本上我使用file_get_contents来获取包含基本选项标签的文件,并将它们分配给选择标签。
感谢 -magician
一些代码:
some.php
<option>1</option>
<option>2</option>
... you get the picture
用于其他files.php
<select>
$hello = file_get_contents("some.php");
echo $hello;
</select>
现在当我在some.php添加一个条件即
如果($右= 1) { 这仅适用于此页面 }
//这不起作用,而是将其显示给所有选择标签。
答案 0 :(得分:2)
在我看来,file_get_contents用于从静态文件(如txt文件)中检索数据,将其存储到字符串中并进行进一步处理。
要实现您要执行的操作,您需要使用include
指令,因为您显然希望第一页中的变量可用于第二页中的if
语句< / p>
正如手册所述,“当包含文件时,它包含的代码会继承包含发生的行的变量范围。调用文件中该行可用的任何变量都将在被调用文件中可用”
答案 1 :(得分:1)
您需要告诉文件生成选项您想要的选项集,并使用查询字符串。
所以,首先要有这个:
<!-- 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>"
}