Forum Moderators: coopster

Message Too Old, No Replies

eregi_replace & loops!

Its something simple I know.....

         

dreamcatcher

4:52 pm on Sep 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I`m looping through some names and e-mail addresses in a database, so that I can send an e-mail message to all of them. I want to use eregi_replace so that <--subname--> in the message becomes the user name and <--unsub--> becomes an unsubscribe link.

Here is my code:

query database stuff.....

while ($row = mysql_fetch_array($result))

{

$subname[] = $row['listname'];
$subemail[] = $row['listemail'];

}

for ($i=0; $i<sizeof($subname); $i++)

{

$mailcomments = eregi_replace("<--subname-->", $subname[$i], $mailcomments);
$mailcomments = eregi_replace("<--unsub-->", $website_url . "/mail.php?action=unsubscribe&email=" . $subemail[$i], $mailcomments);
$mailcomments = stripslashes($mailcomments);
mail($subemail[$i], $mailsubject, $mailcomments, "From: $website<$webemail>");

}

messageSent();

The e-mails are looping ok, but the name that is displayed in the e-mails and the unsubscribe e-mail are always the first person in the database. So the eregi_replace parts aren`t looping.

I assume I`m just missing something silly like a [$i]?

Thanks for the help!

:)

jaski

5:32 pm on Sep 25, 2003 (gmt 0)

10+ Year Member



That is because once the string to be replaced is replaced ...it is no longer there in the next iteration of the loop to be replaced.

$mailcomments_copy = $mailcomments; //make copy of original string

for ($i=0; $i<sizeof($subname); $i++)

{

$mailcomments_copy = eregi_replace("<--subname-->", $subname[$i], $mailcomments_copy);
$mailcomments_copy = eregi_replace("<--unsub-->", $website_url . "/mail.php?action=unsubscribe&email=" . $subemail[$i], $mailcomments_copy);
$mailcomments_copy = stripslashes($mailcomments_copy);
mail($subemail[$i], $mailsubject, $mailcomments_copy, "From: $website<$webemail>");

}

This should work.

Jaski

dreamcatcher

6:37 pm on Sep 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for the help Jaski. Unfortunately that doesn`t work, I still get two identical e-mails.

:(

coopster

9:56 pm on Sep 25, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



jaski is right, he just threw the code down wrong. Combine your original code with his suggestion and try again:
query database stuff.....

while ($row = mysql_fetch_array($result))

{

$subname[] = $row['listname'];
$subemail[] = $row['listemail'];

}

$mailcomments_copy = $mailcomments; //make copy of original string

for ($i=0; $i<sizeof($subname); $i++)

{

$mailcomments = eregi_replace("<--subname-->", $subname[$i], $mailcomments);
$mailcomments = eregi_replace("<--unsub-->", $website_url . "/mail.php?action=unsubscribe&email=" . $subemail[$i], $mailcomments);
$mailcomments = stripslashes($mailcomments);
mail($subemail[$i], $mailsubject, $mailcomments, "From: $website<$webemail>");

$mailcomments = stripslashes($mailcomments_copy);
}

messageSent();

dreamcatcher

10:27 pm on Sep 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yep, thats got it. Thanks for all the help guys!

:)