Newbie stuck...foreach (Solved)


#1

Hi, I’ve recently started to seriously try and learn PHP and MySQL

I’ve bought janet valades book “php and mysql for dummies” and have been setting up her example catalogue and member login scripts to practise.

I’ve managed to get the catalog script working but have run into a problem with the membership script that I just can’t seem to figure out.

my problem is to do with checking to see whether the necessary fields have been filled in on the membership form.

janet’s script has this:
(all fields must be filled in except fax)
[php]
case “new”:
foreach($_POST as $key => $value)
{
if ($key != “fax”)
{
if ($value == “”)
{
unset($do);
$message_new = “Required information is missing.
Please try again.”;
include(“login_form.inc”);
exit();
}
}

/…more code…/[/php]

which doesn’t seem to work as i must still fill in ALL fields to join including “fax”.

I tried modifying the script to this:

[php]case “new”:
foreach($_POST as $key => $value)
{
if (($key == “newname”) or ($key == “password”) or ($key == “firstname”) or ($key == “lastname”) or ($key == “email”))
{
if ($value == “”)
{
unset($do);
$message_new = “Required information is missing.
Please try again.”;
include(“login_form.inc”);
exit();
}
}

/…more code…/[/php]

as i thought stating the fields i didn’t want empty might work.

I’m not even sure I have the syntax(?) right but it seemed to work. except now i can leave any field blank and join. I’m almost positive i’m not using ‘or’ correctly, but my brain is now scrambled with the trial and errors i’ve been making and this seemed to actually get halfway near where I wanted to go.

I’ve searched php.net on all the terms i could think of but now I’m a bit lost. I’m the kind of learner who likes to see how things work ‘in action’ this is making me really confused and I’m finding I’m running around in circles.

Can anyone see something basic there that I’m just not getting? Or can explain the foreach and if statements janet has in simple terms?

Thanks in advance,

NightOwl


#2

Well first I think we can look at the original code you presented.

[php]
case “new”:
foreach($_POST as $key => $value)
{
if ($key != “fax”)
{
if ($value == “”)
{
unset($do);
$message_new = “Required information is missing.
Please try again.”;
include(“login_form.inc”);
exit();
}
}

/…more code…/
[/php]
The CASE is just a selection method (presumably with more than 2 choices.) In this case I assume it’s to register a new user.

The foreach statement http://us3.php.net/foreach is basically looping through an array (from the POSTed variables). It will make available the INDEX of the array as $key and the CONTENTS at a given index available as $value

The first IF is checking to see, that at the given INDEX (expressed by the variable $key) , IF it’s NOT EQUAL ( != ) what is in the quotes. In this case it’s the word fax. It got this INDEX/key from the form that POSTed it. So if the value is NOT EQUAL then it enters the loop or condition. This is where I think you might be having the problem. It must be an EXACT match. So as long as you DON’T get the word fax all lower case, it will enter the loop.

For anything EXCEPT fax it enters the condition. Then it meets the next condition. Now it’s evaluating the CONTENTS stored in $value. It just checks to see if it’s empty ( “” - 2 double quotes) and if it is it does some error reporting and presents the message.

That is the basics of the code you presented.

Again, make sure on your FORM that you called the variable for the element of FAX by the name of fax, again noting the case.

As for your next question about the OR’s , you should use the double pipe ( || ) for the OR and the double ampersand ( && ) for the AND.


#3

peg110,

Thankyou so much.

I’d tried using single pipes (|) for the (or) statement, which I was sure I had read in janet’s book and somewhere else, along with various other methods, (including putting the fields into their own array! - tho I’m not sure why as, as I understand it, the foreach made them an array as such anyway and walked thru each element one at a time.)

I had checked the case and was sure everything was ok. It turns out that two of the elements WERE wrong in case by one letter, (eg:firstname/firstName), and one was labelled wrong.

…there’s a lesson there :wink:

lol, it’s the simple things, and it’s amazing what looking at something with fresh rested eyes can reveal.

Anyway, for those interested, this is the code I changed it to:

[php]case “new”:
foreach($_POST as $key => $value)
{
if (($key == “newname”) || ($key == “newpass”) || ($key == “firstName”) || ($key == “lastName”) || ($key == “email”))
{
if ($value == “”)
{
unset($do);
$message_new = “Required information is missing.
Please try again.”;
include(“login_form.inc”);
exit();
}
}[/php]

I"ll turn it around, back to != and see if I can’t get it to work in reverse later as an exercise.

Thanks again for your help in pointing this beginner in the right direction. It’s sincerely appreciated. :D