garry Posted May 30, 2008 Share Posted May 30, 2008 So I have a page where users can edit an artist. Once they select the artist, they are bought to a form with the artist name, description and genre. The genre is in a select list where the database is queried to obtain a list of all the genres, the genres are then all displayed in the list. The problem is the default genre used (the first one that is on the list that will be obtained if nothing else is selected). If the user is simply editing the description, they won't worry about the changing the genre and will just click "submit" which would change the genre from what it should be to something else. My question is, how is it that I can have the current genre for the artist (already in database) appear at the top of the select list so that the users don't accidentally change the genre to something incorrect. Here's the code i'm using: //the initial query used to get all of the artist information // (contains a row called genre_id to specify the genres current artist $query = "SELECT * FROM artists WHERE artist = '$artistname' "; // and the query to get all of the genres <select name="genre" > <? $query = "SELECT * FROM genres ORDER BY "; $result = mysql_query($query); while ($row = mysql_fetch_assoc($result)) { echo "<option name=\"genre\" value=\"" . $row['id'] . "\">" . $row['genre'] . "</option><br />"; } ?> </select> Thanks for the help! Link to comment https://forums.phpfreaks.com/topic/107950-solved-ordering-the-sql-in-php-form/ Share on other sites More sharing options...
Gighalen Posted May 30, 2008 Share Posted May 30, 2008 Please post the code for the editartist script so I may revise it for you. Link to comment https://forums.phpfreaks.com/topic/107950-solved-ordering-the-sql-in-php-form/#findComment-553313 Share on other sites More sharing options...
garry Posted May 30, 2008 Author Share Posted May 30, 2008 Okay, here's the entire form that I'm using for the edit artist, i'm didn't post the whole file because it's very large but just ask if you need more.. <?php $query = "SELECT * FROM artists WHERE artist = '$artistname' "; $result = mysql_query($query); $row = mysql_fetch_assoc($result); $artist = clean_output($row['artist']); $description = clean_output($row['description']); $artistid = $row['id']; $genreid = $row['genre_id']; if (mysql_num_rows($result) == 1) { ?> <form action="<?php $_SERVER['PHP_SELF']; ?>" method="POST"> <table width="615" border="0"> <tr> <td><div align="right">Artist: </div></td> <td><input type="text" name="artist" value="<?php echo $artist; ?>"></td> <td><input type="hidden" name="artistid" value="<?php echo $artistid; ?>"></td> </tr> <tr> <td><div align="right">Description: </div></td> <td><textarea name="description" rows="20" cols="80"><?php echo $description; ?></textarea></td> </tr> <tr> <td><div align="right">Genre: </div></td> <td><select name="genre" > <? $query = "SELECT * FROM genres ORDER BY "; $result = mysql_query($query); while ($row = mysql_fetch_assoc($result)) { echo "<option name=\"genre\" value=\"" . $row['id'] . "\">" . $row['genre'] . "</option><br />"; } ?> </select></td> </tr> <tr> <td height="33"><input name="submitted2" type="hidden" value="1"></td> <td><input name="submit" type="submit" value="Edit" /></td> </tr> </table> </form> Link to comment https://forums.phpfreaks.com/topic/107950-solved-ordering-the-sql-in-php-form/#findComment-553316 Share on other sites More sharing options...
garry Posted May 30, 2008 Author Share Posted May 30, 2008 nobody? Link to comment https://forums.phpfreaks.com/topic/107950-solved-ordering-the-sql-in-php-form/#findComment-553356 Share on other sites More sharing options...
saint959 Posted May 30, 2008 Share Posted May 30, 2008 Hi Garry, you could do this: <?php $query = "SELECT * FROM genres ORDER BY "; $result = mysql_query($query); while ($row = mysql_fetch_assoc($result)) { echo "<option name=\"genre\" value=\"" . $row['id'] . "\" "; if ($genreid == $row['id']){ echo "selected";} echo ">" . $row['genre'] . "</option><br />"; } ?> basically all im doing is checking if the genreid from the artist table is equal to the genre id from the genres table. that will only work if the id you palce in the artist table is equal to the corresponding id in the genre table. Hope this helps Link to comment https://forums.phpfreaks.com/topic/107950-solved-ordering-the-sql-in-php-form/#findComment-553361 Share on other sites More sharing options...
garry Posted May 30, 2008 Author Share Posted May 30, 2008 I can see what you're doing but what does adding the "selected" text do to it? Link to comment https://forums.phpfreaks.com/topic/107950-solved-ordering-the-sql-in-php-form/#findComment-553363 Share on other sites More sharing options...
saint959 Posted May 30, 2008 Share Posted May 30, 2008 it makes that "option" the default one. for example: say the artist genre was "rock" and rocks id is 6. then when you create the dropdown when the id in the genre table is 6 it will echo selected, which makes that option the default one. i.e the one the user editing the artist will see by default Link to comment https://forums.phpfreaks.com/topic/107950-solved-ordering-the-sql-in-php-form/#findComment-553367 Share on other sites More sharing options...
garry Posted May 30, 2008 Author Share Posted May 30, 2008 Ah dude that worked perfectly. You're a champ Thanks! Link to comment https://forums.phpfreaks.com/topic/107950-solved-ordering-the-sql-in-php-form/#findComment-553370 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.