accessing array value in for loop
I am building an array in loop and the results below it for testing purposes:
PHP Code:
$email_array[]= array(
'email' => $fnd_result->fields['email'],
'name' => $fnd_result->fields['name']
);
//$emailArray
(
[0] => Array
(
[email] => test1@test.com
[name] => test1
)
[1] => Array
(
[email] => test2@test.com
[name] => test2
)
)
$snd_cnt = count($email_array);
then on an event this function is triggered:
PHP Code:
for ($x = 0; $x < $snd_cnt; $x++){
$send_to_name = $email_array[$x]['name'];
$send_to_email = $email_array[$x]['email'];
$email_subject = "Your Forklift Has Arrived!";
// Prepare Text-only portion of message
$text_message = OFFICE_FROM . "\t" . $bname . "\n" .
'As you requested, we are notifying you that we have a new forklift in our inventory' . "\n" ."\n" .
'We added a: ' . "\n" ."\n" .
'Year: ' . $forklift_year . "\n" .
'Make: ' . $forklift_make . "\n" .
'Model: ' . $products_model . "\n" .
'Please visit our website at ojlforklifts.com or call us at (305) 836-4337 ' ."\n" ."\n" ."\n" .
$extra_info['TEXT'];
// Prepare HTML-portion of message
$html_msg['EMAIL_GREETING'] = $email_subject;
$html_msg['EMAIL_WELCOME'] = 'We recieved a Forklift within your specs.';
$html_msg['EMAIL_FIRST_NAME'] = $send_to_name;
$html_msg['EMAIL_MESSAGE_HTML'] = '<table> ' .
'<tr><td>Make: </td> <td>' . $forklift_make . '</td></tr>' .
'<tr><td>Capacity: </td> <td>' . $forklift_capacity . 'Lbs</td></tr>' .
'<tr><td>Fuel: </td> <td>' . $fuelM . '</td></tr>' .
'<tr><td>Tires: </td> <td>' . $tireM . '</td></tr>' .
'<tr><td>Price: </td> <td>' . $products_price . '</td></tr>' .
'<tr><td colspan="2"><img src="http://ojlforklifts.com/images/' . $products_image . '" width="350"/></td></tr>' .
'</table>';
zen_mail($send_to_name, $send_to_email, $email_subject, $text_message, $name, $email_address, $html_msg ,'fork_notify');
}
$send_to_email always returns empty
$send_to_name always returns empty
So the problem is that i cant access the 2 variables in the loop. If I echo $email_array[0]['email'] with the index hardcoded, it will give me the result [email protected], but the minute i put put in a variable for the index, $email_array[$x]['email'], the result is empty.
Can anyone point me in the right direction? I even tried the a while loop and it fails also.
Re: accessing array value in for loop
Is the $email_array generated in the same file as the event is handled?
If the "event" is handled via a notifier, then that handling is being performed in a class-function and you might need to declare the $email_array as a global at the top of that handling function:
Code:
global $email_array;
Re: accessing array value in for loop
Quote:
Originally Posted by
lat9
Is the $email_array generated in the same file as the event is handled?
If the "event" is handled via a notifier, then that handling is being performed in a class-function and you might need to declare the $email_array as a global at the top of that handling function:
Code:
global $email_array;
yes, it is on the same page, that was my question too.
Iuse this bit of code to make sure the email array is on the page:
PHP Code:
if (!function_exists("preprint")) {
function preprint($s, $return=false) {
$x = "<pre style=\"color:#000\">";
$x .= print_r($s, 1);
$x .= "</pre>";
if ($return) return $x;
else print $x;
}
}
preprint($send_to_email);
preprint($email_cnt);
the array is there, and the count is there, but the variables are not, that's why this is driving me crazy lol
Re: accessing array value in for loop
Quote:
Originally Posted by
jeffiec
yes, it is on the same page, that was my question too.
Iuse this bit of code to make sure the email array is on the page:
PHP Code:
if (!function_exists("preprint")) {
function preprint($s, $return=false) {
$x = "<pre style=\"color:#000\">";
$x .= print_r($s, 1);
$x .= "</pre>";
if ($return) return $x;
else print $x;
}
}
preprint($send_to_email);
preprint($email_cnt);
the array is there, and the count is there, but the variables are not, that's why this is driving me crazy lol
Page and file are two different things...
Re: accessing array value in for loop
Write the values between quotes, and add a comma.
Re: accessing array value in for loop
Quote:
Originally Posted by
mc12345678
Page and file are two different things...
yes, understand, the file name is cust_notify.php that includes cust_notify_header.php in the include/modules.
I may have forgotten to mention this is in the admin side :S
But yes, i have an understanding of many files per page concept. :D
Jeff
Re: accessing array value in for loop
Quote:
Originally Posted by
Design75
Write the values between quotes, and add a comma.
that is the output of the code:
PHP Code:
if (!function_exists("preprint")) {
function preprint($s, $return=false) {
$x = "<pre style=\"color:#000\">";
$x .= print_r($s, 1);
$x .= "</pre>";
if ($return) return $x;
else print $x;
}
}
preprint($send_to_email);
that just so i know for sure the array actually has something in it.
Re: accessing array value in for loop
:laugh: :oops: my bad, it is getting late over here.
Quote:
Originally Posted by
jeffiec
that is the output of the code:
PHP Code:
if (!function_exists("preprint")) {
function preprint($s, $return=false) {
$x = "<pre style=\"color:#000\">";
$x .= print_r($s, 1);
$x .= "</pre>";
if ($return) return $x;
else print $x;
}
}
preprint($send_to_email);
that just so i know for sure the array actually has something in it.
Re: accessing array value in for loop
Quote:
Originally Posted by
jeffiec
that is the output of the code:
PHP Code:
if (!function_exists("preprint")) {
function preprint($s, $return=false) {
$x = "<pre style=\"color:#000\">";
$x .= print_r($s, 1);
$x .= "</pre>";
if ($return) return $x;
else print $x;
}
}
preprint($send_to_email);
that just so i know for sure the array actually has something in it.
Where in relation to using the data is this output array? Going back to the concept presented by lat9, is the code area activated "by an event" found in a function in which the array is built, is the array built outside of the function, or is the usage of the array even in a function at all?
Ie. How does all this code tie together, because right now it is very cryptic.
Re: accessing array value in for loop
Quote:
Originally Posted by
mc12345678
Where in relation to using the data is this output array? Going back to the concept presented by lat9, is the code area activated "by an event" found in a function in which the array is built, is the array built outside of the function, or is the usage of the array even in a function at all?
Ie. How does all this code tie together, because right now it is very cryptic.
because its in the admin section i cant link it, basically there is a button the owner can click a button that reloads to itself with an action=sendmail,
then when the page reloads, the action is detected and the code above is fired, sending emails to a customer list retrieved from the database and stored in email_array.
If i take it out of the for loop, set $email_array[0]['email'], it works as expected, the email goes out just fine, but then because there may be several emails in the array, i put it in a for loop, using count($email_array), and try to get just the current email and name, ie... $email_array[$x]['email'], i get a big fat nothing, no php errors except the email doesn't know who to send to and fails.
ERROR: Failed sending email to: "" <> with subject: "Your Forklift Has Arrived!" (failed validation)
Thanks a ton.
Jeff