tommyda Posted November 11, 2009 Share Posted November 11, 2009 Im stuck on this one so if anyone could help that would be much appreciated. I have a database table called genres which is a list of movie genres and a table called assign. In assign I have the genre id and the movie id. I am trying to create a multiple select list with all of the genres and if one of the genres is assigned to this movie to add selected="selected" to the option. while($genre=mysql_fetch_array($getgenres)) { while($assigned=mysql_fetch_array($getassignedgenres)) { if($genre['id']=$assigned['typeid']) { echo '<option selected="selected" style="padding:5px;" value="'.$genre['id'].'">'.$genre['genre'].'</option>'; } else { echo '<option style="padding:5px;" value="'.$genre['id'].'">'.$genre['genre'].'</option>'; }; }; }; At the moment there are 3 genres assigned to this movie but its just giving me the same 3 instead of each one. Please help Quote Link to comment Share on other sites More sharing options...
cags Posted November 11, 2009 Share Posted November 11, 2009 Only glanced at your code briefly but... if($genre['id']=$assigned['typeid']) ...is an assignment not a comparison. That would explain always getting the same result. Quote Link to comment Share on other sites More sharing options...
tommyda Posted November 11, 2009 Author Share Posted November 11, 2009 I changed it to == but im getting the same results Quote Link to comment Share on other sites More sharing options...
cags Posted November 11, 2009 Share Posted November 11, 2009 Your logic doesn't really work. This is also probably not the best method of achieving what your attempting. To select the genres for a movie you should simply need to do something like... $result = mysql_query("SELECT genres.name FROM genres JOIN assigned ON assigned.genreid=genres.id WHERE assigned.movieid=$something"); while($row = mysql_fetch_assoc($result)) { echo $row['name']; } Quote Link to comment Share on other sites More sharing options...
tommyda Posted November 11, 2009 Author Share Posted November 11, 2009 Cags Thanks for the replies. Im not trying to "select the genres for a movie" I have a page to insert movie listings and on that page I have a multiple select box with all the genres in table"genres" listed. I select the genres I would like to assign to this movie and when I save it they are inserted into table"assign". Now I am creating the "edit movie" page and would like to have the same list as in "insert movies" but with the genres that are already assigned I would like them to be selected. Is your last post going to achieve this or are you suggestion I do it some other way. I have no idea what that sql query means or does. Thanks Quote Link to comment Share on other sites More sharing options...
cags Posted November 11, 2009 Share Posted November 11, 2009 If your doing an edit page then you are trying to select the genres for a movie. Though given the extra information it would be better done differently. One method would be to fetch all the genreid's for the current movie. Loop through them adding them to an array. I would them fetch all of the genres (both id and name) and loop through those. For each one, you check if it's in the other array, if it is, you add in selected="selected" to the output. $movie_genres = array(); $result = mysql_query("SELECT genreid FROM assigned WHERE moveid=$something"); while($row = mysql_fetch_assoc($result)) { $movie_genres[] = $row['genreid']; } $result = mysql_query("SELECT * FROM genres"); echo '<select>'; while($row = mysql_fetch_assoc($result)) { if(in_array($row['id'], $movie_genres)) { echo '<option selected="selected" style="padding:5px;" value="'.$genre['id'].'">'.$genre['genre'].'</option>'; } else { echo '<option style="padding:5px;" value="'.$genre['id'].'">'.$genre['genre'].'</option>'; } } echo '</select>'; Quote Link to comment Share on other sites More sharing options...
tommyda Posted November 11, 2009 Author Share Posted November 11, 2009 Perfect thanks for your time, I learned a lot from this. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.