Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

basic combobox / dropdownlist 1

Status
Not open for further replies.

Pampers

Technical User
Apr 7, 2004
1,300
AN
Hi everyone,
Sorry to bother you with basic stuff... Made a little database in MySQl and created some output in a table. Nice. Now I want to create a combobox with this data. But how do you do that. Looked at various placed, but didn't get it [bigglasses]. This is what i got for the table.
Code:
<?
//dim
$database="PhoneCode";


mysql_connect(localhost);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM tblIntPhoneCode";
$result=mysql_query($query);

$num=mysql_numrows($result);
mysql_close();

//build table
//table header
echo "<b><center>Database Output</center></b><br><br>";

//table filling
$i=0;
while ($i < $num) {

$country=mysql_result($result,$i,"country");
$phonecode=mysql_result($result,$i,"phonecode");

echo "$country<br>";

$i++;
}

?>


Pampers [afro]
Keeping it simple can be complicated
 
All you need is correct html now. Your code is more or less ready to do it, you have the header (which would be select tag in the new code), loop (which would ouput options) and after the loop you close the select tag again. See here how html select tag is constructed for a reference:
 
Hi Vragabond,
tnx for the response. I got the idea of the html tag. But putting it togehter? This is the best I got...and creates an error because of Parse Error at the beginning of the tag.
Code:
<?
//dim
$database="PhoneCode";


mysql_connect(localhost);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM tblIntPhoneCode";
$result=mysql_query($query);

$num=mysql_numrows($result);
mysql_close();

//loop
$i=0;
while ($i < $num)
{
$country=mysql_result($result,$i,"country");


<select>
  <option value ="country">$country</option>
$i++;
}
</select>
?>

Pampers [afro]
Keeping it simple can be complicated
 
Putting in an array... and it works (code from BBabb from CodeWalkers).

Code:
<?
//dim database
$database="PhoneCode";

//connect to my-sqlserver
mysql_connect(localhost);

//connect to database
@mysql_select_db($database) or die( "Unable to select database");

//select records
$query="SELECT Country FROM tblIntPhoneCode";

//put records in resultset
$result=mysql_query($query);

//build listbox
print "<SELECT name=item>"; 

    //loop and build array for listbox from resultset
    while ($line = mysql_fetch_array($result)) 
      { 
      foreach ($line as $value) 
       { 
         print "<OPTION value='$value'"; 
      }  
         print ">$value</OPTION>"; 
      } 

    //close connection to my-sqlserver
	mysql_close();
print "</SELECT>"; 

?>

Pampers [afro]
Keeping it simple can be complicated
 
Your first try might have worked if you'd moved the <select> to before the loop... But glad you found your own solution!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top