Forum Moderators: coopster

Message Too Old, No Replies

getting php to match & select from list?

I think I need an array, but I'm not sure...

         

Sol_1056

1:11 pm on May 28, 2004 (gmt 0)

10+ Year Member



I have a list of values, where $list = ___, then $email = ____. I'm carrying over $list from the previous page as a hidden element of the form, and I want the php code to match the value of $list and replace it with the value of a corresponding $email.

If there were only two or three, it'd be easy:

if ($list = "xyz")
{
$email = "xyz@domain.com";
}
else ($list = "abc")
{
$email = "abc@domain.com";
}

and I think that's the code but I'm typing from memory since the actual file's at home.

Anyway, once it gets about three or four, the whole if/elseif/else combination becomes top-heavy and falls through. I went back and forth through my PHP books, and although it *seems* (in pseudo code) that I could do what I want with a combination of match, find, or replace, most of the books seem to figure that I'll just go straight into doing a SQL database. Since this is all I want to do (so far) it seems like overkill to pay for the SQL db on my hosting service, if it's possible to make the list, have PHP select the $email value that corresponds to the $list value carried over, and then use that $email value as the recipient.

Uh...if it's possible, I mean. Is it?

(Thanks again!)

HelenDev

1:52 pm on May 28, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hope I understand you right:) If so this would be my code...


$list = array ( "abc", "xyz", "foo" );

foreach($list as $recipient) {
$recipientlist[] = $recipient."@domain.com";
}

//if you print the array recipientlist you will see it contains the email addresses...
print_r($recipientlist);

Sol_1056

1:56 pm on May 28, 2004 (gmt 0)

10+ Year Member



Thanks, but will that work even though each address has a completely different domain?

HelenDev

2:01 pm on May 28, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



but will that work even though each address has a completely different domain?

er, no.

Where do you get the $email values from? I think I misunderstood that part.

Incidentally, you can get php to read/write a text document as an alternative to working with a db, if that's at all helpful.

H.

Sol_1056

2:05 pm on May 28, 2004 (gmt 0)

10+ Year Member



Err, also forgot to add that it looks like the code you gave me will produce a list of all the email addresses, but I only want the form sent to one of them at a time, depending on the information passed through the strings and into the form.

In the form, on first page, the form includes a line for hidden with name of 'list' and value="<?php echo $list?>". Then, on the proc page, here's the pseudocode for what I'm trying to do.

where $list = "abc" then $email = "asdf@domain1.com"
where $list = "xyz" then $email = "qwer@domain2.net"
where $list = "def" then $email = "xcvb@domain3.org"
(etc, 45 times!)

compare $list (from form/url) to $list values above
get $email value where $list = $list
set $email = $to

(and then the rest of the formmail setup follows)

Does that make more sense?

thedagda

2:17 pm on May 28, 2004 (gmt 0)

10+ Year Member



Not that it is much more elegant, but you can use a switch statement instead of multiple if/elseif statements:

switch($list){
case 'abc':
$email = 'asdf@domain1.com";
break;
case 'dsf':
$email = 'dsf2@domain2.com";
break;
...
}

HelenDev's got the best long-term solution to this I think. Format a text file and read up on your file read/write functions. It will probably much easier to work with in the long run (and less maintenance).

HTH,

Conor

HelenDev

2:23 pm on May 28, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmm, yes it makes more sense now. I think ideally you would have a db for something like this.

How come your elseif statement doesn't work? I'm not aware that there is a limit to how many times you can do that (correct me if I'm wrong anyone!) Perhaps there is a syntax error somewhere? Or does it just take too long?

thedagda

2:25 pm on May 28, 2004 (gmt 0)

10+ Year Member



I don't think there is a limit to the elseif statements either. The switch statement is just much quicker to write (and probably to run).

Sol_1056

6:28 pm on May 28, 2004 (gmt 0)

10+ Year Member



I tried doing the list with the switch option. First thing the form did was throw the error of "you need to fill out all spaces" even when I really had - and that wasn't an error I'd gotten before. So I commented the form's error-checking out, and it then proceeded to throw more errors in the long list of case/break/etc, until I changed:

case 'maryta':
$email = 'mtaylor@domain.com";
break;

so the 'mtaylor... is now "mtaylor...

Then I started getting errors (I guess we're working our way down the page, eh?) at the

$to = $email;

So I tried "$email" and then switched them so it's $email = $to; at which point I started getting parse errors on line 138...which is blank. The line immediately preceeding it is the mail() line.

Ahem. Giving up temporarily, I went back to the if/elseif list I'd originally tried, made sure it was if/elseif/elseif/elseif (I'd missed one), and loaded that up to test. Now it accepts the form...and never delivers the mail.

Ah. Hm. About this text file. Can someone direct me to a page that explains how to do that? (Like I said above, paying the extra $36 a year for SQL option on my hosting service seems ridiculous if there's a simpler way to do this one blasted thing...)

Thx for your help, all.

Sol_1056

8:37 pm on May 28, 2004 (gmt 0)

10+ Year Member



Well, I don't know what crazy stuff was going on when I was doing it from work in Notepad, but I got open, redid it all over again in jEdit, and ta-dah, it works. Like a friggin' CHARM.

*runs around in circles while scaring the beagle*

Thanks, everyone, for suggestions & letting me puzzle it out here. Much, MUCH appreciated - I've been struggling with this for a week with the various emailer codes, and I guess sometimes the simplest really is the best.

Thanks again!

- Sol