...

View Full Version : combo box



bunny1
03-05-2007, 05:26 PM
I am allowing users insert data into a database using a form.
I want one of the fields "department" to display all existing departments in the database in a combo box for users to choose from and also have a field in the combo box for them to add a new department.
Is there a way for me to do this in using php?
I am using php to access the database

Thanks

Fumigator
03-05-2007, 08:45 PM
HTML forms don't have an option for a true combo box (one with a drop-down list and a field to enter a value not in the list) but you can approximate this behavior with two separate input fields (a select input and a text input).

Do a google on "html combo box" for implementation ideas. (here's one (http://www.cs.tut.fi/~jkorpela/forms/combo.html))

As for the PHP side of things, you'll need to populate the list box based on the results of a query when the page loads. The query would be something like "select distinct(dept_value) from mytable".

bunny1
03-05-2007, 08:57 PM
Basically if i could put the values from the database in an array and then use the array to populate the combo.
But i just don't know how to do that.

Here is my php code and below is the html code.
My biggest problem is taking the php array and using it with the html

Any help would be much appreciated.
This is driving me mad!

<?php
$con = mysql_connect(server,username,password);
if (!$con)
{
die('Could not connect: ' . mysql_error());
echo "cannot connect";
}

mysql_select_db(db,$con);


$result = mysql_query("SELECT * FROM Prods") or die(mysql_error());

$var = 0
while($row = @mysql_fetch_array($result))
{
$array[$var]=$row['Department'];
$var = $var + 1;
}

mysql_close($con);
?>

<select name="department" size="1">
<option value= $var></option>
</select></td>

Fumigator
03-05-2007, 09:12 PM
Let's break it down into the two processes: Build the array, and Build the list box values.

Build the array out of a query using the "distinct" keyword.


$query = "SELECT DISTINCT(Department) FROM Prods";
$result = mysql_query($query);
for ($i = 0; $i < mysql_num_rows($result); $i++) {
$deptArray[$i] = mysql_result($result, 0);
}


All unique departments are now stored in $deptArray. To build the list box from this:


<select name="deptSelect">
<?php
foreach ($deptArray as $val) {
print "<option value=\"$val\">$val</option>\n";
}
?>
</select>

bunny1
03-05-2007, 09:30 PM
Thank you so much that works a treat.

Two questions -

the distinct is not working.any other reason why the same value in the database is appearing twice in the combo?

is it possible to have a field in the combo for adding a new value to the database or not?

thanks again for all the help

Fumigator
03-05-2007, 11:52 PM
the distinct is not working.any other reason why the same value in the database is appearing twice in the combo?


You'd have to post examples of your data and query, because "distinct" does in fact work for everyone else in the free world. More than likely you have different capitalization, white space (\n, \t, \r, etc) or other dirty data that is listing more unique values than you expect.


is it possible to have a field in the combo for adding a new value to the database or not?

So did you google it like I suggested? In a few seconds I found three different websites with methods of doing that.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum