Click to See Complete Forum and Search --> : Populating select menu, totally stuck!


DanUK
12-31-2006, 01:53 PM
Hello.

I am trying to add a select menu to my "edit profile" page, that shows what the data in the database is. The row is 'sex' and has an option of 'Male' or 'Female'.

What I want is for the list to have a 'selected' option with the value that's in the database, with the other options too in case it needs to be changed (?), and then if nothing is in the database, just a list, with the 'please select' shown.

Any help appreciated, at the moment I get repeated options.


<tr>
<th class="intro"><label>Sex:</label></th>
<td class="forminput"><select name="sex" class="formwidth">
<?php
if ($frm22==0 || $frm22=='')
echo "<option selected=\"selected\" value=\"\">Please select the sex</option>\n";
$users=mysql_query("Select * from user order by account");
while ($user=mysql_fetch_array($users))
{
if ($frm22==$user['sex'])
echo "<option selected value=\"".$user['sex']."\">".$user['sex']."</option>\n";
else
echo "<option value=\"Male\">Male</option>\n";
echo "<option value=\"Female\">Female</option>\n";
}
?></select></td>
</tr>

NightShift58
12-31-2006, 04:52 PM
This code could be fixed so that it would run BUT to make sure we're not wasting time for nought, let's make sure that we're fixing it to do what you really want it to do.

At this point, the problem is that you're looping through a list of of ALL users in the table - which could be tens or hundreds or more - but you're only comparing the value of $user['sex'] with a single variable, $frm22. At that point in your script, that comparison is fairly static - which is not likely to be what you want - and it's not going to give one <select> per user but one <select> for all users with a bunch of duplicate values and arbitrary "selected" attributes.

This can't be what you wanted. In any case, it doesn't make sense to me.

Q1: Are you trying to show the value of sex in the table for ONE user or for MANY users?

Q2: Where does $frm22 come from?

DanUK
12-31-2006, 05:06 PM
Hi there, thanks for your reply!
Glad you replied, as this is driving me nuts!

Yes, it's only for one user, not many.

Basically, I was trying to help myself by using code elsewhere on my management script (which obviously didn't go to plan!).

The working code, from another section, is:


<tr>
<th class="intro"><label>Trainer:</label></th>
<td class="forminput"><select name="trainer" class="formwidth">
<?php
if ($frm35==0 || $frm35=='')
echo "<option selected=\"selected\" value=\"0\">Please select a Trainer</option>\n";
$users=mysql_query("Select * from user where TR=1 order by account");
while ($user=mysql_fetch_array($users))
{
if ($frm35==$user['account'])
echo "<option selected value=\"".$user['account']."\">".$user['account']."</option>\n";
else
echo "<option value=\"".$user['account']."\">".$user['account']."</option>\n";
}
?></select></td>
</tr>


Where the frm35 is in a code section like:


$sql = "SELECT * FROM user WHERE id_user=" . $_GET['id'];
$result = mysql_query($sql);
$rows = mysql_num_rows($result);

if ($row = mysql_fetch_array($result))
{
$frm35=$row["trainer"];
}


For the one I mentioned, frm22 is $frm22=$row["sex"];

Thanks!

NightShift58
12-31-2006, 05:33 PM
Ok.

Although the "trainer thing" looks similar, it really isn't. In that particular script, they are letting you assign a trainer to a user based on a list of trainers in the table (TR=1. The list of available trainers could be long and that's why it's done that way.

In your case, you're only trying to assign values from within a limited set of options, whose definition lie outside the database, i.e. Male/Female.<tr>
<th class="intro"><label>Sex:</label></th>
<td class="forminput"><select name="sex" class="formwidth">

<?php
IF ($frm22==0 || $frm22=='') :
echo "<option selected='selected' value=''>Please select the sex</option>\n";
ENDIF;
echo "<option value='Male'" . ($frm22=="Male" ? "selected" : "") . ">Male</option>\n";
echo "<option value='Female'" . ($frm22=="Female" ? "selected" : "") . ">Female</option>\n";
?>
</select></td>
</tr>The above should do it. I don't know what is stored in $frm22 so you may have to adjust the script to reflect proper values/formats, i.e. 0,1,2,...

DanUK
12-31-2006, 06:16 PM
THANKS so much and happy new year - that worked great!