在我的表单的“编辑视图”中,我有一个复选框列表,我想检查表book_have_languages
中的那些(注意一本书可以有多种语言)。
代码:
<?php $query_c = mysqli_query($mysqli, "SELECT id_language, name_language FROM languages ORDER BY name_language;")
or die('error '.mysqli_error($mysqli));
while ($data_c = mysqli_fetch_assoc($query_c)) {
echo "<div class='checkbox'><label><input type='checkbox' name='languages[]' value='$data_c[id_languages]'>$data_c[name_language]</label></div>";
$sql = "SELECT id_book, id_language FROM book_have_languages WHERE id_book=3";
$res = $mysqli->query($sql) or die("error: ".$sql);
$results = array();
while($row = mysqli_fetch_assoc($res)) {
$results[] = $row;
}
foreach ($results as $result){
if($result['id_language'] == $data_c['id_language']){
echo"<div class='checkbox'><label><input type='checkbox' name='languages[]' value='$data_c[id_language]'checked >$data_c[name_language]</label></div>";
}
}
}
?>
结果:
如果我的表是:
id_book | id_language
3 - 2
3 - 5
3 - 6
检查语言2,5,6,但它们看起来是重复的。如何在第一个回波中指示仅显示表中没有的语言?
答案 0 :(得分:0)
您正在循环使用这些语言,但不是通过书籍所使用的语言。如果您的表格如上所述,$data_idi
应该有多个结果,并且您只匹配第一个结果。你也应该在这些结果中循环。
答案 1 :(得分:0)
这就是我解决的问题:
<?php
$query_c = mysqli_query($mysqli, "SELECT id_language, name_language FROM languages ORDER BY name_language;")
or die('error '.mysqli_error($mysqli));
while ($data_c = mysqli_fetch_assoc($query_c)) {
$sql = "SELECT id_book, id_language FROM book_te_languages WHERE id_book='$_GET[id]'";
$res = $mysqli->query($sql) or die("error: ".$sql);
$results = array();
while($row = mysqli_fetch_assoc($res)) {
$results[] = $row;
}
echo "<div class='checkbox'><label><input type='checkbox' name='languages[]' value='$data_c[id_language]'";
foreach ($results as $result){
if($result['id_language'] == $data_c['id_language']){
echo "checked";
}
}
echo ">$data_c[name_language]</label></div>";
}
?>