To give some context, the following dropdown is within a form that gives the current profile description of an animal stored in the database: i.e, this pony is yea high, of this gender, owned by somesuch, etc. The purpose of the form is to edit these current values. There are various options that can be overwitten freely, others where I want the choice limited to options from a dropdown menu.
The following is the current code I am using for the Gender field, which – although it does work – cannot be the proper way of doing things. I would be more interested in a method that queried the current state, gave the current state as the default option, stored the current state, queried other available states not equal to the stored current state, then gave the remaining states as options. It is that method that I could best adapt to all the other dropdowns on the form.
There are two tables referenced – the profile tbl and the prm_breedgender tbl. Each gender is given an ID, each profile is then given a corresponding ID to signify their gender (male=1, female=2, etc.). The $profile variable is that which signifies the current profile being looked at.
<label for="profile-gender">Gender / Type:</label>
<select name="profile-gender">
<?php
$genderresult = mysql_query("SELECT prm_breedgender.BreedGender
FROM profiles, prm_breedgender
WHERE ProfileID = $profile
AND prm_breedgender.BreedGenderID = profiles.ProfileGenderID");
$row = mysql_fetch_array($genderresult);
echo '<option value="' . $row['BreedGenderID'] . '">' . $row[BreedGender] . '</option>';
$exgenderresult = mysql_query("SELECT prm_breedgender.BreedGender
FROM profiles, prm_breedgender
WHERE ProfileID = $profile
AND prm_breedgender.BreedGenderID != profiles.ProfileGenderID");
while ($row = mysql_fetch_array($exgenderresult)) {
echo '<option value="' . $row['BreedGenderID'] . '">' . $row[BreedGender] . '</option>';
}
?>
</select>
Any help would be greatly appreciated. I’m not all that experience (obviously!) so accompanying explainations would be fantastic.
You can combine it into a single query, using a computed ‘order by’ value:
The order by clause will evaluate to either true (you’re on the record that should be first) or false (any other record), which gets cased to a value 1/0 which can be sorted, and by doing it in decreasing order, the true values (1) come out first.