doumu9019 2014-04-08 15:24
浏览 54

too long

I have 3 SQL tables, "artists", "tracks" and "tracks_artists" (=linking table, because one track can have multiple artists, I'm restricted to use a linking table).

I now have an "edit page" where I want to show 4 selects where you can choose an arists, but with the track's artists already selected. So if an track has 2 artists, I want the first 2 selects to display the previously selected artists (alongside with all the others artists in my database) and the other 2 selects just to show all the artists in the select (with nobody selected).

I'm now using this, but this only works when the track has just one artist:

<select name="edit_artist">
<?php 
$query_artist = "SELECT * FROM artists";
$result_artist = mysql_query($query_artist) or die(mysql_error());
while ($row_artist = mysql_fetch_array($result_artist)) {?>
<?php
$query_ta = "SELECT artistID FROM tracks_artists WHERE trackID = $trackID";
$result_ta = mysql_query($query_ta);
    while ($row_ta = mysql_fetch_array($result_ta)){
        if($row_ta[0]==$row_artist[artistID]){$selected = "selected='selected'";}
        else {$selected ="";}
    } 
?>
<option value="<?php echo $row_artist[artistID]?>" <?php echo $selected;?>><?php echo $row_artist[name];?></option>
<?php }?>
</select>
  • 写回答

1条回答 默认 最新

  • doucan2102 2014-04-08 15:32
    关注

    You need to use the multiple attribute on your select

    <select name="edit_artist" multiple="multiple">
    

    If you want to submit this you'll need to make the edit_artist name an array using brackets

    <select name="edit_artist[]" multiple="multiple">
    
    评论

报告相同问题?