I can't get the code below to match more than the very first run, what's wrong with it?
$genres are the number of genres in a database.
$query_genre are a query for all genres in the genre table og a database.
$current_genre are the genres that is pulled out for one specific movie.
Then I want to see if the genres set for the movie matches the genres from the database, if they do it will be selected.
Take a look at the code and I think you now where I'm going with it.
<?php
$query_genre = mysql_query("SELECT * FROM genre ORDER BY genre_id ASC");
$current_genre = mysql_query("SELECT genre_id FROM genre_movie WHERE movie_id=$movie_id");
if (mysql_num_rows($current_genre) == 0) {
unset($current_genre);
}
$genres = mysql_num_rows($query_genre);
echo "<select class=\"formfield\" name=\"genre[]\" multiple size=\"$genres\">";
while ($g_row = mysql_fetch_array($query_genre)) {
$genre_id = $g_row["genre_id"];
$genre = $g_row["genre"];
if (isset($current_genre)) {
while ($cg_row = mysql_fetch_array($current_genre)) {
$cgenre_id = $cg_row["genre_id"];
if ($cgenre_id == $genre_id) {
$selected = " selected";
}
elseif (($genreid != $genre_id) && (isset($selected))) {
unset ($selected);
}
}
}
else {
if (isset($selected)) {
unset($selected);
}
}
}
echo "<option value=\"$genre_id\"$selected>$genre";
echo "</select>";
?>
Here is a dump of both my genre table and my genre_movie table.
CREATE TABLE genre (
genre_id int(5) NOT NULL auto_increment,
genre text NOT NULL,
PRIMARY KEY (genre_id),
UNIQUE genre_id (genre_id)
);
CREATE TABLE genre_movie (
genre_id int(5) DEFAULT '0' NOT NULL,
movie_id int(5) DEFAULT '0' NOT NULL,
PRIMARY KEY (genre_id, movie_id)
);