Page 1 of 1

Populate a combo box from a mysql database

Posted: Fri Mar 20, 2015 4:46 pm
by Feedrich
Hi I've been working on this for a project and now I have it working thought I would share to help others.

I wanted to populate a combobox with results from a mysql data base.

To begin with I placed this in a html block: (replace bold items with your data and for "stuff" replace with your column name from your database.
Its important this code is towards the front in the z order, or you can just put in the start of page html, but it must go BEFORE the actual combobox otherwise it wont populate it.

<?php
// select box open tag
$selectBoxOpen = "<select name='stuff'>";
// select box close tag
$selectBoxClose = "</select>";
// select box option tag
$selectBoxOption = '';

// connect mysql server
$con = mysql_connect("yourhostname","yourusername","yourpassword");
if (!$con) {
die('Could not connect: ' . mysql_error());
}

// select database
mysql_select_db("yourdatabase", $con);
// fire mysql query
$result = mysql_query("SELECT stuff FROM stuff");
// play with return result array
while($row = mysql_fetch_array($result)){
$selectBoxOption .="<option value = '".$row['stuff']."'>".$row['stuff']."</option>";
}
// create select box tag with mysql result
$selectBox = $selectBoxOpen.$selectBoxOption.$selectBoxClose;
?>


Next all you have to do is place your combobox in your form area right click and choose object html, select inside tag* and place the following:

<?php echo $selectBoxOption;?>

Voila all done, make sure your page extension is php and not html then publish. - Works great for me :D

Re: Populate a combo box from a mysql database

Posted: Fri Mar 20, 2015 4:48 pm
by BaconFries
Thanks for the share I am sure others will find it of use.

Re: Populate a combo box from a mysql database

Posted: Fri Jul 29, 2016 12:01 am
by ColinM
Hi Feedrich - Thank you very much indeed for putting so much effort into sharing and posting that - very much appreciated! :D 8)

Re: Populate a combo box from a mysql database

Posted: Fri Jul 29, 2016 6:57 pm
by Patrik iden
Thank's for this ColinM :)

Re: Populate a combo box from a mysql database

Posted: Mon Feb 20, 2017 9:02 pm
by tenori
I'm using MSSQL and have been able to adopt this to work with no problems.

What is this used for - $selectBox = $selectBoxOpen.$selectBoxOption.$selectBoxClose;
I've commented that line out and it seems to make no difference.

Also, I would like to use 2 combox boxes on my form, but I can't get the 2nd box to work. Any help is appreciated.

Re: Populate a combo box from a mysql database

Posted: Mon Feb 20, 2017 9:08 pm
by BaconFries
@tenori Please see forum rules if this section
Forum rules
"This section is to share tips, tricks and tutorials related to WYSIWYG Web Builder."
Please do not post questions or problems here. They will not be answered