Page 1 of 2 12 LastLast
Results 1 to 10 of 17
  1. #1
    Join Date
    Jun 2009
    Posts
    86
    Plugin Contributions
    0

    Default 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.

  2. #2
    Join Date
    Sep 2009
    Location
    Stuart, FL
    Posts
    13,385
    Plugin Contributions
    94

    Default 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;

  3. #3
    Join Date
    Jun 2009
    Posts
    86
    Plugin Contributions
    0

    Default Re: accessing array value in for loop

    Quote Originally Posted by lat9 View Post
    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($s1); 
                  
    $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

  4. #4
    Join Date
    Jul 2012
    Posts
    16,799
    Plugin Contributions
    17

    Default Re: accessing array value in for loop

    Quote Originally Posted by jeffiec View Post
    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($s1); 
                  
    $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...
    ZC Installation/Maintenance Support <- Site
    Contribution for contributions welcome...

  5. #5
    Join Date
    Dec 2009
    Location
    Amersfoort, The Netherlands
    Posts
    2,845
    Plugin Contributions
    25

    Default Re: accessing array value in for loop

    Write the values between quotes, and add a comma.
    PHP Code:
     //$emailArray 
     

    [
    0] => Array 
        ( 
            [
    email] => '[email protected]',
            [
    name] => 'test1' 
        


    [
    1] => Array 
        ( 
            [
    email] => '[email protected]',
            [
    name] => 'test2'
        

        ) 

  6. #6
    Join Date
    Jun 2009
    Posts
    86
    Plugin Contributions
    0

    Default Re: accessing array value in for loop

    Quote Originally Posted by mc12345678 View Post
    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.

    Jeff

  7. #7
    Join Date
    Jun 2009
    Posts
    86
    Plugin Contributions
    0

    Default Re: accessing array value in for loop

    Quote Originally Posted by Design75 View Post
    Write the values between quotes, and add a comma.
    PHP Code:
     //$emailArray 
     

    [
    0] => Array 
        ( 
            [
    email] => '[email protected]',
            [
    name] => 'test1' 
        


    [
    1] => Array 
        ( 
            [
    email] => '[email protected]',
            [
    name] => 'test2'
        

        ) 
    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($s1); 
                  
    $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.

  8. #8
    Join Date
    Dec 2009
    Location
    Amersfoort, The Netherlands
    Posts
    2,845
    Plugin Contributions
    25

    Default Re: accessing array value in for loop

    my bad, it is getting late over here.
    Quote Originally Posted by jeffiec View Post
    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($s1); 
                  
    $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.

  9. #9
    Join Date
    Jul 2012
    Posts
    16,799
    Plugin Contributions
    17

    Default Re: accessing array value in for loop

    Quote Originally Posted by jeffiec View Post
    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($s1); 
                  
    $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.
    ZC Installation/Maintenance Support <- Site
    Contribution for contributions welcome...

  10. #10
    Join Date
    Jun 2009
    Posts
    86
    Plugin Contributions
    0

    Default Re: accessing array value in for loop

    Quote Originally Posted by mc12345678 View Post
    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

 

 
Page 1 of 2 12 LastLast

Similar Threads

  1. help... Warning: Cannot use a scalar value as an array in cart_upsell.php
    By jamie in forum All Other Contributions/Addons
    Replies: 0
    Last Post: 12 Nov 2010, 12:15 AM
  2. Replies: 2
    Last Post: 10 Jun 2009, 12:24 PM
  3. Huh? Where did the value ARRAY come from?
    By dbr66 in forum Setting Up Categories, Products, Attributes
    Replies: 0
    Last Post: 3 Jun 2008, 02:39 AM
  4. Huh? Where did the value ARRAY come from?
    By dbr66 in forum Setting Up Categories, Products, Attributes
    Replies: 0
    Last Post: 30 May 2008, 07:18 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
disjunctive-egg
Zen-Cart, Internet Selling Services, Klamath Falls, OR