Replace num with title in email placeholder

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

By JeffC - February 22, 2023

Hi

My booking form has a pulldown list field called 'reason'.

The 'reasons' are stored in tablename: reason
Use this field for option values: num
use this field for option labels: title

My placeholder uses the num in the email. How do I alter this so the title is sent.

Thanks in advance

This is what I have so far.

	// SendEmail
	$emailHeaders = emailTemplate_loadFromDB(array(
	'template_id'  => 'Booking Enquiry',
	'placeholders' => array(
	'reason'       => (is_array($_REQUEST['reason']))? implode(",", $_REQUEST['reason']): $_REQUEST['reason'] ,
	),
	));
Jeff

By Dave - February 24, 2023

Hi Jeff, 

That's a bit complicated.  Often what we try to do for things like this is have the code block separate that figures out the titles so we can more easily debug it on it's own.

It sounds like $_REQUEST['reason'] might have multiple values?  Is that true?  Try this code:

  // uncomment to test values
  //$_REQUEST['reason'] = [1,2,3];
  //$_REQUEST['reason'] = 2;
  
  // get Reason titles
  $reasonTitles = ""; 
  $records      = mysql_select('reasons');
  $numToTitle   = array_column($records, 'title', 'num');
  $reasonNums   = (array) $_REQUEST['reason']; // force to array if it's not already one
  foreach ($reasonNums as $num) {
    if ($reasonTitles) { $reasonTitles .= ", "; }
    $reasonTitles .= $numToTitle[$num] ?? "Unknown title #$num";
  }
  if (!$reasonTitles) { $reasonTitles = "No reasons selected"; } 
  
  // uncomment to show output
  //showme($reasonTitles);

Let me know if that works for you or if you have any questions.

Dave Edis - Senior Developer
interactivetools.com

By JeffC - February 25, 2023

Sorry Dave,

I've answered my own question. Probably should have done that before posting :)

'reason' => $reasonTitles,

Seems to do the trick. Unless you think there is a reason to not do it this way?

Jeff

By Dave - February 26, 2023

Yea, that's perfect!

When possible we like to make the code as readable and simple as possible as it makes future maintenance easier.

Dave Edis - Senior Developer
interactivetools.com