Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nested foreach loop with IF statement

Tags:

php

mysql

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!

like image 383
Josip Avatar asked Sep 06 '16 09:09

Josip


People also ask

Can you nest forEach loops?

An important feature of foreach is the %:% operator. I call this the nesting operator because it is used to create nested foreach loops. Like the %do% and %dopar% operators, it is a binary operator, but it operates on two foreach objects.

Can we use nested for each loop in Java?

Java, like most other programming languages, supports nested loops. This means just a loop within a loop.

Can we use for loop inside forEach loop in Javascript?

forEach loops accept a callback function whereas for loops do not. In a for loop, all of your code is enclosed in the main body of the loop. In a forEach loop, you must write a function which will be executed for each item in the list over which you are iterating.

Can we use forEach inside forEach in Java?

Since you are operating on stream, it also allows you to filter and map elements. Once you are done with filtering and mapping, you can use forEach() to operate over them. You can even use the method reference and lambda expression inside the forEach() method, resulting in a more clear and concise code.


2 Answers

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

?>
like image 59
KikiTheOne Avatar answered Sep 21 '22 02:09

KikiTheOne


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

like image 32
Swarna Sekhar Dhar Avatar answered Sep 22 '22 02:09

Swarna Sekhar Dhar