我想要编辑表单,用户已选择值已表示我想以编辑形式显示已选中的复选框值,所以我正在尝试这样
<?php
include("dbconfig.php");
$sql = "SELECT * FROM residential_propertytype WHERE status !='1'";
$result = mysql_query($sql);
while($pr_type = mysql_fetch_array($result))
{
$propty[] = $pr_type;
}
echo "<pre>";
var_dump($propty);
echo "</pre>";
foreach($propty as $res)
{
$checked = in_array($res['id'], 5) ? 'checked' : '';
?>
<input type="checkbox" value="<?php echo $res['id']?>" <?php echo $checked; ?>><?php echo $res['propertyName']?>
<?php
}
?>
见下文 var_dump($ propty)我得到这样的答案
array(10) {
[0]=>
array(8) {
[0]=>
string(1) "1"
["id"]=>
string(1) "1"
[1]=>
string(9) "Apartment"
["propertyName"]=>
string(9) "Apartment"
[2]=>
string(0) ""
["reg_date"]=>
string(0) ""
[3]=>
string(1) "0"
["status"]=>
string(1) "0"
}
[1]=>
array(8) {
[0]=>
string(1) "2"
["id"]=>
string(1) "2"
[1]=>
string(9) "Villament"
["propertyName"]=>
string(9) "Villament"
[2]=>
string(0) ""
["reg_date"]=>
string(0) ""
[3]=>
string(1) "0"
["status"]=>
string(1) "0"
}
[2]=>
array(8) {
[0]=>
string(1) "3"
["id"]=>
string(1) "3"
[1]=>
string(16) "Individual House"
["propertyName"]=>
string(16) "Individual House"
[2]=>
string(0) ""
["reg_date"]=>
string(0) ""
[3]=>
string(1) "0"
["status"]=>
string(1) "0"
}
[3]=>
array(8) {
[0]=>
string(1) "4"
["id"]=>
string(1) "4"
[1]=>
string(9) "Row House"
["propertyName"]=>
string(9) "Row House"
[2]=>
string(0) ""
["reg_date"]=>
string(0) ""
[3]=>
string(1) "0"
["status"]=>
string(1) "0"
}
[4]=>
array(8) {
[0]=>
string(1) "5"
["id"]=>
string(1) "5"
[1]=>
string(5) "Villa"
["propertyName"]=>
string(5) "Villa"
[2]=>
string(0) ""
["reg_date"]=>
string(0) ""
[3]=>
string(1) "0"
["status"]=>
string(1) "0"
}
[5]=>
array(8) {
[0]=>
string(1) "6"
["id"]=>
string(1) "6"
[1]=>
string(4) "Plot"
["propertyName"]=>
string(4) "Plot"
[2]=>
string(0) ""
["reg_date"]=>
string(0) ""
[3]=>
string(1) "0"
["status"]=>
string(1) "0"
}
[6]=>
array(8) {
[0]=>
string(1) "7"
["id"]=>
string(1) "7"
[1]=>
string(13) "Builder Floor"
["propertyName"]=>
string(13) "Builder Floor"
[2]=>
string(0) ""
["reg_date"]=>
string(0) ""
[3]=>
string(1) "0"
["status"]=>
string(1) "0"
}
[7]=>
array(8) {
[0]=>
string(1) "8"
["id"]=>
string(1) "8"
[1]=>
string(9) "Penthouse"
["propertyName"]=>
string(9) "Penthouse"
[2]=>
string(0) ""
["reg_date"]=>
string(0) ""
[3]=>
string(1) "0"
["status"]=>
string(1) "0"
}
[8]=>
array(8) {
[0]=>
string(1) "9"
["id"]=>
string(1) "9"
[1]=>
string(10) "Farm House"
["propertyName"]=>
string(10) "Farm House"
[2]=>
string(0) ""
["reg_date"]=>
string(0) ""
[3]=>
string(1) "0"
["status"]=>
string(1) "0"
}
[9]=>
array(8) {
[0]=>
string(2) "10"
["id"]=>
string(2) "10"
[1]=>
string(16) "Studio Apartment"
["propertyName"]=>
string(16) "Studio Apartment"
[2]=>
string(0) ""
["reg_date"]=>
string(0) ""
[3]=>
string(1) "0"
["status"]=>
string(1) "0"
}
}
在进行选中复选框值时,我收到错误警告:in_array()期望参数2为数组,给定整数如何解决此问题
答案 0 :(得分:0)
您需要定义,可能是您对5
..
应该是这样的:$ array_list = array(1,2,3,4,5);
$checked = in_array($res['id'], $array_list) ? 'checked' : '';
在您的示例中,您传递的是5
而不是array
答案 1 :(得分:0)
您知道要匹配的值和数组键,为什么要使用in_array()
?
$checked = ($res['id'] == 5) ? 'checked' : '';
答案 2 :(得分:0)
在HTML表单代码中,您要创建数组的复选框都需要与末尾的括号相同。这样选中的复选框将在$ _POST数组
中形成一个数组<form method=post action=yourstuff.php>
Pick your favorite pizza toppings -
<input type=checkbox name=topping[] value="pepperoni"> Pepperoni <br />
<input type=checkbox name=topping[] value="ex. chz"> Extra cheese <br />
<input type=checkbox name=topping[] value="black olives"> Black olives <br />
<input type=submit>
</form>