将 PHP 变量输入 HTML 选择标签
我有一个 php 变量 ($list
),它是用逗号分隔的值列表。我试图弄清楚如何将此变量转换或输入到 HTML 选择标记中。这是我到目前为止所拥有的:
$dbquery = @$db->query('SELECT * FROM Company');
while ($queryText = $dbquery->fetchArray()){
$array[]=$queryText['Company_Name'];
}
//the next batch of code, which is not included, converts the $array[] into a variable named $list//
//the values for $list are: Company1, Company2, Company3, Company4...//
//HTML select tag//
echo '<select name="testSelect" id="testId">';
echo '<option value="'.$list.'">'.$list;
echo '</option></select>';
我知道我需要一个像“while”或“for”这样的循环来列出 $list 中的值,但我不确定确切的语法。有人可以帮助我吗?
谢谢,
东风公司
I have a php variable ($list
) that is a list of values separated by commas. I am trying to figure out how to convert or input this variable into a HTML select tag. Here is what I have so far:
$dbquery = @$db->query('SELECT * FROM Company');
while ($queryText = $dbquery->fetchArray()){
$array[]=$queryText['Company_Name'];
}
//the next batch of code, which is not included, converts the $array[] into a variable named $list//
//the values for $list are: Company1, Company2, Company3, Company4...//
//HTML select tag//
echo '<select name="testSelect" id="testId">';
echo '<option value="'.$list.'">'.$list;
echo '</option></select>';
I understand that I would need a loop like a "while" or a "for" to list the values within $list, but I am not sure of the exact syntax. Can anyone assist me with this?
Thanks,
DFM
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
每个列表项(公司名称)都需要一个
option
元素。You'll need an
option
element for every list item (company name).您需要
explode()
将列表转换为数组(如果您不能由于某种原因直接使用$array
)然后执行以下操作:You need to
explode()
the list into an array (if you can't use$array
directly for some reason) then do:为什么首先将数组转换为逗号分隔的列表?一旦你有了一个数组,你就可以
输出你的选项。如果由于某种原因确实需要使用逗号分隔的列表步骤,则始终可以使用explode(',', $list) 将其转换回数组。
Why are you converting the array into a comma separated list in the first place? Once you have an array you can do
to output your options. If you for some reason really need to have the comma separated list step, you can always use explode(',', $list) to convert it back to an array.
您可以使用类似
explode()
的方法将内容列表放入数组中,然后循环遍历该数组..类似:有关 PHP
explode()
函数的更多信息:< a href="http://www.php.net/manual/en/function.explode.php" rel="nofollow noreferrer">http://www.php.net/manual/en/function.explode.php
You could use something like
explode()
to put the list of stuff into an array, then loop through that.. something like:More on PHP
explode()
function:http://www.php.net/manual/en/function.explode.php
请参阅 implode()
您可以使用循环模拟 implode() 函数,字符串连接。
例如:
See implode()
You can simulate the implode() function with a loop and string concatenation.
For example: