Jump to content

[SOLVED] ordering the sql in php form


garry

Recommended Posts

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

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>

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.