Create comma separated email list

2 years 8 months ago #326152 by yeatea
Create comma separated email list was created by yeatea
Joomla! 3.10
CB 2.6.2
PHP 7.4.16
MySQL 5.7.32

I have a number of club members who can 'mentor' other members on a number of topics. Each 'mentor' may cover various topics, and each topic may have many mentors available..
When an ordinary club member registers for support I wish to email only those 'mentors' that can cover that topic. I can set up an auto action of type email, but wish to be able to derive the comma separated list of mentor email addresses for that topic for use in the CC field of the email..
I have tried using a CB Code type field with variations of the following code:

_____________________________________________________

// Prepare an empty array
$items = array();
// Prepare the database connection
$db = JFactory::getDbo();

// Run the SQL query and store it in $results
$db->setQuery("SELECT mentor_email FROM confreretestdb.diploma_mentor_subject");
$results = $db->loadObjectList();

// Now, load the array.
foreach ($results as $result) {
$email = $result->mentor_email;
$items[] = $email;
}

// Multiple values are to be separated by commas,
$items = implode(",", $items);

// Now return the value
return $items;

________________________________________________________________

However, this always results in an error (syntax error, unexpected end of file) or occasionally a fixed value of "Array".

I am not a programmer (the above code is copied from elsewhere), so is this the right way of creating my list, or is there a better way. If the above is a suitable method, can you advise where I am going wrong.

Please Log in to join the conversation.

2 years 8 months ago - 2 years 8 months ago #326154 by lousyfool
Replied by lousyfool on topic Create comma separated email list
If the complete name of the database table including prefix is really "diploma_mentor_subject", then try this:
$db = JFactory::getDbo();
$query = $db
	->getQuery(true)
	->select($db->quoteName('mentor_email'))
	->from($db->quoteName('diploma_mentor_subject'));
$db->setQuery($query);
$results = $db->loadColumn();
return implode(',', $results);

docs.joomla.org/Selecting_data_using_JDatabase
www.w3schools.com/php/func_string_implode.asp

EDIT:
By the way, if you chose "Query" instead of "PHP", you could get your comma-separated list also with this simple single-line query:
SELECT GROUP_CONCAT(`mentor_email`) FROM `diploma_mentor_subject`
www.mysqltutorial.org/mysql-group_concat/
The following user(s) said Thank You: krileon

Please Log in to join the conversation.

2 years 8 months ago #326161 by krileon
Replied by krileon on topic Create comma separated email list
Suggest just using a Query field provided by CB Query Field. You won't need any PHP for this. Example as follows.

Query:
SELECT GROUP_CONCAT( `mentor_email` ) FROM `diploma_mentor_subject`
Output: Single Row
Columns: Single Column

You can also let CB Query Field comma delimiter it or if you ever need a custom delimitered list, HTML formatting, etc.. the below is how you'd to that.

Query:
SELECT `mentor_email` FROM `diploma_mentor_subject`
Output: Multiple Rows
Row:
[column_mentor_email], 

Next just substitute that query field into the CC parameter of your CB Auto Actions.


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in to join the conversation.

2 years 8 months ago - 2 years 8 months ago #326162 by yeatea
Replied by yeatea on topic Create comma separated email list
Thanks 'lousy fool' for your guidance on the PHP solution, and Kyle for excellent description of the CB Query field solutions.

I now have 2 options I can use in future.

Very grateful to you both for the prompt responses.
The following user(s) said Thank You: nant, krileon

Please Log in to join the conversation.

Moderators: beatnantkrileon
Time to create page: 0.354 seconds

Facebook Twitter LinkedIn