Home  >  Article  >  Backend Development  >  php 怎么输出复选框呢?

php 怎么输出复选框呢?

WBOY
WBOYOriginal
2016-06-23 14:05:122081browse

表单有6个复选框,如果选中的分别是第126个,数据库存为   1,2,6

现在的问题是  在修改的页面 怎么用php从数据库取值,输出6个复选框,让126为checked ?   


回复讨论(解决方案)

$row['id']=1;
>

我的意思是  从数据库取到值 1,2,6 之后, 怎么输出6个复选框,让第126个默认选中,345个不选中,
难点在于中间插入不选中的

我的意思是  从数据库取到值 1,2,6 之后, 怎么输出6个复选框,让第126个默认选中,345个不选中,
难点在于中间插入不选中的
把你选出来的复选框(比如对应1,2, 6)所对应的checked 属性设置上就可以了啊。
如果你数据库存的是1,2,6,你可以直接把对应的复选框的名字或id也设置成对应的,然后就可以直接访问到需要改变状态的checkbox

我的意思是  从数据库取到值 1,2,6 之后, 怎么输出6个复选框,让第126个默认选中,345个不选中,
难点在于中间插入不选中的
做个if判断,等于1,2,6的就 checked, 不等的就不checked

echo "<input type="/checkbox/" value=/"1/" checked=true>";echo "<input type="/checkbox/" value=/"2/" checked=true>";echo "<input type="/checkbox/" value=/"3/">";echo "<input type="/checkbox/" value="/4/">";echo "<input type="/checkbox/" value="/5/">";echo "<input type="/checkbox/" value="/6/" checked=true>";

$arr=array(1,2,6);for($i=0;$i<6;$i++){    $checked=in_array($i+1,$arr) ? 'checked' : '';    echo "<input type='checkbox' ".$checked.">\n";}

...
数据库取值
$a = '1,2,6';

请问if括号里面怎么写?


" />
" />
" />
" />
" />
" />




$a='1,2,6';
$ar=explode(',',$a);
$i=0;
?>
 />
 />
 />
 />
 />
 />

终于搞定,多谢楼上大牛。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn