2017-10-08 127 views
0

存在我尽量选择基于价值复选框使用破灭已经插入数据库中获取如此插入与分离数据库的价值,但当我拿出与选定的价值形式,它会选择什么chechbox检查是否值在数据库

//getting from DB 
$moteur=$row['moteur']; 
$moteur= explode(",",$moteur); 

try to select 

<th> <input type="checkbox" name="moteur[]" value="Vidange" 
<?php 
$count=count($moteur); 
for($i=0;$i<$count;$i++) 
echo ($moteur[$i]=='Vidange' ? 'checked' : 'disabled'); ?> > 

     </th> 
<th> <input type="checkbox" name="moteur[]" value="nv" 
<?php 
for($i=0;$i<$count;$i++) 
echo ($moteur[$i]=='nv' ? 'checked' : 'disabled'); ?> > 

      </th> 
<th> <input type="checkbox" name="moteur[]" value="remplace" 
<?php 
for($i=0;$i<$count;$i++) 
echo ($moteur[$i]=='remplace' ? 'checked' : 'disabled'); ?> > 
     </th> 
<th> <input type="checkbox" name="moteur[]" value="nettoye" 
<?php 
for($i=0;$i<$count;$i++) 
echo ($moteur[$i]=='nettoye' ? 'checked' : 'disabled'); ?> > 



     </th> 
<th> <input type="checkbox" name="moteur[]" value="effectue" 

<?php 
for($i=0;$i<$count;$i++) 

echo ($moteur[$i]=='effectue' ? 'checked' : 'disabled'); ?> >  </th> 
<th> <input type="checkbox" name="moteur[]" value="controle" 
<?php 
for($i=0;$i<$count;$i++) 
echo ($moteur[$i]=='controle' ? 'checked' : 'disabled'); ?> >  
     </th> 
+0

你能喜欢,清理代码,以便它是更具可读性? –

+0

尝试var_dump($ moteur);并根据 –

+0

$ moteur包含的内容检查您获得的数据类型是否正确。你想在$ moteur的基础上制作复选框。 –

回答

0

复选框值存储为1或0(除非您已手动更改此设置)。你正在比较你自己设定的价值。这不起作用。

如果您在保存之前操作表单数据,我无法从这段代码中知道,因为您从未提及它,我会假设不。

将输入名称与值1或0进行比较以切换“已检查”状态。

0

我从你的问题明白了什么,试试这个简单的代码片段

<th> <input type="checkbox" name="moteur[]" value="Vidange" <?php echo (in_array('Vidange',$moteur))? 'checked' : 'disabled' ?> ></th> 
<th> <input type="checkbox" name="moteur[]" value="nv" <?php echo (in_array('nv',$moteur))? 'checked' : 'disabled' ?> > </th> 
<th> <input type="checkbox" name="moteur[]" value="remplace" <?php echo (in_array('remplace',$moteur))? 'checked' : 'disabled' ?> ></th> 
<th> <input type="checkbox" name="moteur[]" value="nettoye" <?php echo (in_array('nettoye',$moteur))? 'checked' : 'disabled' ?> ></th> 
<th> <input type="checkbox" name="moteur[]" value="effectue" <?php echo (in_array('effectue',$moteur))? 'checked' : 'disabled' ?> > </th> 
<th> <input type="checkbox" name="moteur[]" value="controle" <?php echo (in_array('controle',$moteur))? 'checked' : 'disabled' ?> ></th>