转载:http://bbs.****.net/topics/392019778?page=1
http://www.w3school.com.cn/php/func_array_in_array.asp
checkbox将选中的值写入数据库中,在修改的时候如何从数据库中读取并设定Checkbox的状态
1.写入数据库
提交后
因为你的rol是数组,所以可以使用$_POST获取
PHP code
1
2
3
4
5
6
|
<?php $rol = $_POST [ 'rol' ];
// 连接数据库后 $sqlstr = "insert into 表(rol) values(" .implode( ',' , $rol ). ")" ;
mysql_query( $sqlstr );
?> |
2修改时读到checkbox
PHP code
1
2
3
4
5
6
7
8
9
10
11
|
<?php // 连接数据库,把对应记录获取 $sqlstr = "select * from 表 limit 1" ;
$query = mysql_query( $sqlstr );
$result = mysql_fetch_assoc( $query ); // $result就是你的数据记录
$rols = explode ( ',' , $result [ 'rol' ]); // 分解为数组
然后判断: ?> <input type= "checkbox" name= "rol[]" value= "1" id= "rol_0" <?php if (in_array(1, $rols )){ echo 'checked="checked"' ;} ?> >
|
3简单效果
PHP code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
<?php $rols = array (1,2,3);
?> <li>拥有权限: <p>
<span style= "padding-top:15px;" >
<input type= "checkbox" name= "rol[]" value= "1" id= "rol_0" <?php if (in_array(1, $rols )){ echo 'checked="checked"' ; } ?> >
随机抽取人员</span>
<br>
<span>
<input type= "checkbox" name= "rol[]" value= "2" id= "rol_1" >
新增执法人员</span>
<br>
<span>
<input type= "checkbox" name= "rol[]" value= "3" id= "rol_2" >
编辑执法人员</span>
<br>
<span>
<input type= "checkbox" name= "rol[]" value= "4" id= "rol_3" >
信息查询打印</span>
<br>
<span>
<input type= "checkbox" name= "rol[]" value= "5" id= "rol_4" >
部门设置</span>
<br>
<span>
<input type= "checkbox" name= "rol[]" value= "6" id= "rol_5" >
管理员设置</span>
<br>
<span>
<input type= "checkbox" name= "rol[]" value= "7" id= "rol_6" >
全局设置</span>
<br>
</p>
</li>
|
4.php三个函数解释:
in_array() 函数搜索数组中是否存在指定的值。
in_array(search,array,type)
参数 | 描述 |
---|---|
search | 必需。规定要在数组搜索的值。 |
array | 必需。规定要搜索的数组。 |
type | 可选。如果设置该参数为 true,则检查搜索的数据与数组的值的类型是否相同。 |
说明
如果给定的值 search 存在于数组 array 中则返回 true。如果第三个参数设置为 true,函数只有在元素存在于数组中且数据类型与给定值相同时才返回 true。如果没有在数组中找到参数,函数返回 false。
注释:如果 search 参数是字符串,且 type 参数设置为 true,则搜索区分大小写。
例子:
<?php
$people = array("Bill", "Steve", "Mark", "David"); if (in_array("Mark", $people)
)
{
echo "匹配已找到";
}
else
{
echo "匹配未找到";
}
?>
explode() 函数把字符串打散为数组。
注释:"separator" 参数不能是空字符串。
语法
explode(separator,string,limit)
参数 | 描述 |
---|---|
separator | 必需。规定在哪里分割字符串。 |
string | 必需。要分割的字符串。 |
limit |
可选。规定所返回的数组元素的数目。 可能的值:
|
例子
<!DOCTYPE html>
<html>
<body><?php
</body>
$str = 'one,two,three,four'; // 零 limit
print_r(explode(',',$str,0)); // 正的 limit
print_r(explode(',',$str,2)); // 负的 limit
print_r(explode(',',$str,-1));
?>
</html>
结果:
Array ( [0] => one,two,three,four ) Array ( [0] => one [1] => two,three,four ) Array ( [0] => one [1] => two [2] => three )
implode() 函数返回由数组元素组合成的字符串。
语法
implode(separator,array)
参数 | 描述 |
---|---|
separator | 可选。规定数组元素之间放置的内容。默认是 ""(空字符串)。 |
array | 必需。要组合为字符串的数组。 |
例子
<!DOCTYPE html>
<html>
<body><?php
</body>
$arr = array('Hello','World!','I','love','Shanghai!');
echo implode("+",$arr);
?>
</html> 结果
Hello+World!+I+love+Shanghai! 5在MVC模式下的应用
html部分:
<?php
//根据逗号,将字符串分割为数组
$label = explode(',', $data["label"]);
?>
<input type="checkbox" name="label[]" value="1" <?php if(in_array(1, $label)){ echo 'checked="checked"';} ?> >网站
<input type="checkbox" name="label[]" value="2" <?php if(in_array(2, $label)){ echo 'checked="checked"';} ?> >客户端
<input type="checkbox" name="label[]" value="3" <?php if(in_array(3, $label)){ echo 'checked="checked"';} ?> >移动app
<input type="checkbox" name="label[]" value="4" <?php if(in_array(4, $label)){ echo 'checked="checked"';} ?> >硬件
<input type="checkbox" name="label[]" value="5" <?php if(in_array(5, $label)){ echo 'checked="checked"';} ?> >其他
Controller部分
$product = M('Product')->where(' id= 2')->find();
$this->assign('data', $product);
$this->display();
Mysql 数据库字段
id name label
label varchar类型 例如: 1,3,5