Jump to content
Larry Ullman's Book Forums

Chapter 6 - Pursue


Recommended Posts

Hey ho,

 

Just want to dedicate this post to a few questions regarding chpter 6 and it's pursue section. sorry in advance if these questions are 'dumb' 'm still getting my head around the basics!

 

The first task was to use a variable for the current year, I used the date('Y') function like this $current_year = date('Y'); Then used $current_year instead of the hard coded value. Would this be correct?

 

 

Second task was to display all inputted variables from the Registration Form, this is what did;

 

//Declare variables

$email = $_POST['email'];

$password = trim($_POST['password']);

$confirm = trim($_POST['confirm']);

$year = $_POST['year'];

$colour = $_POST['colour'];

$terms = $_POST['terms'];

 

//Print variable values

$results = $email.'<br /> '.$password.'<br /> '.$confirm.'<br /> '.$year.'<br /> '.$colour.'<br /> '.$terms;

print $results;

 

Is there a better simpler way of doing this?

 

 

Third task, displaying the colour chosen in the chosen colour, did this:

 

<style type="text/css">

.blue { color: blue; }

.yellow { color: yellow; }

.red { color: red; }

.green { color: green; }

</style>

 

switch ($colour) {

case 'blue':

$colour_type = 'primary';

print "<p class=\"blue\">The colour you selected was $colour of the $colour_type type</p>";

break;

case 'red':

$colour_type = 'primary';

print "<p class=\"red\">The colour you selected was $colour of the $colour_type type</p>";

break;

case 'yellow':

$colour_type = 'secondary';

print "<p class=\"yellow\">The colour you selected was $colour of the $colour_type type</p>";

break;

case 'green':

$colour_type = 'primary';

print "<p class=\"green\">The colour you selected was $colour of the $colour_type type</p>";

break;

default:

print "<p>Please select a colour from the list</p>";

$flag = FALSE;

break;

}

 

Again is there a better way bearing in mind 've not used concatination?

 

 

Forth task of validating day and month as well as seting avariable to show day/month/year I did this:

 

//Declare Variables

$day = $_POST['day'];

$month = ucfirst($_POST['month']);

$year = $_POST['year'];

 

//Validate Day

if (!isset($_POST['day'])) {

print "<p>Please enter a value for day</p>";

$flag = FALSE;

}

 

//Validate Month

if (!isset($_POST['month'])) {

print "<p>Please enter a value for month</p>";

$flag = FALSE;

}

 

//Validate year is numeric and calculate age

if (is_numeric($year) && (strlen($year) == 4)) {

if ($year < $current_year) {

$age = $current_year - $year;

} else {

print "<p>Please enter a valid year</p>";

$flag = FALSE;

}

} else {

print "<p>Please re-enter the year you was born</p>";

$flag = FALSE;

}

 

then at end of script i did this:

 

//Print registration results

if ($flag) {

$dob = $day.' '.$month.' '.$year;

print "<p>Thank you. You have been registered</p>";

print "<p>Your D.O.B is: $dob</p>";

}

 

Again in the context of what I've learnt so far is there a better way of doing this?

 

Jst to add all of these tasked worked.

 

Sorry for the long post by the way!

Link to comment
Share on other sites

I came up with this. Basically it is the same as what you have, except that I made another switch so I could print out the "your favorite color is this" statement where I wanted it. Also instead of escaping the trouble characters, I did the print statement with single quotes and concatenation. Maybe that is what the book was looking for.

 

 

switch ($color) {

case 'red':

print '<p class="red">Your favorite color is ' . $color . '</p>';

break;

case 'yellow':

print '<p class="yellow">Your favorite color is ' . $color . '</p>';

break;

case 'green':

print '<p class="green">Your favorite color is ' . $color . '</p>';

break;

case 'blue':

print '<p class="blue">Your favorite color is ' . $color . '</p>';

break;

default:

break;

}

Link to comment
Share on other sites

phpRob, the only thing I see in your fourth task that you can improve is that you first declared the variables from $_POST, but then when you validated the day and the month, you still called the values using $_POST instead of using the new variable name.

 

oh also at the end when you create the $dob variable, you can add a / in between your two single quotes to make the date look exactly like what the book is asking for. Like this:

 

$dob = $month . '/' . $day . '/' . $year;

But maybe that is just being nit-picky

Link to comment
Share on other sites

Thanks for the replies, wasn;t sure i'd get any for a minute :)

 

Maybe someone else can share some light on how to tackle the third task better?

 

Well spotted on the fourth task and th calling of values via £_POST, yes I should have used the variables I delared at start of script.

 

I did think about inserting the '/' between $day $month $year though I didn't think it was too improtant so left it out. I concerned with functionability more than anything at this stage.

Link to comment
Share on other sites

 

Well spotted on the fourth task and th calling of values via £_POST, yes I should have used the variables I delared at start of script.

 

 

Just tried amending the script to use the declared variables $day and $month:

 

//Validate Day

if (!isset($day)) {

print "<p>Please enter a value for day</p>";

$flag = FALSE;

}

 

//Validate Month

if (!isset($month)) {

print "<p>Please enter a value for month</p>";

$flag = FALSE;

}

 

It now reads no input from the html form as TRUE?? My Form script looks like this:

 

<form action="handle_post.php" method="post">

 

<p>Email address: <input type="text" name="email" size="30" /></p>

 

<p>Passsword: <input type="password" name="password" /></p>

 

<p>Confirm password: <input type="password" name="confirm" /></p>

 

<p>D.O.B:

 

<select name="day">

<option value="">Day</option>

<?php

for ($i = 1; $i <= 31; $i++) {

print "<option value=\"$i\">$i</option>\n\t\t";

}

?>

</select>

 

<select name="month">

<option value="">Month</option>

<option value="january">January</option>

<option value="february">February</option>

<option value="march">March</option>

<option value="april">April</option>

<option value="may">May</option>

<option value="june">June</option>

<option value="july">July</option>

<option value="august">August</option>

<option value="september">September</option>

<option value="october">October</option>

<option value="november">November</option>

<option value="december">December</option>

</select>

<input type="text" name="year" value="YYYY" size="4" />

</p>

 

<p>Favourite colour: <select name="colour">

<option value="">----</option>

<option value="blue">Blue</option>

<option value="yellow">Yellow</option>

<option value="green">Green</option>

<option value="red">Red</option>

</select></p>

 

<p><input type="checkbox" name="terms" value="Yes" /> I accept the terms</p>

 

<p><input type="submit" name="submit" value="Submit" /></p>

 

</form>

 

Any suggestions on how I can get this to work, noticed the validation dosn't work? Is it bcause the variable has a value of "" to begin with?

Link to comment
Share on other sites

I tried giving the default options of the drop-downs a value of 'Day' and 'Month' and calling the following validation via php:

 

if ($day == 'Day') {

print "<p>Please enter a value for day</p>";

$flag = FALSE;

}

 

if ($day == 'Month') {

print "<p>Please enter a value for day</p>";

$flag = FALSE;

}

 

This works, but are there any other validation on 'select HTML elements' I need to be aware of?

Link to comment
Share on other sites

The first task was to use a variable for the current year, I used the date('Y') function like this $current_year = date('Y'); Then used $current_year instead of the hard coded value. Would this be correct?

 

Sorry for the delayed reply. I didn't have the book in front of me. And then I was confused because what you suggest is the first task (etc.) is actually the second (etc.) For the second task, that is correct.

 

 

Second task was to display all inputted variables from the Registration Form, this is what did;

 

//Declare variables

$email = $_POST['email'];

$password = trim($_POST['password']);

$confirm = trim($_POST['confirm']);

$year = $_POST['year'];

$colour = $_POST['colour'];

$terms = $_POST['terms'];

 

//Print variable values

$results = $email.'

'.$password.'

'.$confirm.'

'.$year.'

'.$colour.'

'.$terms;

print $results;

 

Is there a better simpler way of doing this?

 

For the third task, that is one option, which is the long way. The short way is to do print_r($_POST);

 

Third task, displaying the colour chosen in the chosen colour, did this:

 

.blue { color: blue; }

.yellow { color: yellow; }

.red { color: red; }

.green { color: green; }

 

switch ($colour) {

case 'blue':

$colour_type = 'primary';

print "

The colour you selected was $colour of the $colour_type type

";

break;

case 'red':

$colour_type = 'primary';

print "

The colour you selected was $colour of the $colour_type type

";

break;

case 'yellow':

$colour_type = 'secondary';

print "

The colour you selected was $colour of the $colour_type type

";

break;

case 'green':

$colour_type = 'primary';

print "

The colour you selected was $colour of the $colour_type type

";

break;

default:

print "

Please select a colour from the list

";

$flag = FALSE;

break;

}

 

Again is there a better way bearing in mind 've not used concatination?

 

For the fourth task, that is one option. Or you could just keep the message where it was and use $_POST['color'] (or $colour) in the message (twice).

 

 

Forth task of validating day and month as well as seting avariable to show day/month/year I did this:

 

//Declare Variables

$day = $_POST['day'];

$month = ucfirst($_POST['month']);

$year = $_POST['year'];

 

For the fifth task, I'm not sure why you're using ucfirst(), because month should be a number.

 

//Validate Day

if (!isset($_POST['day'])) {

print "

Please enter a value for day

";

$flag = FALSE;

}

 

//Validate Month

if (!isset($_POST['month'])) {

print "

Please enter a value for month

";

$flag = FALSE;

}

 

That's a start. You could also use is_numeric() to make sure they're numbers, and then make sure they are greater than and less than the right values.

Link to comment
Share on other sites

If terms is not set then it will not equal 'yes', as it uses the AND operator this if conditional would never be true. The second if conditional uses the OR operator, i.e. is variable 'terms' set OR is variable not equal to 'yes'.

I'm sure more experienced programmers will explain this better!

Link to comment
Share on other sites

  • 1 month later...

Here is what I did for Pursue 4:

 

//Create some css styles

 

.red {color:red;}

.yellow {color:yellow;}

.green {color:green;}

.blue {color:blue;}

 

//Alter the switch statement so that each case assigns values to some new variables, and then concatenates them to a new variable, along with some html to apply the css class to the print output at the end

 

//Validate the color

switch ($_POST['color']) {

case 'red':

$color_type = 'primary';

$color_selected ='red';

$css_class = '<span class="red">' . "$color_selected" . '</span>'. ',';

break;

case 'yellow':

$color_type = 'primary';

$color_selected ='yellow';

$css_class = '<span class="yellow">' . "$color_selected" . '</span>' . ',';

break;

case 'blue':

$color_type = 'primary';

$color_selected ='blue';

$css_class = '<span class="blue">' . "$color_selected" . '</span>' . ',';

break;

case 'green':

$color_type = 'secondary';

$color_selected ='green';

$css_class = '<span class="green">' . "$color_selected" . '</span>'. ',';

break;

default:

print '<p class="error">Please select your favourite colour.</p>'. ',';

$okay = FALSE;

break;

} //end of switch

 

//Then alter the print output to include the new variable

 

//If there are no errors, print a success message:

if ($okay) {

print '<p>You have been successfully registered (but not really).</p>';

print "<p>You will turn $age this year";

print "<p>Your selected color, $css_class is a $color_type color.</p>";

}

 

 

I concatenated a comma onto the end of the $css_class variable cos I am a punctuation nerd (I even worried whether to put it inside or outside the span!).

 

This seems to work well, but I would be grateful for any comments.

Link to comment
Share on other sites

Thanks for sharing. You've got the right idea, although I would remove this line from every switch case:

$css_class = '' . "$color_selected" . ''. ',';

and just use it later:

print "

Your selected color, $color_selected is a $color_type color.

";
Link to comment
Share on other sites

  • 3 months later...

For the third task, that is one option, which is the long way. The short way is to do print_r($_POST);

 

 

I tried this, but all I get is the word "Array".

 

 

 

Edit: Nevermind. I figured it out. I thought I had resubmitted the form from the html file, but I must have reloaded the handle_reg.php by mistake.

Link to comment
Share on other sites

For the fifth task I tried:

//Validate Day
if (empty($_POST['day'])) {
print '<p class="error">Please enter a value for day</p>';
$okay = FALSE;
}
//Validate Month
if (empty($_POST['month'])) {
print '<p class="error">Please enter a value for month</p>';
$okay = FALSE;
}

//Validate the birth year  
if (is_numeric($_POST['year']) AND (strlen($_POST['year']) == 4) ) {

//Check that they were born before the current day
$current_year = date('Y');
$today  = date( "n/j/Y");
$birthday = $_POST['month']."/".$_POST['day']."/".$_POST['year'];
if ($birthday < $today) {
	$age = $current_year - $_POST['year'];
	//Calculate age this year
} else {
	print '<p class="error">Either you entered your birth year wrong or you come from the future!</p>';
	$okay = FALSE;
} //End of 2nd conditional

} else { //Else for 1st conditional
print '<p class="error">Please enter the year you were born as four digits.</p>';
$okay = FALSE;

but when I enter a valid birthday, I get this message: "Either you entered your birth year wrong or you come from the future!" so my conditional ($birthday < $today) is coming back false when it shouldn't. What am I doing wrong?

Link to comment
Share on other sites

Thanks. That should have occurred to me. I've gotten it to work, sort of. If I try to intentionally trigger an error I get the error message (Either you entered...) up through 2037(using April 3 as the month and day). Using a 2038 year date the code

print "<p>You will turn $age this year.</p>";

gives me "You will turn -26 this year."

Here is the pertinent code:

//Check that they were born before the current day
$current_year = date('Y');
$today  = new DateTime();
$birthday = strtotime($_POST['month']."/".$_POST['day']."/".$_POST['year']);
if ($birthday < $today->getTimestamp()) {
	$age = $current_year - $_POST['year'];
	//Calculate age this year
} else {
	print '<p class="error">Either you entered your birth year wrong or you come from the future!</p>';
	$okay = FALSE;
} //End of 2nd conditional

} else { //Else for 1st conditional

print '<p class="error">Please enter the year you were born as four digits.</p>';
$okay = FALSE;

} //End of 1st conditional

What am I doing wrong?

Link to comment
Share on other sites

  • 1 month later...

Hello!

 

Here is what I arrived at for the 4th "Pursue" assignment:

 

Up in the <head>

<style type="text/css" media="screen">

.blue { color: blue; }

.green { color: green; }

.red { color: red; }

.yellow { color: yellow;}

</style>

 

And then down in the "success message" section:

$color = $_POST['color'];

print "<p>Your favorite color is <span class=\"$color\">$color</span> and is a $color_type color.</p>";

 

Seems to work OK, but no concatenation (as suggested in the intructions). Is there a more efficient way to have accomplished this?

 

Also, Larry, wondering if you have made your own versions of the "pursue" scripts available for download? If not, a suggestion for the future - I know I would find it helpful to have them to compare to.

 

Thanks!

Link to comment
Share on other sites

That's a fine solution and thanks for sharing it. You could have used concatenation instead of \" but it's a matter of preference.

 

As for my own versions of the pursue prompts, I never created any. The Review & Pursue are things I thought of while writing the chapter that I thought would be good exercises, stress key points, and be recommendations for further study. That's as far as I took it. Others have asked that I manufacture and provide these solutions, but I would estimate that about a third of the pursue prompts are not providable (e.g., "Check out the PHP manual's pages..."), another third would vary wildly from one person to another, and only about the last third would have a relatively predictable result. And, of course, I'm happy to answer anyone's question on any of them here for free.

 

Thanks for the suggestion, though.

Link to comment
Share on other sites

You're quite welcome and thanks for the nice words. By the way, I always appreciate positive Amazon reviews as a way of saying thanks, if you're so inclined.

 

Will do, gladly. I'm on my way over there soon to order PHP and MySQL for Dynamic Web Sites, and looking forward to going through that book.

 

Thanks again!

Link to comment
Share on other sites

 Share

×
×
  • Create New...