[thelist] building a dynamic variable in php

sasha spam at bittersweet2.com
Mon Jan 13 12:27:01 CST 2003


On Mon, 13 Jan 2003 10:14:51 -0800 (PST), Tom Dell'Aringa
<pixelmech at yahoo.com> wrote:

> Right, my bad guys. I have a send to friend list where they can send
> a link to 10 friends. Psuedocode:
>
> get 10 post variables as i
> if i is set
> send a mail to i
> end
>
> So I want the mail() code within the loop so I don't have to repeat
> the code, and just use variables for the data. The body and all that
> info is all the same except the receipient of course.
>
> Does an array make sense in this context? Here is my current code:
> -------------------------------------------------
> for($i = 1; $i<=10; $i++)
> {
> 	$var = "mail" . $i;
> 	${$var} = $_POST[$i];
>
> 	$body = "body of email";
>
> 	mail(${$var}, "From $personA/$personB", $body, "From:
> $email\r\nReply-To: $email\r\n");
> }
> --------------------------------------------------
> Tom

(assuming all "to" addresses are in a single array, the html form would
have to reference all "to" fields as name="to[]")

$sendto = '';

foreach ($_POST['to'] as $address) {
	$sendto .= $address . '; ';
}

$sendto = preg_replace("/\; $/", '', $sendto); // cross fingers on reg_exp
here; might need to be \;\s$ instead of \; $

--
sasha



More information about the thelist mailing list