Creating Variables

13 posts by 3 authors in: Forums > CMS Builder
Last Post: September 25, 2014   (RSS)

By gkornbluth - September 14, 2014

Hi All,

I'm having some trouble creating variables from information pulled from records in the User Account table.

I'm using that information to choose which user's email address is to be used to send emails to various board members and committee chairs.

The visitor can then send the output of the form to the appropriate email address by selecting an option from a pull down field

So Far...

As a test, I’ve been able to get the form working by creating a series of variables for the various board positions in a single record editor.

Like this:

<?php
$president = $board_of_directors_informationRecord['president_email'];
$vice_president = $board_of_directors_informationRecord['vice_president_email'];
$secretary = $board_of_directors_informationRecord['secretary_email'];
$treasurer = $board_of_directors_informationRecord['treasurer_email'];
?>

and then using if statements that match the $reason field options in the mail form with the variable values to sort them out.

Like This:

if ($FTGreason == "President") {
 
  $emailTo = $president;

if ($FTGreason == "Secretary") {
 
  $emailTo = $secretary;

etc.

My goal

My goal is to be able to assign the email recipient from a combination of a pull down:
(board_of_director_position_1,  board_of_director_position_2,  board_of_director_position_3)

and check box
(board_of_director_position_1_email , board_of_director_position_2_email, board_of_director_position_3_email)

in the member’s user record, and I’m up to the last step

I was able to generate a list of positions and email addresses that match the format of the above variables.

Like This:

<?php foreach ($directorposition as $positiona): ?>
<?php foreach ($accountsRecords as $record): ?>
<?php if ($record['board_of_director_position_1:label'] == $positiona || $record['board_of_director_position_2:label'] == $positiona || $record['board_of_director_position_3:label'] == $positiona) : // is there a position selected ?>
<?php $positionc = strtolower($positiona) ; // format the position ?>
<?PHP $positionc = preg_replace('/\s+/', '_', $positionc ); ?>
<?PHP $positionc = preg_replace('/-/', '_', $positionc ); ?>
<?php if ($record['board_of_director_position_1:label'] == $positiona && $positiona && $record['board_of_director_position_1_email'] == '1'): // is this records email address the one to use ?>
<?php $positionb = $record['email'] ?>
<?php endif ?>
<?php if ($record['board_of_director_position_2:label'] == $positiona && $positiona && $record['board_of_director_position_2_email'] == '1'): ?>
<?php  $positionb = $record['email']?>
<?php endif ?>
<?php if ($record['board_of_director_position_3:label'] == $positiona && $positiona && $record['board_of_director_position_3_email'] == '1'): ?>
<?php $positionb = $record['email']?>
<?php endif ?>
<?php '$'; ?><?php $positionc ?> <?php  '='; ?> <?php $positionb ?><?php  '; '  // This is the code that is supposed to create the variables ?>;
<?php endif ?>
<?php endforeach ?>
<?php endforeach ?>

I’m having no luck converting that output into actual variables, and hope that someone has an idea or two.

The output I’m looking for is a series of variables like:

$president = thepresidentsemailaddress@comcast.net;
$vice_president = thevicepresidentsemail address@gmail.com;
$secretary = thescrretarysemail@hotmail.com;
etc.

I’ve attached the code I have so far. (line 44 is supposed to create the variables and lines 479, 493 etc. are supposed to use them)

Thanks for looking,

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By claire - September 19, 2014

Yeah, this looks like it could be a problem with the variable names getting mixed up.

Jerry, can you confirm that the code is doing the right thing?

--------------------

Claire Ryan
interactivetools.com

Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

By gkornbluth - September 20, 2014

Hi Claire, Equinox,

Thanks to your suggestions, I think I’ve sorted out the variable logic and have renamed the variables so that they are less confusing to me and to everyone else.

I can get a listing of the created variables by echoing the values in the line:

<?php '$'; ?><?php $formatted_position ?><?php '='; ?><?php $email_to_use ; ?><?php ';' ; // This is the code that is supposed to create the variables ?>

They are all correct, and render like this:

$president=presidents_real_email@comcast.net;$vice_president=vice_presidents_real_email@gmail.com;$secretary= secretarys_real_email@hotmail.com; etc.

However I’m not sure if these variables are ever fed to the $emailTo in the email header, based on the selected $FTGreason option in the form, and haven’t had any luck trying to determine that.

Here’s the new cleaned up code to check for existing positions and email assignments, and to create the variables:

<?php list($board_of_director_positionsRecords, $board_of_director_positionsMetaData) = getRecords(array(
    'tableName'   => 'board_of_director_positions',
   ));
     $directorposition = array_filter(array_pluck($board_of_director_positionsRecords, 'position'));
   list($accountsRecords, $accountsMetaData) = getRecords(array(
    'tableName'   => 'accounts',
'where' => "board_of_director_position_1_email  LIKE '%1%' OR  board_of_director_position_2_email  LIKE '%1%' OR board_of_director_position_3_email  LIKE '%1%' ",
  ));
    ?>
<?php foreach ($directorposition as $actual_position): ?>
<?php foreach ($accountsRecords as $record): ?>
<?php if ($record['board_of_director_position_1:label'] == $actual_position || $record['board_of_director_position_2:label'] == $actual_position || $record['board_of_director_position_3:label'] == $actual_position): // is there a position selected  ?>
<?php $formatted_position = strtolower($actual_position); // format the position ?>
<?PHP $formatted_position = preg_replace('/\s+/', '_', $formatted_position ); ?>
<?PHP $formatted_position = preg_replace('/-/', '_', $formatted_position ); ?>
<?php if ($record['board_of_director_position_1:label'] == $actual_position && $actual_position && $record['board_of_director_position_1_email'] == '1'): // is this records email address the one to use  ?>
<?php $email_to_use = $record['email']?>
<?php endif ?>
<?php if ($record['board_of_director_position_2:label'] == $actual_position && $actual_position && $record['board_of_director_position_2_email'] == '1'): ?>
<?php  $email_to_use = $record['email']?>
<?php endif ?>
<?php if ($record['board_of_director_position_3:label'] == $actual_position && $actual_position && $record['board_of_director_position_3_email'] == '1'): ?>
<?php $email_to_use = $record['email']?>
<?php endif ?>
<?php '$'; ?><?php $formatted_position ?><?php '='; ?><?php $email_to_use ; ?><?php ';' ; // This is the code that is supposed to create the variables ?>
<?php endif ?>
<?php endforeach ?>
<?php endforeach ?>

And here’s an example of the code that’s supposed to send the email to the selected position:

 if ($FTGreason == "President") {
 
  $emailTo = $president;
   
  $emailFrom = FilterCChars("$FTGemail");
   
  $emailHeader = "From: $emailFrom\n"
   . "MIME-Version: 1.0\n"
   . "Content-type: text/plain; charset=\"UTF-8\"\n"
   . "Content-transfer-encoding: 8bit\n";
   
  mail($emailTo, $emailSubject, $emailBody, $emailHeader);
 
 }

I've attached the latest version of the complete set of pages so that you can see the code in context.

Thanks again for all of this.

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By claire - September 24, 2014

Hi Jerry

I suspect this has something to do with the FTGreason variable. Just before you get to the check if($FTGreason == "President"), add the following code to see if the variables are being processed correctly:

var_dump($FTGreason);
var_dump("President");
die;

What you should see is some information like string(9) "President". The var_dump will show you what FTGreason actually is just before it hits this check, and it won't pass and execute the email code if it doesn't match what you expect it to be. This is by far the most common reason that if-statements stop working, and I've been caught by it so many times it's the first thing I look at when troubleshooting.

The die statement will obviously kill the script so it'll output the var_dump without processing any further.

--------------------

Claire Ryan
interactivetools.com

Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

By gkornbluth - September 24, 2014

Hi Claire,

Thanks for the tip.

Here's the code I inserted after the if statement in apbc_master_form_BOD13_php.php (I added  var_dump ($president);):

 if ($FTGreason == "President") {
 
  $emailTo = $president;
   var_dump($FTGreason);
var_dump("President");
var_dump ($president);
die;

And here's what I got back

string(9) "President" string(9) "President" NULL

So it seems that the $FTGreason is as expected.

Problem is, I don't have any idea why the $president variable is NULL, unless the variables are not being created at all.

Any chance you can take a closer look?

Would you need FTP access?

Thanks,

Jerry

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By claire - September 24, 2014

It might be more simple than you think. I looked through the code, and there's only one reference to $president:

$emailTo = $president;

So clearly it's not being set anywhere. Is it a global variable?

The mail function will fail without a valid email address, obviously enough.

--------------------

Claire Ryan
interactivetools.com

Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

By gkornbluth - September 24, 2014 - edited: September 24, 2014

Thanks for looking at this Claire,

The variables are supposed to be set in the lines preceding 36 and if I add echo to the various components of line 33 I can see a set of variables that is correct. (but of course it also breaks the page)

The issue must lie in my attempt to create a series of variables line 33, but I don't know what it is.

Jerry

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By claire - September 24, 2014 - edited: September 24, 2014

Jerry, I'm looking at this now, and I've just realized what you're trying to do on line 33. That's never going to work - you need variable variables, which is, eh, maybe a little tricky to get handle on.

At the moment all you're doing is outputting a string, which PHP will produce but which won't actually set anything.

Try changing the line as follows:

<?php $$formatted_position = $email_to_use; ?>

The extra $ is not a typo. This may or may not work. It's supposed to create a variable using the name stored in $formatted_position and assign it the value in $email_to_use.

--------------------

Claire Ryan
interactivetools.com

Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

By gkornbluth - September 24, 2014 - edited: September 24, 2014

Sorry, Claire,

That produced the same string(9) "President" string(9) "President" NULL

Latest code is attached

Hope there's a solution to this...

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php