php foreach返回最后一个选择的变量
foreach似乎无法正常工作,其中最后选择的值正在工作,但不是全部 从以下位置选择阵列:php foreach返回最后一个选择的变量,php,Php,foreach似乎无法正常工作,其中最后选择的值正在工作,但不是全部 从以下位置选择阵列: <fieldset> <legend>status:</legend> <?php foreach ($statuss as $status): ?> <div><label for="status<?php hscout($status['id']); ?>"><in
<fieldset>
<legend>status:</legend>
<?php foreach ($statuss as $status): ?>
<div><label for="status<?php hscout($status['id']);
?>"><input type="checkbox" name="statuss1[]"
id="status<?php hscout($status['id']); ?>"
value="<?php hscout($status['id']); ?>"<?php
if ($status['selected'])
{
echo ' checked="checked"';
}
?>/><?php hscout($status['description']); ?></label></div>
<?php endforeach; ?>
</fieldset>
$selected1s[]
仅在选择一个变量时有效。如果选择了多个变量,则最后一个变量将被解析。每次通过获取所选项目的foreach
循环时,您都会说$selected1s=array()
,这将清除用于累积项目的数组
以写着$selected1s=array()的行为例如果您只是想让代码正常工作,请将其移动到foreach
循环之前
不过,有一种方法可以构建一个查询,一次获取所有行。这通常比为每个项目做一大堆查询更容易
$selected1s = array();
if (!empty($_POST['statuss1'])) {
// Create a function that SQL-escapes and quotes an ID...
// (Note: requires PHP 5.3+)
$sqlify = function($id) use ($link) {
return "'" . mysqli_real_escape_string($link, $id) . "'";
};
// and apply it to the array to get back a list of SQL-ready values
$ids = array_map($sqlify, $_POST['statuss1']);
// string them together as a comma-separated list
$ids_sql = implode(',', $ids);
// and say `d IN (the list)`
$sql = "SELECT b FROM c WHERE d IN ($ids_sql)";
... do the query, fetch results into $selected1s
}
(这是我通常提倡准备好的语句并告诉您使用它们的部分。在大多数情况下,您应该这样做。但是对于中的查询或等效的或查询,它们往往很糟糕。)
$selected1s = array();
if (!empty($_POST['statuss1'])) {
// Create a function that SQL-escapes and quotes an ID...
// (Note: requires PHP 5.3+)
$sqlify = function($id) use ($link) {
return "'" . mysqli_real_escape_string($link, $id) . "'";
};
// and apply it to the array to get back a list of SQL-ready values
$ids = array_map($sqlify, $_POST['statuss1']);
// string them together as a comma-separated list
$ids_sql = implode(',', $ids);
// and say `d IN (the list)`
$sql = "SELECT b FROM c WHERE d IN ($ids_sql)";
... do the query, fetch results into $selected1s
}