[英]nested foreach loop with IF statement
Database tables: 数据库表:
film (id_film PK, name) 电影(id_film PK,名字)
genre(id_genre PK, name) 类型(id_genre PK,名称)
film_genre(id_film FK, id_genre FK) film_genre(id_film FK,id_genre FK)
This outputs all genres from genre table: 这将从流派表输出所有类型:
$genremenu = $veza -> prepare("select * from genre");
$genremenu -> execute();
$resultmenu = $genremenu -> fetchALL(PDO::FETCH_OBJ);
This outputs all selected genres from film_genre table for specific film: 这将输出film_genre表格中所有选定的特定电影类型:
$izraz = $veza -> prepare("select * from genre a inner join film_genre b on a.id_genre=b.id_genre where b.id_film=:id_film");
$izraz -> execute(array("id_film" => $film -> id_film));
$selectedgenre = $izraz -> fetchAll(PDO::FETCH_OBJ);
I am having a problem with outputting data from database to multiple selected list in a form. 我在将数据从数据库输出到表单中的多个选定列表时遇到问题。 It's a movie database, and i'm doing foreach iteration to read all lines of movie genres to output to multiple select field.
这是一个电影数据库,我正在进行foreach迭代,以读取所有电影类型的行,以输出到多个选择字段。 But i'm having a trouble with outputting "selected" genres to that list.
但是我在输入“选定”类型到该列表时遇到了麻烦。 the code is
代码是
foreach ($resultmenu as $line) {
foreach ($selectedgenre as $sg) {
if ($line-> id_genre === $sg-> id_genre) {
echo "<option value=\"" . $line-> id_genre . "\" selected>" . $line-> name . "</option>";
} else {
echo "<option value=\"" . $line-> id_genre . "\">" . $line-> name . "</option>";
}
}
}
Now i'm aware that i got duplicate outputs in selected list because, for example, if movie has got 2 genres let's say Comedy and Crime, that means for every $line he will iterate twice to check for $selectedgenre, so i get output like: 现在我知道我在选定的列表中有重复的输出,因为,例如,如果电影有2个类型,让我们说喜剧和犯罪,这意味着每个$行他将迭代两次检查$ selectedgenre,所以我得到输出喜欢:
I'm new to php so i'm asking how to get the right list output with no duplicate entries? 我是php的新手,所以我问如何获得没有重复条目的正确列表输出? I tried with brake and continue but not working or i didnt use it right?
我试过制动并继续但不工作或我没有使用它? Please help and provide (if possible) more alternative solutions.
请帮助并提供(如果可能的话)更多替代解决方案。 Thank you!
谢谢!
make it unique array by using a loop as follow 通过使用如下循环使其成为唯一数组
$out=array();
foreach ($resultmenu as $line) {
$out[$line-> id_genre]=$line;
}
before using the array 在使用数组之前
or use distinct(id_genre)
in select statement or use group by id_genre
或者在select语句中使用
distinct(id_genre)
或使用group by id_genre
使用group by id_genre
or instead of foreach put the selectedgenre id_genre in an array and 或者代替foreach将selectedgenre id_genre放入数组中
foreach ($selectedgenre as $sg) {
if ($line-> id_genre === $sg-> id_genre) {
use inarray for checking 使用inarray进行检查
U Need to build an Array of Genres u have on the film and then crosscheck them with all there are. 你需要在电影中建立一个类型阵列,然后用它们进行交叉检查。
try this 试试这个
<?php
$genres = array();
foreach ($selectedgenre as $sg) {
$genres[] = $sg->id_genre;
}
foreach ($resultmenu as $line) {
if (in_array($line->id_genre,$genres)) {
echo "<option value=\"" . $line->id_genre . "\" selected>" . $line->name . "</option>";
} else {
echo "<option value=\"" . $line->id_genre . "\">" . $line->name . "</option>";
}
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.