Page 1 of 1

selected option issue

Posted: Wed Jun 02, 2010 3:23 pm
by tito85
Hi everyone,

I am using the below code in an edit page of movie information so in the genre field it displayed as selected the genres in the database.

My problem is that only one genre is showed as selected although the movie has more than 1 genre in the database.

Any idea why?

Code: Select all

<?php
 $select = "SELECT * FROM genres WHERE MovieID = '$id'";
          $result = mysql_query($select);
		  $genreid = mysql_result($result, 0, 'GenreTypeID');
?>
<?php
  $query = "SELECT * FROM genretypes ORDER BY Genre ASC";
  $result2 = mysql_query($query);
  while ($genre = mysql_fetch_array($result2)) {
      $selected = "";
      if ($genreid == $genre['GenreTypeID']) {
          $selected = "selected=\"selected\"";
      }
      echo "<option $selected value=\"" . $genre['GenreTypeID'] . "\">" . $genre['Genre'] . "</option>";
  }
?>
Thanks!!!

Re: selected option issue

Posted: Wed Jun 02, 2010 4:03 pm
by AbraCadaver
I'm making some assumptions on your tables, but you need to get all genres for the movie, you were only getting one. Then see if the current genre is in that list. Something like this:

Code: Select all

$select = "SELECT * FROM genres WHERE MovieID = '$id'";
$result = mysql_query($select);
while ($row = mysql_fetch_array($result)) {
	$movie_genres[] = $row['GenreTypeID'];
}

$query = "SELECT * FROM genretypes ORDER BY Genre ASC";
$result2 = mysql_query($query);
while ($genre = mysql_fetch_array($result2)) {
	$selected = "";
	if (in_array($genre['GenreTypeID'], $movie_genres)) {
		$selected = "selected=\"selected\"";
	}
	echo "<option $selected value=\"" . $genre['GenreTypeID'] . "\">" . $genre['Genre'] . "</option>";
}

Re: selected option issue

Posted: Wed Jun 02, 2010 4:26 pm
by tito85
Thanks a lot AbraCadaver.

It is now working!

Thanks again for your time!