Forum Moderators: coopster

Message Too Old, No Replies

Edit form using drop down boxes populated from database

I am having a hard time figuring out how to populate the drop-down boxes wi

         

stevetremblay

7:02 pm on Aug 20, 2005 (gmt 0)

10+ Year Member



Hello all-

I have a form that uses drop-down boxes to save information to a MySQL database using a php script I wrote. That works fine, but the problem comes when I am trying to edit the information in the database. I have an "edit form" that pulls the values from the database and displays the fields in the fields accordingly. However, I am having a hard time figuring out how to populate the drop-down boxes with the value from the database. Basically, I want the "option selected" tag to be the database and be able to still have the rest of the options to select from. Does this make sense?

Thanks!
Steve

electricocean

7:49 pm on Aug 20, 2005 (gmt 0)

10+ Year Member



<select name="news">
<?php
$query ="";//you query
$result = mysql_result($query);
while ($row = mysql_fetch_array($result)){
$date = $row2['date'];
$news = $row2['news'];

echo "<option value="{$date}">{$news}</option>";
}
?>
<option value=""></option>
</select>

is this what you're looking for?

electricocean

stevetremblay

8:34 pm on Aug 20, 2005 (gmt 0)

10+ Year Member



electricocean,

I'm relatively new to PHP so please excuse me if I seem ignorant but it looks like this code you posted will go to a database and populate the drop-down box with the rows of a database. Is that correct?

Thanks,
Steve

electricocean

8:35 pm on Aug 20, 2005 (gmt 0)

10+ Year Member



yes thats exactly what will happen

stevetremblay

8:59 pm on Aug 20, 2005 (gmt 0)

10+ Year Member



electricocean,

First, I must say I appreciate your help with this but that's not what I'm trying to do. I re-read my post and it seems confusing so let me try to explain further. I have a database that keeps information about various equipment in my organization. To populate the database, I have a form that has various text fields and drop-down boxes. I then have a PHP script that takes those values and puts them into a database. That works great. However, the problem comes when I need to edit the data that was put into the database in that fashion. I have an edit form that pulls the values from the fields in the database and prints them as values into the text fields on the edit form but I need a way to present the values that were inserted into the database via a drop-down box in a drop-down box on the edit form. For instance, lets say I have a field in the database with a value of "Television" which was put in there by a drop-down box value with many other options like "VCR", "DVD Player", etc. I need to be able to show that "Television" is the option selected in the drop-down box on the edit form so I can see what the value is but I must be able to change it so that's why I need to have the drop down box with all of the other options.

Is this clearer?

Thanks!
Steve

Aleister

12:05 am on Aug 21, 2005 (gmt 0)

10+ Year Member



So,

If you selected "option1" in the drop-down box, and then typed in a value in a textfield next to it and clicked a button, you want it to update the "option1" field in the database with the value you entered?

stevetremblay

12:53 am on Aug 21, 2005 (gmt 0)

10+ Year Member



Hi Aleister,

Nope, all I want to do is have the drop-down menu show the value that's in the database field among the other options in that same drop-down menu. This is so I could see what is current value in the database before changing the option.

Thanks!
Steve

Aleister

3:00 am on Aug 21, 2005 (gmt 0)

10+ Year Member



Can I see the code you have now, as well as the names of the fields in your database?

It sounds like you just need to extract both the name and the value from the database, and when generating the drop down box, do it like this:

echo "<option value=\"whatever\">" . $the_item " = " . $the_value . "</option>";

That way it displays the value alongside the item name.

Unless I still do not know exactly what you mean :)

henry0

11:08 am on Aug 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A few weeks ago I made a little contrib

Review the thread
[webmasterworld.com]
Scroll down the thread to find my script.

dreamcatcher

1:15 pm on Aug 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




$query = mysql_query("SELECT * FROM table") or die(mysql_error());

while ($row = mysql_fetch_assoc($query))
{
echo '<select name="dropdown">';
echo '<option'.($row['name']=="TV"? ' selected' : '').'>TV</option>';
echo '<option'.($row['name']=="Video"? ' selected' : '').'>Video</option>';
echo '<option'.($row['name']=="Car"? ' selected' : '').'>Car</option>';
echo '<option'.($row['name']=="House"? ' selected' : '').'>House</option>';
echo '</select>';
}

Try that. Or are you just pulling a single entry? In which case this would do:


$query = mysql_query("SELECT * FROM table WHERE id = 'id'") or die(mysql_error());
$row = mysql_fetch_object($query);

echo '<select name="dropdown">';
echo '<option'.($row->name=="TV"? ' selected' : '').'>TV</option>';
echo '<option'.($row->name=="Video"? ' selected' : '').'>Video</option>';
echo '<option'.($row->name=="Car"? ' selected' : '').'>Car</option>';
echo '<option'.($row->name=="House"? ' selected' : '').'>House</option>';
echo '</select>';

Best if you post your code.

dc

stevetremblay

6:31 pm on Aug 21, 2005 (gmt 0)

10+ Year Member



Hi dreamcatcher,

I think your first example is just what I'm looking for. Does it compare the value from the database with the options in the select box then display the one which matches as the option selected?

Thanks,
Steve

dreamcatcher

6:33 pm on Aug 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



yep, it should do. ;)

Whats been used is whats called a Ternary Operator.

dc

stevetremblay

7:51 pm on Aug 21, 2005 (gmt 0)

10+ Year Member



Thank you, dreamcatcher, you provided me with the solution to my problem. Now here's just an add on I'm sure you'll be able to solve quickly. Eventually, I'll want to pull ALL the options from a database so if I get another piece of equipment, I won't have to edit all the hard coded values in all of my pages. So basically I (or anyone else) would be able to go to some administration page and add equipment to a table and then I would have a drop down box populate from the table. Would this still work with that?

Steve

dreamcatcher

9:28 pm on Aug 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Steve,

It will indeed work ok for that, you would need a couple of loops and you would be good to go. You might also want to think of storing your options in array too. Easier to add another option to an array than keep going through your pages and editing HTML.

$info = array('TV','Video','Car', 'House');

$query = mysql_query("SELECT * FROM table") or die(mysql_error());

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

echo '<select name="dropdown">';

foreach ($info as $data)
{
echo '<option'.($row['name']==$data? ' selected' : '').'>'.$data.'</option>';
}

echo '</select>';

}

Good luck.

:)

stevetremblay

12:53 pm on Aug 23, 2005 (gmt 0)

10+ Year Member



Thank you, once again, dreamcatcher!

Steve

dreamcatcher

1:52 pm on Aug 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You`re welcome my friend.

:)

Sarah Atkinson

3:24 pm on Aug 23, 2005 (gmt 0)

10+ Year Member



how do you have the options in your database? are they verriable text, text, int, enum or set?

Or you could have another table that consists of a list of all of your options and then cross conect each item with the id#.

I don't know if there is away to call all the posibilities of an enum in a database. seems like their should be.

Sarah Atkinson

3:47 pm on Aug 23, 2005 (gmt 0)

10+ Year Member



Just looked it up and you can use the DESCRIBE statment with a mysql query to get a list of the posible choices. then jsut fetch array and get it from the 'type' part of the array.

then put each one of those into an array then use a while statment to plug them into your html form.