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: 13
Joined: Thu Nov 21, 2013 8:06 pm

Populate a combo box from a mysql database

Post by Feedrich » Fri Mar 20, 2015 4:46 pm

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: 4354
Joined: Thu Aug 16, 2007 7:32 pm

Re: Populate a combo box from a mysql database

Post by BaconFries » Fri Mar 20, 2015 4:48 pm

Thanks for the share I am sure others will find it of use.

hurtman
 
 
Posts: 48
Joined: Sun Oct 26, 2014 1:17 pm

Re: Populate a combo box from a mysql database

Post by hurtman » Thu Jul 28, 2016 11:35 pm

Thanks. This is a great example and easy to follow. When I used this example, all items are listed down the page but the combobox does not list the first item. Any thoughts?

I was able to figure out the problem. I'm not sure if it had anything to do with the problem I encountered but I was using a SELECT DISTINCT query. The problem was that web builder seemed to be missing a "<" when the html code was generated.

Instead of

<?php echo $selectBoxOption;?>

I had to use this

>
<?php echo $selectBoxOption;?>
Last edited by hurtman on Fri Jul 29, 2016 1:27 am, edited 2 times in total.

User avatar
ColinM
 
 
Posts: 1059
Joined: Wed Feb 09, 2011 3:40 am
Location: Western Australia
Contact:

Re: Populate a combo box from a mysql database

Post by ColinM » Fri Jul 29, 2016 12:01 am

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
FORUM MODERATOR
Please think before you post, be polite, don't hijack threads and above all please respect Pablo's huge effort!
My Website

User avatar
Patrik iden
 
 
Posts: 653
Joined: Wed Mar 24, 2010 9:07 pm
Location: Sweden

Re: Populate a combo box from a mysql database

Post by Patrik iden » Fri Jul 29, 2016 6:57 pm

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 » Mon Feb 20, 2017 9:02 pm

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: 4354
Joined: Thu Aug 16, 2007 7:32 pm

Re: Populate a combo box from a mysql database

Post by BaconFries » Mon Feb 20, 2017 9:08 pm

@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

omarcb
 
 
Posts: 3
Joined: Tue Jul 25, 2017 3:01 am

Re: Populate a combo box from a mysql database

Post by omarcb » Thu Jul 27, 2017 9:34 pm

Thanks a lot dear friend Feedrich !
You save a lot of time to me.

I have two problems.

2) mysql_commands was deprecated
1) My tongue language is español with á,é,í,ü,ñ, etc. characters

I must change small things and works fine for me.

1) After connect db, and before select db I put this:
mysqli_query($con,"SET NAMES 'utf8'");
to correct problems with codepage in utf-8

2) This is the final code with mysqli:

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

// connect mysql server
$con = mysqli_connect("server.com","user","password");
if (!$con) {
die('Could not connect: ' . mysqli_error());
}
mysqli_query($con,"SET NAMES 'utf8'");

// select database
mysqli_select_db($con,"database");
// fire mysql query
$result = mysqli_query($con,"SELECT field_name FROM table_name");
// play with return result array
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
$selectBoxOption .="<option value = '".$row['field_name']."'>".$row['field_name']."</option>";
}
// create select box tag with mysql result
$selectBox = $selectBoxOpen.$selectBoxOption.$selectBoxClose;
?>

Again...Thank you very much.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest