PDA

View Full Version : Dropdown Problem



stedem
May 27th, 2008, 11:07 AM
Hello

I'm busy creating a small cms for my personal site. In the cms i have a dropdown list with some options in but I want this list to auto update instead of hardcoded.

I have 2 problems

Problem 1:
How can I link the dropdown list to a database? For example if i have a table DROPDOWNVALUE with 5 values in. How can i let my dropdown list auto show these values. If i add an extra item to the table this also should appear in this list and must be selectable.

Problem 2:
Can I have this dropdown value to auto update to the value stored in the database products. For example. I have a dropdown with the values "flowers, animals or gifts". If I edit a product with the value "flowers" the dropdown should be automatically select "flowers" even if this is the 3th value of the dropdownlist.

Thanks!

ahmednuaman
May 27th, 2008, 01:12 PM
You can use PHP to get your data from the database and do almost anything with it, including writing a drop down box. How much PHP and MySQL do you know?

stedem
May 27th, 2008, 02:03 PM
I can do some things in php and sql but this thing i didn't figure out.
I have a small own created webshop with small cms.

I can add , edit and delete products but the dropdown is the only thing that doesn't works :)

ahmednuaman
May 27th, 2008, 03:11 PM
Ok, so assuming that you've connected to your DB already, let's do some code:


$query = mysql_query('SELECT * FROM TableThatContainsStuff');

// check that there's some results
if (mysql_num_rows($query) > 0) {

// begin the drop down
echo '<select name="my_first_dropdown">';

// run loop
while (($row = mysql_fetch_object($query)) !== false) {

// begin option
echo '<option value="'.$row->Value.'"';

// now we see if the selected value of '$selected' is relative to any of these options
if ($selected == $row->Value) { echo ' selected="selected"'; }

// finish up
echo '>'.$row->Name.'</option>';

}

}


That's pretty much it

djheru
May 27th, 2008, 03:18 PM
what he said

djheru
May 27th, 2008, 03:20 PM
DOH! you beat me by 7 minutes

ahmednuaman
May 27th, 2008, 03:53 PM
My bad :)

stedem
May 27th, 2008, 03:55 PM
Thanks,

I'm going to try this tomorrow, first going to sleep, tomorrow early up to catch a plane
I'll keep you informed if i have a problem or question

ahmednuaman
May 28th, 2008, 05:15 AM
Ok, run a *AMP stack on your machine for testing on the plane!