Jump to content

[SOLVED] Populating A Multiple Select List With (assigned and unassigned) Options


tommyda

Recommended Posts

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

Link to comment
Share on other sites

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'];
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>';

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.