Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-05-2007, 05:26 PM   PM User | #1
bunny1
New Coder

 
Join Date: Feb 2007
Posts: 92
Thanks: 1
Thanked 0 Times in 0 Posts
bunny1 is on a distinguished road
combo box

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
bunny1 is offline   Reply With Quote
Old 03-05-2007, 08:45 PM   PM User | #2
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
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)

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".
__________________
Fumigator is offline   Reply With Quote
Old 03-05-2007, 08:57 PM   PM User | #3
bunny1
New Coder

 
Join Date: Feb 2007
Posts: 92
Thanks: 1
Thanked 0 Times in 0 Posts
bunny1 is on a distinguished road
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>
bunny1 is offline   Reply With Quote
Old 03-05-2007, 09:12 PM   PM User | #4
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
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.
PHP Code:
$query "SELECT DISTINCT(Department) FROM Prods";
$result mysql_query($query);
for (
$i 0$i mysql_num_rows($result); $i++) {
    
$deptArray[$i] = mysql_result($result0);

All unique departments are now stored in $deptArray. To build the list box from this:
PHP Code:
<select name="deptSelect">
<?php
foreach ($deptArray as $val) {
    print 
"<option value=\"$val\">$val</option>\n";
}
?>
</select>
__________________
Fumigator is offline   Reply With Quote
Old 03-05-2007, 09:30 PM   PM User | #5
bunny1
New Coder

 
Join Date: Feb 2007
Posts: 92
Thanks: 1
Thanked 0 Times in 0 Posts
bunny1 is on a distinguished road
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
bunny1 is offline   Reply With Quote
Old 03-05-2007, 11:52 PM   PM User | #6
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
Quote:
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.

Quote:
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.
__________________
Fumigator is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:20 PM.


Advertisement
Log in to turn off these ads.