lundi 30 mars 2015

How to create checkboxes with preselected values from database?

I am trying to create checkbox with preselected values which comes from my database (Genres). For example, if this movie has 3 different genres: comedy, romance and action (in the database), these genres will be preselected (have default value) checkboxes. The problem is that i need all the checkboxes (genres) which I get from the following SQL-query:

$sql = 'SELECT Movie.* FROM Movie WHERE Movie.id = ?';

and default values (ticked checkboxes) which I get from the following SQL-query:

$sql = 'SELECT Genre.name FROM (Genre JOIN Movie2Genre ON Movie2Genre.idGenre = Genre.id) JOIN Movie ON Movie2Genre.idMovie = Movie.id WHERE Movie.id = ?';



If you have a solution or any simpler method I would love to hear you out.

(PS: My if-statement is not working as I want it to do)



<?php
foreach ($allGenre as $val){
foreach ($aktivGenre as $act) {
if ($act==$val) {
$active = "checked";
}else {
$active = "";
}
}


echo "<input type='checkbox' name='{$val["name"]}' value='{$val["name"]}'".$active." >{$val["name"]}"; //$active="selected" when
}
?>




Aucun commentaire:

Enregistrer un commentaire