2016-07-08 2 views
0

他人のデータをチェックボックスで表示するデータを爆発取得し、私は、データベーステーブル内のすべての果物(「フルーツ」)などを取得する必要がありますこの

$all_fruits = array('apple','banana','kiwi','melon','orange','watermelon');

は、チェックボックスに表示されますが、テーブルに(「fruit_select」)を果物を持っている場合は、チェックボックスにチェックインする必要があります。 テーブル( 'fruit_select')のいくつかのデータをチェックして表示するにはどうすればよいですか?

あなたはこれを試すことができ、すべてのデータ

foreach ($all_fruits as $fruit){ 
    echo "<label><input type='checkbox' value='".$fruit."'/>".$fruit."</label>"; 
} 
+0

は何を'$ fruit =" apple、orange、banana ";とあなたのデータベースの関係は?それは行の価値ですか?もしそうなら、私はあなたのテーブルを再構成するだろう。ここの実際の目標は私にはあまり明確ではありません。 – chris85

答えて

1

表示するには、この私の見解コード:

$fruit = "apple,orange,banana"; 
$fruitArr = explode(",",$fruit); // convert selecte fruits to array 
foreach ($all_fruits as $fruit){ 
    $checkedStatus = ""; 
    // check if $fruit in $selected fruit array - make it checked 
    if(in_array($fruit,$fruitArr)) { $checkedStatus ="checked"; } 
    echo "<label><input type='checkbox' ".$checkedStatus." value='".$fruit."'/>".$fruit."</label>"; 
} 

出力:

<body> 
 
    <label><input type="checkbox" value="apple" checked="">apple</label> 
 
    <label><input type="checkbox" value="banana" checked="">banana</label> 
 
    <label><input type="checkbox" value="kiwi">kiwi</label> 
 
    <label><input type="checkbox" value="melon">melon</label> 
 
    <label><input type="checkbox" value="orange" checked="">orange</label> 
 
    <label><input type="checkbox" value="watermelon">watermelon</label> 
 
</body> 
 

 

 
Output:

+0

うわー、ありがとうございます=) –

関連する問題

 関連する問題