Create pick list from multiple fields

6 posts by 2 authors in: Forums > CMS Builder
Last Post: February 26, 2013   (RSS)

Hi Guys,

I have a section called "Project" and one the the fields is a select list for "Contact Name" that gets options from the "Contacts" table.  However, the contacts fields are "first_name" and "last_name". 

How do I get the drop list to show "Fred Blogs" rather than just "Blogs", because I may have multiple contacts with that last name.

Thanks!

Cheers,

Tim (toledoh.com.au)

Hi Tim, 

If you edit your list field, and change the field type to "Get options from MySQL query". Then you can use the MySQL concat function to combine two fields into one like this:

SELECT num, CONCAT(first_name, ' ', last_name) fullname
  FROM `<?php echo $TABLE_PREFIX ?>project`

This is example code, so you might need to change some of the variable names.

The field will use the num value for its value, and use the fullname meta field for its label.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

That's great!

Is there also a way to add to this: where category=option1, so the list only shows contacts within the option1 category?

Cheers,

Tim (toledoh.com.au)

Hi Tim, 

You can do this by adding the where statement to the end of the current MySQL statement:

SELECT num, CONCAT(first_name, ' ', last_name) fullname
FROM `<?php echo $TABLE_PREFIX ?>project`
WHERE category = `option1`

Let me know if you have any questions.

Thanks

Greg

Greg Thomas







PHP Programmer - interactivetools.com

Hi Greg,


I keep getting errors with this;  I'm 100% positive that I have a list field called "type" in the "contacts" table.

SELECT num, CONCAT(first_name, ' ', last_name) fullname
FROM `<?php echo $TABLE_PREFIX ?>contacts`

works perfectly; however

SELECT num, CONCAT(first_name, ' ', last_name) fullname
FROM `<?php echo $TABLE_PREFIX ?>contacts`
WHERE type = `Person`

gives an error "Warning: Cannot modify header information - headers already sent by (output started at /home/toledoh1/public_html/cmsAdmin/lib/menus/header.php:13) in /home/toledoh1/public_html/cmsAdmin/lib/database_functions.php on line 554 There was an error creating the list field 'contact_person'. MySQL Error: Unknown column 'type = Person' in 'where clause'"

Cheers,

Tim (toledoh.com.au)