[thelist] PHP Noob-comparing array element
Bryan Talbot
bryan at btworlds.com
Wed Jan 15 14:29:08 CST 2003
Hello folks,
It is with utmost reluctance that I ask for help, as I am a big believer in
figuring things out for myself. I am about to abandon this approach
and use MySQL, but I hate to leave a problem unsolved.
I have a registration form that stores Names and Email addresses into
a .txt file, and naturally wish to prevent duplicate Email addresses.
I'm exploding an array containing (name|emailaddress),
and can successfully display every arrayelement[1], but I cannot
make the subsequent comparison statement work.
Here's the code:
<?
if (!isset($name) || !isset($email) || empty($name) || empty($email)) {
echo "Both name and email address are required.";
}
$email = strtolower($email);
$file = file('subscriber.txt');
function issubscribed($email) {
$subscribers = file('subscriber.txt');
if ($subscribers) {
foreach($subscribers as $count => $subscriber) {
$info = explode('|', $subscriber);
echo $info[1] . "<br>";
// I did that echo statement to make sure $info[1] is a reality-- //
// it displays a nice complete list of all the email addresses //
// BUT, the following comparison statement does not work: //
if ($info[1] == $email) {
echo "The email address " . $email . " is already subscribed.<br>\n";
exit;
}
}
}
}
function subscribe($name, $email) {
$file = @fopen('subscriber.txt', 'a+');
if (!$file) {
echo "Error: Couldn't open subscriber file.";
}
fputs($file, "$name|$email\n");
fclose($file);
echo "Done! Thanks.";
}
issubscribed($email);
subscribe($name, $email);
?>
The subscribe($email) function works fine.
It will happily store duplicate addresses all day long.
No errors are displayed.
I have tried substituting an existing email address for $email in the
comparison statement and that doesn't work.
I have tried changing the order of the function calls in the script.
I have tried everything I can think of.....
and yes, I have RTFM.
Like I said, I'm a noob...what am I missing here...does anyone have an idea?
Thank you,
Bryan
More information about the thelist
mailing list