Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/59104-solved-prefilled-drop-down-box/
Share on other sites

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>";
       }     
}
    }
  }
  ?>

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>

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.dropdownbox.JPG

 

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

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

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

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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