Populate a combo box from a mysql database

Do you want to share WYSIWYG Web Builder tips, tricks, tutorials or useful HTML code? You can post it here...
(no questions or problems please, this section is not monitored by support).
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.

PLEASE READ THE FORUM RULES BEFORE YOU POST:
viewtopic.php?f=12&t=1901
Post Reply
Feedrich
 
 
Posts: 5
Joined: Thu Nov 21, 2013 8:06 pm

Populate a combo box from a mysql database

Post 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
User avatar
BaconFries
 
 
Posts: 5619
Joined: Thu Aug 16, 2007 7:32 pm

Re: Populate a combo box from a mysql database

Post by BaconFries »

Thanks for the share I am sure others will find it of use.
User avatar
ColinM
 
 
Posts: 917
Joined: Wed Feb 09, 2011 3:40 am
Location: Western Australia

Re: Populate a combo box from a mysql database

Post by ColinM »

Hi Feedrich - Thank you very much indeed for putting so much effort into sharing and posting that - very much appreciated! :D 8)
Yours truly
Colin M
Western Australia
User avatar
Patrik iden
 
 
Posts: 479
Joined: Wed Mar 24, 2010 9:07 pm
Location: Sweden

Re: Populate a combo box from a mysql database

Post by Patrik iden »

Thank's for this ColinM :)
tenori
 
 
Posts: 1
Joined: Fri Dec 30, 2016 5:53 pm

Re: Populate a combo box from a mysql database

Post 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.
User avatar
BaconFries
 
 
Posts: 5619
Joined: Thu Aug 16, 2007 7:32 pm

Re: Populate a combo box from a mysql database

Post 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
Post Reply