Jump to content
Larry Ullman's Book Forums

Recommended Posts

All the code used in the book can be downloaded from the following link:

http://www.larryullman.com/books/php-and-mysql-for-dynamic-web-sites-visual-quickpro-guide-4th-edition/#downloads

 

Unfortunately, when I tried to download it just now, the zip file seemed to be corrupted, and I couldn't unzip it.

Hopefully Larry can shed some light on this.

Share this post


Link to post
Share on other sites

Thanks, I have all the scripts. I see there was a banking database created in chapter 6 and there are no scripts for chapters 4-7. I can easly create a database, but I am curious about what database is referenced.

Share this post


Link to post
Share on other sites

I need some help on Chapter 13 Review and pursue.  Instead of posting a new topic I thought I place it here. 

 

Page 432 the last "Pursue" challenge: "Apply the Fileinfo extension to the show_image.php script from Chapter 11".

 

I can not figure out how to do this.  I have only been using the original script.  I do not see how to use the resource (file info) once created.  I believe I need to apply this resource to $name = $_GET['image'], but am unsure how this would work as this is a function, and the show_image.php is a script that images.php validates from.  

 

Any ideas  on how to pursue this challenge?  I sure wish I could figure it out.  

 

 

Thanks in advance for any ideas, or help understanding the Fileinfo extension.  

 

Mike

Share this post


Link to post
Share on other sites

If I recall correctly, what I was thinking here is that you'd use Fileinfo to verify the file's type before showing it (as opposed to just using getimageinfo()). So start by getting the Fileinfo reference on the file. Then check the file's type. 

Share this post


Link to post
Share on other sites

That makes sense, I'll get it... Thank you for answering my previous question. 

 

Now a poetncially really stupid question: I see page 473-478 we use javascript to control the pages error handling. We use calculator.html, calculator.js, and calculator.php (and a new version of the css document).  I am very curious how this works.  I can test to see that javascript is handling the new error reporting because the errors show up on the page (no alert window).  This thwarts the calculator.php page attempt at handling the errors because we use false to prevent actual form submission.  However, when we submit is javascript doing the calculation, or calculator.php?

 

What I am trying to wrap my head around is : the form from the calculator.html still calls calculator.php to do the calculations, or is calculator.js doing the calculations?  I test by changing the math on the .php version and the results are different.  When I change them on the .js version they are the same.  I am thinking that calculator.php is doing the math which leads me to be a little confused as to why calculator.js has the same math in it?  Can you clarify what is happening? 

 

I sure hope this makes sense.  

Share this post


Link to post
Share on other sites

eonfloyde, you are referring to pages 473-478 in the Modern JavaScript book, not the 4th edition of the PHP/MySQL book, correct?

no sir, I am talking about PHP and MySQL for dynamic websites fourth edition. 

Share this post


Link to post
Share on other sites

Yes, you are right. I'm sorry.

I didn't have the book readily available when I asked the question, so I just wanted to make sure.

 

Anyway, to answer your question, unless there is a mistake in your code, calculator.js should be doing all the calculations when you click the "Calculate!" button.

As you mentioned, the return false statement at the end of the event handler suppresses any actual form submissions. As such, calculator.php is never being called and none of the code in it is used for anything. The code on line 66 on page 477 is what is causing the total to be displayed on the screen.

 

With that said, it's beneficial to keep the form pointing to calculator.php and to keep that file around in the event that JavaScript is not supported or is intentionally disabled in someone's browser.

While the percentage of people nowadays that browse the Internet without JS enabled is quite low (reportedly around 1%), if you want to reach the widest audience possible, then keeping the PHP backup there in the event that JS cannot be used is good.

With that said, for the 99% of people that have JS enabled, using the JS solution instead of the PHP one will lead to a more responsive and fluid experience.

 

In short, providing a JS solution will generally lead to a better overall experience, but having a PHP solution as well as a backup will help you reach a wider audience (as well as potentially help increase the SEO of your site, etc.).

 

That help at all?

  • Upvote 1

Share this post


Link to post
Share on other sites

I have a question about my solution of the problem in Chapter 17.

 

"Modify the header and other files so that
each page’s title uses both the default
language page title and a subtitle based
upon the page being viewed"
 
My solution. Placing this in header:
	<title><?php if (basename($_SERVER['PHP_SELF']) == 'index.php'){
		echo $words['title'];};
				if (basename($_SERVER['PHP_SELF']) == 'forum.php' ){
		echo $words['forum_home'];			
				};
				if (basename($_SERVER['PHP_SELF']) == 'read.php'){
		echo $words['subject'];
		        if (isset($_GET['sub'])){
		echo ' about ';
		echo $_GET['sub'];        	
		        };			
				};
		?>
	</title>

I get $_GET['sub'] from forum.php.

 

My question: Is this solution safe? Before displaying the page title i can see the path to the file in the browser (chrome, exploler) where title shows up.

I wonder if this is only on localhost or on a live server too.

 

Share this post


Link to post
Share on other sites

First, I'd definitely use an if-elseif instead of just multiple IF statements. Second, generally speaking, directly outputting what's passed in the URL (i.e., in $_GET) isn't great, as it's easily manipulated.

Share this post


Link to post
Share on other sites

Larry,

I have the Kindle edition of

PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (4th Edition)Sep 23, 2011
by Larry Ullman
 
In it you mention briefly (or allude to) a 5th edition. I'd like to go back to paper. I miss the highlighting, scribbling in the margins, thumbing through the pages and sticking plastic tabs on them. And, I like the weight and feel of it as it begins to soften with wear. Eventually, it becomes a trusted friend.
 
Oh, and it's quite nice for all I learn from it!
Can we expect number 5 soon?
thanks,
Gary (aka chop)

 

Share this post


Link to post
Share on other sites

Thanks for the interest! I'll be writing the 5th edition in 2016, and it will come out in 2016. I'll update it for PHP 7. 

Share this post


Link to post
Share on other sites

Hi folks

 

And thanks Larry for a great book, its quite inspiring. I'm new to programming, I know my HTML and CSS but that's it. I've been following your book and I'm up to Chapter 2 -  Review and Pursue and stuck on the third question, sorry if this has been covered already.

 

Rewrite the gender conditional in handle_form.php (Script2.4) as one conditional instad of two nested ones, Hint: you'll need to use the AND operator.

 

 

 

The code in question is

if (isset($_REQUEST['gender'])) {
 $gender = $_REQUEST['gender'];
 if ($gender == 'M') {
  $greeting = '<p><b>Good day, Sir!<b></p>';
 } elseif ($gender == 'F') {
  $greeting = '<p><b>Good day, Madam!<b></p>';
 } else {
  $gender = NULL;
  echo '<p class="error">Gender should be either "M" or "F"!</p>';
} 

I've been stuck all day, I have gone away and come back again and I'm still drawing a blank.

 

Any help would be really appreciated, thanks.

Share this post


Link to post
Share on other sites

Sure thing! (And thanks for the nice words.) I'll give you the start of it which should get you moving. Let me know if you need further help!

if (isset($_REQUEST['gender']) && ($_REQUEST['gender'] == 'M')) {  
    $greeting = '<p><b>Good day, Sir!<b></p>';

Share this post


Link to post
Share on other sites

Many thanks Larry,

 

I'm afraid to say, I'm still stumped with this, at least that is if i've understood it correctly- I'm reading it as using only one conditional and no nested conditionals. 

 

With one conditional I can only work out how to account for 2 eventualities - either gender being M or F.

if (isset($_REQUEST['gender']) && ($_REQUEST['gender'] == 'M')) {
	$greeting = '<p><b>Good day, Sir!<b></p>';
} else {
	$greeting = '<p><b>Good day, Madam!<b></p>';
	}

I understand this no good as it does not account for checking isset and setting gender = NULL if the user hasn't selected M or F

Share this post


Link to post
Share on other sites

I think I had moment of clarity this morning! After re-reading the paragraph about elseif I presume elseif isn't classed as a nested conditional - rather adding a conditional to an existing one.

 

Anyway I managed it with the following code.

if (isset($_POST['gender']) && ($_POST['gender'] == 'M')) {
		$greeting = '<p><b>Good day, Sir!<b></p>';
	} elseif ((isset($_POST['gender']) && ($_POST['gender'] == 'F')) {
		$greeting = '<p><b>Good day, Madam!<b></p>';
	} else {
		$gender = NULL;
		echo '<p class="error">Gender should be either "M" or "F"!</p>';
}	

Share this post


Link to post
Share on other sites

Oops. I should have put in some /n to make the source code readable. Thought of it just after I posted. Apologies.

 

Thanks for the comments and for taking the time to reply.

The double quotes was just silly of me.

I wish I had thought of using the number _format that way. Just good style from a pro.

I shall take on board the variable names comments.

I have now got some more. The code for the ch 2 review and pursue is getting kind of big for posting here but I shall await your feedback on this. I suppose if it is just copied and pasted into and IDE it will be more readable.

I hope this may be of some use to someone.

I await your feedback with trepidation.

 

 

PHP version?

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

 

<title>PHP Version</title>

 

</head>

<body>

<?php #script chapter2 review and pursue page 73

 

phpinfo();//gives all details of php installed

echo '<br /> PHP version is ' . PHP_VERSION; //just the version

?>

</body>

</html>

 

All the rest of the form and handler:

 

pursue_form.html

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

 

<title>HTML Feedback Form</title>

<style type="text/css" title="text/css" media="all" >

label,legend{

font-weight: bold;

color: #300acc;

}

form{

background-color: #cccccc;

}

</style>

</head>

<body>

<!--script 2.1 form.html-->

 

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

 

<fieldset><legend>Please enter your information in the form below:</legend>

<p><label>Please complete all entries.</label></p>

<p><label>First Name: <input type="text" name="fname" size="20" maxlength="40" /></label></p>

<p><label>Last Name: <input type="text" name="lname" size="20" maxlength="40" /></label></p>

<p><label>Email address: <input type="text" name="email" size="40" maxlength="60" /></label></p>

<p><label for="gender">Gender: </label>

<input type="radio" name="gender" value="male" /> Male

<input type="radio" name="gender" value="female" /> Female</p>

<p><label>Age:

<select name="age">

<option value="0-24">Under 25</option>

<option value="25-64">Between 30 and 60</option>

<option value="65+">Over 60</option>

</select></label></p>

<p><label>How many times have you used the garage?

<select name="use">

<option value="1">First time</option>

<option value="<6">2 to 5 times</option>

<option value=">5">More than 5 times</option>

</select></label></p>

<p><label>How did we do? (choose one only): </label></p>

<p><input type="checkbox" name="quality[]" value="poor" />Poor</p>

<p><input type="checkbox" name="quality[]" value="average" />Average</p>

<p><input type="checkbox" name="quality[]" value="good" />Good</p>

<p><input type="checkbox" name="quality[]" value="great" />Great</p>

<p><input type="checkbox" name="quality[]" value="excellent" />Excellent</p>

 

<p><label>What work did we do on your last visit? Choose all that apply: </label></p>

<p><input type="checkbox" name="service[]" value="mot" />MOT</p>

<p><input type="checkbox" name="service[]" value="mot repairs" />MOT Repairs</p>

<p><input type="checkbox" name="service[]" value="air con recharge" />Recharge Air Conditioning</p>

<p><input type="checkbox" name="service[]" value="tyres" />Tyres</p>

<p><input type="checkbox" name="service[]" value="car servicing" />Car Servicing</p>

<p><input type="checkbox" name="service[]" value="other work" />Other Work</p>

 

<p><label>Any comments? What can we improve on? <textarea name="comments" rows="3" cols="40"></textarea></label></p>

</fieldset>

<p align="center"><input type="submit" value="Submit Information" /></p>

</form>

</body>

</html>

 

handle_pursue_form.php

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

 

<title>Form Feedback</title>

<style type="text/css" title="text/css" media="all" >

label,legend{

font-weight: bold;

color: #300acc;

}

form{

background-color: #cccccc;

}

.error{

color: #C00;

font-weight: bold;

}

</style>

</head>

<body>

<?php #script page 73 pursue. I have concatenated most of the page 73 questions in this one script

 

//validate the gender:

//rewrite gender conditional

//is this what was asked for or have I misunderstood the question?

 

//was anything selected and was it male or female

if(isset($_POST['gender']) && (($_POST['gender']=='male')) ){//value checked AND male

//value checked

$gender = $_POST['gender'];//male

$title = 'Mr.';

 

}elseif(isset($_POST['gender']) && $_POST['gender']=='female'){//value checked AND female

 

$gender = $_POST['gender']; //female

$title = 'Ms.';

 

}else{//radio button not checked

 

$gender = NULL;

$title_error = '<p class="error">Gender should be either male or female.</p>';//error message

 

}

 

//validate the first name

if (!empty($_POST['fname'])){//first name entered

 

$fname = $_POST['fname'];

 

}else{//nothing entered in the box

 

$fname = NULL;

$fname_error = '<p class="error">You did not enter your first name.</p>'; //error message

 

}

 

//validate the last name

if (!empty($_POST['lname'])){//last name entered OK

 

$lname = $_POST['lname'];

 

}else{//no last name entered

 

$lname = NULL;

$lname_error = '<p class="error">You did not enter your last name.</p>';//error message

 

}

 

//validate number of times garage used

//couldn't resist trivial use of switch function

if ((isset($_POST['use']))){

$use = $_POST['use'];

switch ($use) {

case '1':

$greeting = 'Nice to see a new customer ';

break;

case '<6':

$greeting = 'Great to see you back again';

break;

case '>5':

$greeting = 'Welcome back ';

break;

default:

$greeting_error = '<p class="error">You have not told us how many times you have used the garage.</p>';//no number of visits selected error message

break;

}

 

}else{//error message

 

$greeting_error = '<p class="error">Please indicate how many times you used the garage</p>';//no number of visits selected error message

 

}

//validate email

if(!empty($_POST['email'])){ //if the box is not empty

 

$email = $_POST['email'];

 

}else{//no mail address entered

 

$email = NULL;

$email_error = '<p class="error">You did not enter your email.</p>';

 

}

 

//validate the comments

if(!empty($_REQUEST['comments'])){//if the box is not empty

 

$comments = $_REQUEST['comments'];

 

}else{//no comments entered

 

$comments = NULL;

$comments_error = '<p class="error">You made no comments.</p>';//no comment error message

 

}

 

//validate age

if (isset($_POST['age'])){//selection made

 

$age = $_POST['age'];

 

if($age=='0-24'){

 

$age_message = '<p>Your age is given as under 25 years old. You may qualify for a student discount.</p>';

 

}elseif ($age =='25-64'){

 

$age_message = '<p>Your age is given as between 25 and 60. If you are in part time employment or unemployed, you may qualify for a reduced rate.</p>';

 

}elseif ($age == '65+'){

 

$age_message = '<p>Your age is given as above 65 years old, so you may qualify for our pensioners discount.</p>';

 

}else{

 

$age = NULL;

$age_error = '<p class="error">Please choose a value for your age.</p>';//no age chosen error message

 

}

}else{//nothing selected

 

$age = NULL;

$age_error = '<p class="error">You did not select an age.</p>';//error message

 

}

 

 

//how did we do? Array. If more than one box ticked give error message

if (isset($_POST['quality'])){//at least one box ticked

 

$quality = $_POST['quality'];

 

$num = count($quality); //trivial use of count: better as radio button

 

if ($num == 1){//if just one box ticked

 

foreach ($quality as $k => $v){

$quality_ok = '<p>You have found our service to be ' . $v . '.</p>';//this only works because only one value can be printed

}

 

 

}else{//too many boxes ticked

 

$quality = NULL;

$quality_error = '<p class="error">You ticked ' . $num . ' boxes. Please make only a single choice for quality of service.</p>';//error message using count function

 

}

}else{//no box ticked

 

$quality = NULL;

$quality_error = '<p class="error">You made no choice of quality of service. Please tick one box.</p>';//error message

 

}

 

//what services did we provide? Array

if (isset($_POST['service'])){//if box/boxes ticked

 

$service = ($_POST['service']);

sort ($service);//sort in alphabetical order

//shuffle($service);//randomly sort the list

$work_done = implode (' <br />', $service);//turn array into string

$work = '<p>We provided the following services last time you visited: <br /> ' . $work_done . '</p>';

 

}else{

 

$service = NULL;

$work_error = '<p class="error">Please choose at least one service.</p>';//error message

 

}

 

//if all OK, display information

if($fname && $lname && $greeting && $email && $age && $gender && $use && $comments && $quality && $service){

 

echo '<p>' . $greeting . ' ' . $title . ' ' . $fname . ' ' . $lname . ' and thank you for your comments:

<br /><br /><tt>' . $comments .'</tt> '. $work . $quality_ok . $age_message . '

We will reply to you at <i>'. $email . '</i></p>';

 

}else{//if it goes wahoonee shaped display error messages

 

echo $title_error . $fname_error . $lname_error . $greeting_error . $age_error . $work_error .$quality_error . $comments_error . $email_error .

'<p class="error">Please go back and complete the form.</p>';//concatenate error messages

}

 

?>

</body>

</html>

Hello I'm new around here and i was having trouble with the review and pursue chapter 2. I tried your code Graham and i was getting these errors when executing" Notice: Undefined variable: greeting_error in /var/www/html/php_sandbox/RnP_Chap2.php on line 194

 

Notice: Undefined variable: age_error in /var/www/html/php_sandbox/RnP_Chap2.php on line 194

 

Notice: Undefined variable: work_error in /var/www/html/php_sandbox/RnP_Chap2.php on line 194". I am seeing for my self that the variables are defined but i don't understand why it's giving me these errors. Please i will need some guidance.

Share this post


Link to post
Share on other sites

The problem is you're outputting all the error messages if ANY omission is made, but the error variables are only defined when a single omission is made. In other words, for example, if someone doesn't select a "service", it'll still try to print out $age_error, even though the age was properly selected. 

 

You could avoid this by setting all the error variables to an empty string before going through the validation conditionals. Or you could use one error message, initially set it to an empty string, and then concatenate additional error messages to it, then output just it.

  • Upvote 1

Share this post


Link to post
Share on other sites

Please I have some worries to understand Chapter 10 of the book in the subsection from paginating query requests: I understand the $_GET['value'] variable holds the value was passed to the browser but how does the script knows that it is supposed to pass 'p' or 's' or 'sort' to the browser.

Share this post


Link to post
Share on other sites

You can name those whatever you want. They're just variables defined by the script, used in the URL, and then referenced again by the script. You could call them "larry", "moe", and "curly" if you wanted!

Share this post


Link to post
Share on other sites

Ok

Thanks very much Larry for your prompt answer.

By the way very nice book it has turned me from a non-programmer to someone who is now dreaming in code!!!!

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...