PDA

View Full Version : Selecting DISTINCT except for one


e-Raser
08-11-2005, 05:00 AM
There's this membership script, I am adding usergroups to it and so I made a new field called groups and everything. Now I want to add it to the admin profile update page. I'd like it to be in a dropdown menu and I already know how to do that, using the distinct function and everything. But I'd like the user's current usergroup to be the default on the dropdown menu, so I know which one they are now.
I was going to retrieve the user's current group but if I get the groups from the whole list as well, it would come out twice wouldn't it?

raf
08-11-2005, 08:49 AM
i don't understand it + i don't see the MySQL relevance.
you'll normally do this with two querys:
- query 1 : get the current group --> store this groupname in a variable (like $currentgroup)
- query 2 : run the distinct --> loop throught this recordset to build the dropdown. For each option, you compaire the value with $currentgroup. If both values are the same, then you ad a selected="selected" in your option-tag.

like this (for PHP)

$sql = "SELECT groupname FROM yourtable WHERE somecolumn=somevalue LIMIT 1";
$result = mysql_query($sal) or die('Query 1 has problem');
if (mysql_num_rows($result) !=1){
die('No current group found');
}else{
$currentname = mysql_result($result, 0);
$sql = "SELECT DISTINCT groupname FROM yourtable";
$result = mysql_query($sal) or die('Query 2 has problem');
if (mysql_num_rows($result) < 1){
die('No groups found');
}else{
echo '<select name="whatever" id="whatever" size="1">'
while ($row = mysql_fetch_assoc($result)){
$selected = '';
if ($row['groupname']==$currentname ){
$selected = ' selected="selected"';
}
echo '<option value="', $row['groupname'] ,'"', $selected ,'>', $row['groupname'] ,'</option>' ;
}
echo '</select>';
}
}

if you would have a normalised db then you wounldn't need the distinct, of course. Yhen you could just query your grouptable (to get the groupname and groupID) and compaire it with the groupID of your current group. The ID is then the value of the options, and the name is then the vissible label.

e-Raser
08-12-2005, 05:05 AM
Okay thanks, the revelance to MySQL is that I needed to know what queries to run.