将 PHP 变量输入 HTML 选择标签

发布于 2024-08-13 20:59:33 字数 692 浏览 2 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

尘曦 2024-08-20 20:59:33

每个列表项(公司名称)都需要一个 option 元素。

<select name="testSelect" id="testId">
    <?php foreach ($array as $companyName): ?>
        <option value="<?php echo $companyName; ?>"><?php echo $companyName; ?></option>
    <?php endforeach; ?>
</select>

You'll need an option element for every list item (company name).

<select name="testSelect" id="testId">
    <?php foreach ($array as $companyName): ?>
        <option value="<?php echo $companyName; ?>"><?php echo $companyName; ?></option>
    <?php endforeach; ?>
</select>
痞味浪人 2024-08-20 20:59:33

您需要 explode() 将列表转换为数组(如果您不能由于某种原因直接使用 $array )然后执行以下操作:

$listArray = explode(', ', $list);
echo '<select name="testSelect" id="testId">';
foreach ($listArray as $item)
    echo '<option value="'.htmlspecialchars($item).'">'. htmlspecialchars($item) . "</option>\n";
echo '</select>';

You need to explode() the list into an array (if you can't use $array directly for some reason) then do:

$listArray = explode(', ', $list);
echo '<select name="testSelect" id="testId">';
foreach ($listArray as $item)
    echo '<option value="'.htmlspecialchars($item).'">'. htmlspecialchars($item) . "</option>\n";
echo '</select>';
蓝礼 2024-08-20 20:59:33

为什么首先将数组转换为逗号分隔的列表?一旦你有了一个数组,你就可以

foreach ($company in $list) {
    echo '<option value="' . htmlspecialchars($company) . '">' . htmlspecialchars($company) . '</option>';
}

输出你的选项。如果由于某种原因确实需要使用逗号分隔的列表步骤,则始终可以使用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

foreach ($company in $list) {
    echo '<option value="' . htmlspecialchars($company) . '">' . htmlspecialchars($company) . '</option>';
}

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.

小兔几 2024-08-20 20:59:33

您可以使用类似 explode() 的方法将内容列表放入数组中,然后循环遍历该数组..类似:

$myArray = explode(",", $list);

echo "<select name=\"testSelect\" id=\"testId\">\n";
for($i = 0; $i < count($myArray); $i++) {
    // add each option to select list..
    echo "<option>" . $myArray[$i] . "</option>\n";
}
echo "</option>\n";

有关 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:

$myArray = explode(",", $list);

echo "<select name=\"testSelect\" id=\"testId\">\n";
for($i = 0; $i < count($myArray); $i++) {
    // add each option to select list..
    echo "<option>" . $myArray[$i] . "</option>\n";
}
echo "</option>\n";

More on PHP explode() function:

http://www.php.net/manual/en/function.explode.php

灼疼热情 2024-08-20 20:59:33

请参阅 implode()

您可以使用循环模拟 implode() 函数,字符串连接。

例如:

$out = '';
$append = ', ';

for($arr as $v)
{
  $out .= $v.$append;
}

$out = substr($out, 0, (-1) * strlen($append));

See implode()

You can simulate the implode() function with a loop and string concatenation.

For example:

$out = '';
$append = ', ';

for($arr as $v)
{
  $out .= $v.$append;
}

$out = substr($out, 0, (-1) * strlen($append));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文