5

Database tables:

film (id_film PK, name)

genre(id_genre PK, name)

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:

$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. 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:

  1. Comedy
  2. Comedy "selected"
  3. Crime
  4. Crime"selected"
  5. Horror
  6. Horror
  7. etc.

I'm new to php so i'm asking how to get the right list output with no duplicate entries? 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!

4
  • 1
    DISTINCT on your SQL query will help sort your duplicate issue. As for the selected issue, what does var_dump($line->id_genre, $sg->id_genre); output? Commented Sep 6, 2016 at 9:33
  • check this - if ($line-> id_genre === $sg-> id_genre) Commented Sep 6, 2016 at 9:43
  • I updated my question so i think that DISTINCT wont help cause i'm not selecting from one table with different values... @JonStirling it's more clear now from the question what var_dump outputs Commented Sep 6, 2016 at 10:13
  • Can anyone come to idea how to resolve this problem? Help please Commented Sep 6, 2016 at 21:21

2 Answers 2

3

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>";
    }
}

?>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you alot for your answer! This solved my problem i was confused how to check value only once so not to get duplicated outputs! @KikiTheOne regards!
2

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

or instead of foreach put the selectedgenre id_genre in an array and

foreach ($selectedgenre as $sg) {
       if ($line-> id_genre === $sg-> id_genre) {

use inarray for checking

1 Comment

i dont have duplicate entries in database so why should I use distinct(id_genre)? or where should I use group by... I updated my question with database structure and querys so please read now and if possible give your idea for solution. thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.