2017-10-08 1 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

try var_dump($ moteur);あなたがどんな種類の価値を得ているかチェックしてください。 –

+0

$ moteurに含まれています。 $ moteurに基づいてチェックボックスを作成しますか? –

答えて

0

チェックボックスの値は1または0として保存されます(手動で変更した場合を除く)。自分で設定した値と比較しています。これは動作しません。

フォームデータを保存する前に操作している場合は、このコードスニペットからはわかりません。

入力名と値1または0を比較して、「checked」状態を切り替えます。

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> 
関連する問題