PHP - Day 32 - May 9 - Part One Posted 6 days ago
digg
delicious
stumble
reddit

Now….going to use what I learned yesterday on my handleReg.php page.
Adding two more conditionals, and nesting (yikes!) one of the $year conditionals inside another! Exciting!

First - (shiver) I delete the existing validation for the $year variable entirely. Am re-writing it, so it’s easier to start from scratch than to try and add/delete to the existing one. Ok…it’s gone, now on to what is to be in its place:

First, I’ll check that the $year is a four-digit number:
as so: if ( is_numeric ($year) AND strlen ($year) == 4) {
}

The first part of this checks to see if it is numeric, using the “is_numeric” function. Then it checks that the string length (thus strlen) is equal to 4. The “AND” in there means that both of the conditions have to be true - the numeric part and the length part. Cool. That is pretty straightforward - and I understand what’s going on here - that’s good!

Now comes the second conditional….gotta be careful I get this right - looks easy to mess up - so I’m going slow.
as so: Now checking to see if the birthdate precedes 2008

        if ($year >= 2008 {
print " Either you entered your birth year wrong, or
you come from the future!";
} else {
$birthYear = $year;
} This is the end of the second conditional


This whole thing goes inside the curly braces of the first part above. Here’s the whole thing together:

if ( is_numeric ($year) AND strlen ($year) == 4) {  (first curly brace)

if ($year >= 2008 { (second curly brace)
print " Either you entered your birth year wrong, or
you come from the future!";
} (end of second curly brace) else { (third curly brace)
$birthYear = $year;
} (end of third curly brace) End of the 2nd conditional


} (end of first curly brace)

Ok - that part is done, and all the curly braces have a beginning and an end. Now to finish up the first conditional:

as so:   else { (fourth curly brace)
print "Please enter the year you were born as a four digit
number!";
} (end of fourth curly brace)


Now to add that in to my previous code so I can see the whole thing!

if ( is_numeric ($year) AND strlen ($year) == 4) {  (first curly brace)

if ($year >= 2008) { (second curly brace)
print " Either you entered your birth year wrong, or
you come from the future!";
} (end of second curly brace) else { (third curly brace)
$birthYear = $year;
} (end of third curly brace) End of the 2nd conditional

else { (fourth curly brace)
print "Please enter the year you were born as
a four digit number!";
} (end of fourth curly brace)


} (end of first curly brace)

Looking good - everything matches up….so are we done? NO! Of course not! Now we’re going to validate that a color was picked (wondered when we’d get to that one!)

as so;  if ( !$color) {
print "Please select your favorite color!";
}


The exclamation point before the $color variable is saying that if no color was selected, the variable has no value in it, and the condition is TRUE, so the message will be printed.

Now the time is here…..dum de dum dum…..to save and upload and test! Oops! Almost forgot the semi-colons - again proving how easy it is to leave them out! They go after every print command - and I didn’t put them in at first. At least I caught it before I uploaded it!

OK…not working - and I’m not sure why - back to checking all the code….stay tuned…
Think I found it - when I put in the if ($year >= 2008), I forgot to close the parenthesis (is? es? hmmm). So, changed that, now uploading again…

and another blank page.

Now going to take it line by line - adding them in til I find what is doing this grrrr!

Too many things wrong here.

Have to step away from the computer for a while - have been here WAY beyond my 15 and gotten nowhere. I’ll try again later today.

BUMMER!


Recent Comments

No comments yet.

Please login to comment.

Back