Jump to content

drop down list


Recommended Posts

Simple example.

 

<?php

  // connect
  $sql = "SELECT data FROM tbl";
  if ($result = mysql_query($sql)) {
    if (mysql_num_rows($result)) {
      echo "<select>";
      while($row = mysql_fetch_assoc($result)) {
        echo "<option value='{$row['data']}'>{$row['data']}</option>";
      }
      echo "</select>";
    }
  }

?>

Link to comment
Share on other sites

<?php

//Select the data from the DB
$sql = mysql_query("SELECT col1, col2 FROM tbl WHERE condition");

//Start dropdown
echo '<select name="s_name">';

//Populate the drop down with results from the DB
while ($row=mysql_fetch_assoc($sql)){
   echo "<option>" . $row['col'] . "</option>";
}

//end drop down
echo '</select>';
      
?>

 

I can see someone posted while I was, but I might as well post mine too since I spent the time typing it, haha.

Link to comment
Share on other sites

<?php

//Select the data from the DB
$sql = mysql_query("SELECT col1, col2 FROM tbl WHERE condition");

//Start dropdown
echo '<select name="s_name">';

//Populate the drop down with results from the DB
while ($row=mysql_fetch_assoc($sql)){
   echo "<option>" . $row['col'] . "</option>";
}

//end drop down
echo '</select>';
      
?>

 

I can see someone posted while I was, but I might as well post mine too since I spent the time typing it, haha.

 

Sorry, but thats a pretty bad example. Absolutely no error handling.

Link to comment
Share on other sites

if u get your data into an array this simple example might be usefull. im not too hot on db stuff though.

 

<?php

$test_array = array();

$test_array[0] = "Yes";

$test_array[1] = "No";

$test_array[2] = "Maybee";

 

echo "<select name=\"test_select\" >\n";

foreach ($test_array as $option)

{

if ( isset($option) && $option == "Yes")

{

echo "<option value=\"" . $option .  "\" selected>" . $option . "</option>\n";

} elseif ( isset($option) ) {

echo "<option value=\"" . $option .  "\">" . $option . "</option>\n";

}

}

echo "</select>";

?>

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.