Re: multiple foreach loop?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Quoting Aaron Wolski <aaronjw@martekbiz.com>:

> Hi All,
>  
> Trying to create a "refer a friend" script.
>  
> User enters 10 names and email address of a friend to refer to a site.
>  
> The following code SORT of works in that it will send the email, etc..
> but problem is it duplicates - sends the email out twice to the same
> email address.
>  
> **CODE***
>  
> foreach($nameRefered as $referedName) {
>  
>             foreach($emailRefered as $referedEmail) {
>  
>                         if ($referedEmail != "" && $referedName != "") {
>  
>                                     <message body here>
>  
>                                     /* To send HTML mail, you can set
> the Content-type header. */
>                                     $headers = "Return-Path:
> <$support_email>\r\n";                          
>                                     $headers .= "MIME-Version: 1.0\r\n";
> 
>                                     $headers .= "Content-type:
> text/html; charset=iso-8859-1\r\n";
>                                     $headers .= "From: $nameReferer
> <".$emailReferer.">\r\n";
>  
>                                     mail ($referedEmail,"Your Order on
> Rinkrake.com", $message, $headers);
>  
>                         }
>             }
> }
>  
> Any thoughts on why I get the email twice?
>  
> Thanks all. Much appreciated!
>  
> Aaron 

Lemmie, try that again, (darn webmail and TABS moving to next form element)...

FYI - prob. better in the php-general list.

But, I think your nested foreach() are the culprit.

Assuming $nameRefered = (
  'Joe', 
  'Bob', 
  'Jane' 
)
Assuming $emailRefered = (
  'example1@example.com',
  'example2@example.com',
  'example3@example.com'
)

With your code, you would get emails to the following...
  Joe <example1@example.com>
  Joe <example2@example.com>
  Joe <example3@example.com>
  Bob <example1@example.com>
  Bob <example2@example.com>
  Bob <example3@example.com>
  Jane <example1@example.com>
  Jane <example2@example.com>
  Jane <example3@example.com>

I would consider using the following due to the name/email being stored in
separate variable structures.

for( $i=0, $i < sizeof($nameRefered), $i++){
  /* to get this name */
  $thisname = $nameRefered[$i];

  /* to get thisname's email */
  $thisemail = $emailRefered[$i];
}

Provided of course that the values in the slots of the array correspond perfectly.
-- 
Ryan T. Gallagher




-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux