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
Re: accessing array value in for loop
A "Web page" location wouldn't help anyways, but to be sure it is understood. If in the exact place where you have the for loop, you instead assign the $email_array such that $email_array[0] has data, then it works the rest of the way through?
Do you do an assignment to $email_array to declare it before the for loop? Like:
Code:
$email_array = array();
If the assignment of $email_array[0] related data is happening just before the processing of the email, then of course it will operate. The situation here is better "explained" by providing the code through which all this is processed or identifying things like where a function "group" exists if any, or that no function block exists between the two. Or, and going out on a limb, just before the email generation loop, like the line before, if you add global $email_array; might it fix the issue?
Re: accessing array value in for loop
Quote:
Originally Posted by
jeffiec
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.
Did you echo $x too? What you describe sounds like $x is something unexpected.
And what about $snd_cnt ?
Re: accessing array value in for loop
Quote:
Originally Posted by
DrByte
Did you echo $x too? What you describe sounds like $x is something unexpected.
And what about $snd_cnt ?
Yes, i echo'd them both, $x is empty, while $snd_cnt has the expected value.
I haven't tried adding the global yet, will try that tomorrow and let you know if it solves it.
Re: accessing array value in for loop
Quote:
Originally Posted by
jeffiec
Yes, i echo'd them both, $x is empty, while $snd_cnt has the expected value.
I haven't tried adding the global yet, will try that tomorrow and let you know if it solves it.
If $x is empty then you've got even bigger problems, assuming you're echoing $x from inside the "for ($x=0; ...$x++)" loop.
Re: accessing array value in for loop
Possibly the variable $x is getting clobbered somewhere else, if you're only showing part of your code? Maybe don't use array indexes - try this instead:
Code:
foreach ($email_array as $email_addy) {
$send_to_name = $email_addy['name'];
$send_to_email = $email_addy['email'];
...
}
Re: accessing array value in for loop
It seems to be going through the loop as i get errors that it cant send to "" blank field the correct number of times.
I still haven't gotten around to trying to use $global yet. I have another issue i am making a post about before i can get back on this issue lol.
Re: accessing array value in for loop
Well ended up rewriting the page and it works now, must have been some loose code somewhere, mark this resolved.
Thanks a ton
Jeff