$username Posted July 9, 2007 Share Posted July 9, 2007 Hello All, I am working on some PHP code that automatically displays info from a MYSQL table in the drop down box. My code works fine but the only thing is it list 1 item that is the one it is pulling from the database and then the other default options that are in the code. <p> <tr><td><font color="blue">Client Name: </font></td> <td><select name="ClientName"> <option selected="<? echo $formVars["ClientName"]; ?>"><? echo $formVars["ClientName"]; ?></option> <?$sql = "SELECT * FROM userlogin ORDER BY `ID` DESC"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_assoc($result)) { echo "<option value=\"{$row['username']}\">{$row['username']}</option>"; } } } ?> Is there away to make it so it select one of the options and not display the 2 of the same items? Here is more code that works the same with yes and no drop down box. <p> <td><font color="blue">Paid:</font></td> <td><select name="Paid"> <option selected="<? echo $formVars["Paid"]; ?>"><? echo $formVars["Paid"]; ?></option> <option value="No">No</option> <option value="Yes">Yes</option> </select><big></big></td> </tr> </p> thanks, Brett Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted July 9, 2007 Share Posted July 9, 2007 to quote you (also please if its a php code/html use the opener tags so it turns to pretty print) <html> <p> <tr><td><font color="blue">Client Name: </font></td> <td><select name="ClientName"> <option selected="<?php echo $formVars["ClientName"]; ?>"><? echo $formVars["ClientName"]; ?></option> <?php $sql = "SELECT `username FROM userlogin ORDER BY `ID` DESC"; //Take only what you need the * pulls everything and slows you down if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_assoc($result)) { if($row['username'] == $magicuser){ echo "<option value=\"{$row['username']}\" selected>{$row['username']}</option>"; } else{ echo "<option value=\"{$row['username']}\" >{$row['username']}</option>"; } } } } ?> Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 9, 2007 Share Posted July 9, 2007 Another way: <?php $options=''; $query=mysql_query("SELECT `username` FROM `userlogin` ORDER BY `ID` DESC"); while ($fetch=mysql_fetch_assoc($query)) { $options.='<option value="'.$fetch['username'].'"'.($magicuser==$fetch['username'] ? ' selected="selected"' : '').'>'.$fetch['username'].'</option>'; } ?> That would build a string $options containing all the options. Next all you need to do is insert that into your HTML: <td><select name="ClientName"><?=$options?></select></td> Quote Link to comment Share on other sites More sharing options...
$username Posted July 9, 2007 Author Share Posted July 9, 2007 Ok I think i miss "spoke". What I am looking for is that the there are not to of the same options in the drop down box. How would I have the code to just show the selected and not display both test2. Its like it is showing the one that is from databse with all the clients and one that is from the database with the one that was selected when the case was made. See this is and update form so a person can go into the case to update. I hope that this makes sense. Thank you, Brett Quote Link to comment Share on other sites More sharing options...
sKunKbad Posted July 9, 2007 Share Posted July 9, 2007 You must use DISTINCT in your query. $query=mysql_query("SELECT DISTINCT`username` FROM `userlogin` ORDER BY `ID` DESC" Quote Link to comment Share on other sites More sharing options...
$username Posted July 9, 2007 Author Share Posted July 9, 2007 Well I tried the DISTINCT but it is still showing all both options I think it may have something to do with my option select "<option selected="<? echo $formVars["ClientName"]; ?>"><? echo $formVars["ClientName"]; ?></option>" <p> <tr><td><font color="blue">Client Name: </font></td> <td><select name="ClientName"> <option selected="<? echo $formVars["ClientName"]; ?>"><? echo $formVars["ClientName"]; ?></option> <?$sql = "SELECT DISTINCT username FROM userlogin ORDER BY `ID` DESC"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_assoc($result)) { echo "<option value=\"{$row['username']}\">{$row['username']}</option>"; } } } ?> </select><big>*</big></td> </tr> </p> Sorry about the Code/HTML I dont really know what you have to add to get the pretty colors. Thank you, Brett Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 9, 2007 Share Posted July 9, 2007 SELECT DISTINCT(`username`) FROM `userlogin` ORDER BY `ID` DESC Quote Link to comment Share on other sites More sharing options...
$username Posted July 9, 2007 Author Share Posted July 9, 2007 Ok sorry guys I am still pretty New. Here is my code <p> <tr><td><font color="blue">Client Name: </font></td> <td> <?$sql = "SELECT DISTINCT(`username`) FROM `userlogin` ORDER BY `ID` DESC"; $result=mysql_query($sql); echo "<select name=\"ClientName\">"; while($row=mysql_fetch_array($result)) { echo "<option value=\"$row[username]\">$row[username]"; } echo "</select>"; ?> </td> </tr> </p> Now it is showing just the first one in the list and not the one that was entered in the database as the one to be shown. I found this code that may work <p> <tr><td><font color="blue">Client Name: </font></td> <td> <?$sql = "SELECT DISTINCT(`username`) FROM `userlogin` ORDER BY `ID` DESC"; $result=mysql_query($sql); echo "<select name=\"ClientName\">"; while($row=mysql_fetch_array($result)) { echo "<option value=\"$row[username]\">$row[username]"; } echo "</select>";?> </td> </tr> </p> But I cannot get the right one to be selected. It should have the one that the database has to be selected. Thanks guys for putting up with me. Brett Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 9, 2007 Share Posted July 9, 2007 OK, the problem is that you are first creating an option for the selected value and then creating options for all the possible values (which would include the selected value that you already created). You need to remove the first bit of code to create an option just for the selected option. The samples above will create options for all possible values AND select the appropriate value! Try this: <select name="ClientName"> <?php $sql = "SELECT ClientName FROM userlogin ORDER BY `ID` DESC"; if ($result = mysql_query($sql)) { while ($row = mysql_fetch_assoc($result)) { $selected = ($row['username']==$formVars['ClientName'])?" selected=\"selected\"":""; echo "<option value=\"{$row['username']}\"{$selected}>{$row['username']}</option>"; } } ?> </select> Note: I am assuming 'ClientName' is unique, so DISTINCT is not necessary. Quote Link to comment Share on other sites More sharing options...
$username Posted July 9, 2007 Author Share Posted July 9, 2007 That did the trick. Thanks guys!!!! I have more brain busters for you guys so stay tuned. Thank you, Brett Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.