View Single Post
  #2  
Old 10-13-2007, 06:51 AM
navaldesign's Avatar
navaldesign navaldesign is offline
General & Forum Moderator
 
Join Date: Oct 2005
Location: Italy
Posts: 9,419
Default Re: how to fill combobox with dynamic php variables ?

Your approach is correct. However, make sure that the html box is, in the page code flow, BEFORE the combobox (and the entire form) itself, otherwise the values don't exist yet when the combobox is created. If necessary right click on the html box and select Move to Back.

Now, if it doesn't work with the code

$option_one = "White";
$option_two = "Black";
$option_three = "Red";

placed in the Start of Page,

make sure that you have set your page to publish as php


Please also note that this approach is rather static. I mean, you have to define the options statically, and the number of options is static also. Usually, the dynamic population of comboboxes is used to populate it with values that are contained in a database table. Now, since these values can be continously changes, both as values and as number, you need a dynamic approach like the followingexample. Let's say that you have a form that uploads products into a cart database. During upload, you have to define which category the product belongs to. So, you have a table called "categories" in your database, and you have there all your categories. Now, when you upload a product, your script must look into this table, retrieve the category IDs and titles, and present them in the combobox as options. Here is what you do:
<?
require ("dbconnect.php");
$query = "SELECT categoryID, category_title FROM categories ORDER BY category_title ASC";
$result = mysql_query($query, $db);
while ($row = mysql_fetch_array($result)) {
$category_title[] = $row['category_title'];
$categoryID[] = $row['categoryID'];
}
?>
<select name="categoryID" size="1" id="Combobox1"
><?
$option = "<option value=\"Please select a Category\">Please select a Category</option> \n";
for ($i = 0; $i < count($category_title); $i++) {
$option .= "<option ";
$option .= "value=\"$categoryID[$i]\">$category_title[$i]</option> \n";
}
echo $option;
?>
</select>

As you can see, there is a first part in blue color. This one retrieves the category titles and IDs from the database (i have included here the code that is required to connect to the database, however, i do not have here the dbconnect file code. It is simply the code necessary to connect to the database)

Then there is a second part in blue, that is responsible for populating the combobox. The parts in black are autocreated by BV.

if you are using a BV form, then the first part in blue goes into the object (combobox) Before Tag and the second in the Inside Tag
__________________
Navaldesign
Logger Lite: Low Cost, Customizable, multifeatured Login script
Instant Download Cart: a Powerfull, Customized, in site, DB driven, e-products Cart
DBTechnosystems.com Forms, Databases, Shopping Carts, Instant Download Carts, Loggin Systems and more....
Advanced BlueVoda Form Processor : No coding form processor! Just install and use! Now with built in CAPTCHA!

Reply With Quote