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
https://forums.phpfreaks.com/topic/193904-echo-values-from-sql-into-dropdpwn/
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

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

Archived

This topic is now archived and is closed to further replies.

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