Replace num with title in email placeholder

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

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

Thanks Dave

The code block works – when I uncomment the code I get the titles for each of the records – but how to I get output to send in my email?

Is it as simple as changing:

'reason' => (is_array($_REQUEST['reason']))? implode(",", $_REQUEST['reason']): $_REQUEST['reason'] ,

to

'reason' => $reasonTitles,
Jeff

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