Jump to content

Populating dropdown box.


todayme

Recommended Posts

I have been looking around the internet for an example of what I need to do but cant find anything was wondering if someone here might be able to help.

 

What I need to do is to connect to a mysql db select a table and then a field from that table and put all the contents of that field into a dropdown box.

 

Anyone got any sugestions?

 

 

Link to comment
https://forums.phpfreaks.com/topic/41172-populating-dropdown-box/
Share on other sites

Something like this:

<select name="names"><?php
  $query=mysql_query("SELECT name FROM table ORDER BY name ASC");
  while ($fetch=mysql_fetch_assoc($query)) {
    echo '<option value="'.$fetch['name'].'">'.$fetch['name'].'</option>';
  }?></select>

this is how I do it:

 

<select size="1" name="txtRecall" style="font-family: Arial Unicode MS; font-size: 10px">
						<?php
						mysql_select_db("database_name") or die(mysql_error());
						$result = mysql_query("SELECT * FROM tblName")
			or die(mysql_error());
						while ($row = mysql_fetch_array($result)) {
							$Item_ID = $row['ID'];
							$Name = $row['Name'];
							?>
							<option value="<?=Item_ID?>"><?=$Name?></option>
						<? } ?>
						</select>

I have done this cond with your example but have a problem with it... any comments on what the problem may be? 

 

<?

 

$dbhost = "localhost";

$dbuser = "admin_sex";

$dbpass = "sex";

$dbname = "admin_sex";

 

 

 

$db = mysql_pconnect($dbhost,$dbuser,$dbpass);

mysql_select_db($dbname) or die(mysql_error());

 

 

<select name="names">

  $query=mysql_query("SELECT name FROM _Industries");

  while ($fetch=mysql_fetch_assoc($query)) {

    echo '<option value="'.$fetch['Industry'].'">'.$fetch['Industry'].'</option>';

  }</select>

 

 

 

 

?>

Okay I am trying this now no errors but also no data in the drop down box ha ha......any ideas lol

 

<?

 

$dbhost = "localhost";

$dbuser = "admin_sex";

$dbpass = "sex";

$dbname = "admin_sex";

 

 

 

$db = mysql_pconnect($dbhost,$dbuser,$dbpass);

mysql_select_db($dbname) or die(mysql_error());

 

?>

 

 

<select name="names"><?php

 

  $query=mysql_query("SELECT name FROM _Industries");

  while ($fetch=mysql_fetch_assoc($query)) {

    echo '<option value="'.$fetch['Industry'].'">'.$fetch['Industry'].'</option>';

  }?></select>

 

 

 

 

 

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.