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
https://forums.phpfreaks.com/topic/50052-drop-down-list/#findComment-245679
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
https://forums.phpfreaks.com/topic/50052-drop-down-list/#findComment-245681
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
https://forums.phpfreaks.com/topic/50052-drop-down-list/#findComment-245684
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
https://forums.phpfreaks.com/topic/50052-drop-down-list/#findComment-245687
Share on other sites

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.