Jump to content

Echo Values from SQL into dropdpwn


drewb29693

Recommended Posts

This page updates values already in a particular database.  I can get the values to show in the dropdown menu and i can get the page to update my database.  I need to know how to set the default value on the dropdown to what the customer has already entered instead if just want is first in the table.  It is probably something easy i just can't think of it.  Thanks in advance whoever fixes my problem.  Here is my code:

 

 

<td height="40" align="right" valign="top" bgcolor="#FFFFFF">Time Type:<span class="style1">__</span></td>

      <td align="left" valign="top"><label>

        <?php $result = mysql_query("SELECT type, visit_id FROM visit_type "); ?>

        <select name="visit" id="visit">

          <?php while($row = mysql_fetch_array($result))  { ?>

          <?php echo '<option value=' . $row[visit_id] . '> ' . $row['type']  . '</option> '; ?>

          <?php  } ?>

        </select>

      </label></td>

Link to comment
Share on other sites

try this:

 

 

<?php $result = mysql_query("SELECT type, visit_id FROM visit_type ");

        echo '<select name="visit" id="visit">';

        while($row = mysql_fetch_array($result))  {

          if ($row[visit_id] == $variableNameOfPreviouslySelectedItem){

              echo '<option selected value=' . $row[visit_id] . '> ' . $row['type']  . '</option> ';

          }

          else {

              echo '<option value=' . $row[visit_id] . '> ' . $row['type']  . '</option> ';

          }

        }

        echo '</select>';

?>

 

 

just change '$variableNameOfPreviouslySelectedItem' to the variable name of whatever the previously selected item is

Link to comment
Share on other sites

whenever i do this i use the ternary operator

 

<td height="40" align="right" valign="top" bgcolor="#FFFFFF">Time Type:<span class="style1">__</span></td>
       <td align="left" valign="top"><label>
         <?php $result = mysql_query("SELECT type, visit_id FROM visit_type "); ?>
         <select name="visit" id="visit">
           <?php while($row = mysql_fetch_array($result))   { ?>
           <?php echo '<option value=' . $row[visit_id] . $row[visit_id]==$whatever ? " selected" : ""'> ' . $row['type'] . ' </option> '; ?>
           <?php   } ?>
         </select>
       </label></td>

 

make sure to set $whatever to your checked item which will probably be another mysql query

Link to comment
Share on other sites

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.