Jump to content
Larry Ullman's Book Forums

Recommended Posts

Ok so I am going through the Review and Pursue questions at the end of chapter 9 and one of them is quite confusing to me. The question is:

 

Rewrite customize.php so that the script also applies the user's preferences. Hint: you need to take into account the fact that cookies aren't available immediatetly after they've been set. Instead, you would write the CSS code using the $_GET values after the form has been submitted, and the $_COOKIE values upon first arriving at the page (if the cookie exists), and the default values otherwise.

 

Can someone help me clarify what is meant by writing the CSS codes for the customize.php script. The last sentence in the question is confusing to me.

 

Any help is appreciated.

 

Thank you,

 

Alex

Share this post


Link to post
Share on other sites

Sure thing, Alex. This is what I meant...

 

Here's the key code from view_settings.php:

 

<style type="text/css">
body {
<?php // Script 9.2 - view_settings.php

// Check for a font_size value:
if (isset($_COOKIE['font_size'])) {
print "\t\tfont-size: " . htmlentities($_COOKIE['font_size']) . ";\n";		
} else {
print "\t\tfont-size: medium;";
}

// Check for a font_color value:
if (isset($_COOKIE['font_color'])) {
print "\t\tcolor: #" . htmlentities($_COOKIE['font_color']) . ";\n";
} else {
print "\t\tcolor: #000;";
}

?>
}
</style>

 

So to get that to work in customize.php, which also needs to be aware of the posted form, the code would be:

 

 

<style type="text/css">
body {
<?php // Script 9.2 - view_settings.php

// Check for a font_size value:
if (isset($_COOKIE['font_size'])) {
print "\t\tfont-size: " . htmlentities($_COOKIE['font_size']) . ";\n";		
} elseif (isset($_POST['font_size']) {
print "\t\tfont-size: " . htmlentities($_POST['font_size']) . ";\n";		
} else {
print "\t\tfont-size: medium;";
}

// Check for a font_color value:
if (isset($_COOKIE['font_color'])) {
print "\t\tcolor: #" . htmlentities($_COOKIE['font_color']) . ";\n";
} elseif (isset($_POST['font_color']) {
print "\t\tcolor: #" . htmlentities($_POST['font_color']) . ";\n";
} else {
print "\t\tcolor: #000;";
}

?>
}
</style>

 

Note that there's an error in the prompt, in that the form uses POST, not GET. Let me know if you still have questions about this.

Share this post


Link to post
Share on other sites

I too have a the same question (and to be honest, as I started chapter 8, I began to feel out of my league - so I am in severe need of step by step instructions come this chapter until some of these concepts hit home)

Here is what I have - my view_settings php file looks like this (which I got help from this forum) and it is not working:

 

<style type="text/css">

body {

<?php // Pursue # 9 - Customize rewrite - view_settings.php

 

// Check for a font_size value:

 

if (isset($_COOKIE['font_size'])) {

print "\t\tfont-size: " . htmlentities($_COOKIE['font_size']) . ";\n";

} elseif (isset($_POST['font_size']) {

print "\t\tfont-size: " . htmlentities($_POST['font_size']) . ";\n";

} else {

print "\t\tfont-size: medium;";

}

 

// Check for a font_color value:

if (isset($_COOKIE['font_color'])) {

print "\t\tcolor: #" . htmlentities($_COOKIE['font_color']) . ";\n";

} elseif (isset($_POST['font_color']) {

print "\t\tcolor: #" . htmlentities($_POST['font_color']) . ";\n";

} else {

print "\t\tcolor: #000;";

}

 

?>

}

</style>

 

</head>

<body>

 

<p><a href="customize.php">

Customize Your Settings</a></p>

<p><a href="reset.php">Reset your Settings</a></p>

 

<p> Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda Yadda </p>

</body>

</html>

 

My customize looks like this:

<?php // Script 9.1 - Cutomize.php

 

// Handle the form if it has been submitted:

if (isset($_POST['font_size'], $_POST['font_color'])) {

 

 

// Send the cookies:

setcookie('font_size', $_POST['font_size'], time()+1000000, '/', '', 0);

setcookie('font_color', $_POST['font_color'], time()+1000000, '/', '', 0);

 

//Message to be printed later:

$msg = '<p> Your settings have been entered! Click <a href="view_settings.php"> here</a> to see them in action.</p>';

 

} // End of submitted IF.

?><!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>Customize Your Settings</title>

</head>

<body>

<?php // If the cookies were sent, print a message.

if (isset($msg)) {

print $msg;

}

?>

 

<p>Use this form to set your preferences:</p>

 

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

<select name="font_size">

<option value="">Font Size</option>

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

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

<option value="small">small</option>

<option value="medium">medium</option>

<option value="large">large</option>

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

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

</select>

<select name="font_color">

<option value="">Font Color</option>

<option value="999">Gray</option>

<option value="0c0">Green</option>

<option value="00f">Blue</option>

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

<option value="000">Black</option>

</select>

<input type="submit" name="submit" value="Set My Preferences" />

</form>

 

</body>

</html>

Share this post


Link to post
Share on other sites

When you say it is "not working", what does that mean? What happens that shouldn't happen? What doesn't happen that should? Also, per the forum guidelines, what version of PHP are you using?

Share this post


Link to post
Share on other sites

Well... First of all Dreamweaver is saying that I have an error on line 16 which is my } elseif (isset($_POST['font_size']) { and line 18 which is my } else { and it's also erroring on 25 and 27 which is the } elseif (isset($_POST['font_color']) { and my } else { I can't believe that he script isn't right because I copied it from the previous post. Unless I am missing something? Am I supposed to change my customize.php fil at all? The only file I changed is my view_settings.php fil.e

 

Like I said, I feel like I am in over my head. I really like your book, I am just not comprehending some of these concepts and the order of things .... and the order of the syntax.

 

 

By not working I mean, When I run the script in the browser, my colors do not change as per the instructions - aren't my font color and my font size suppose to dynamically change? Maybe I didn't understand the instructions, but nothing changes.

 

PHP Version 5.3.5

Share this post


Link to post
Share on other sites

I've read through this thread but I'm still not sure what the goal of this Pursue problems is supposed to be. It says to rewrite it so that the "script also applies the user's preferences". Can anyone clarify this for me. What should the end result look like? I mean, I see the code you wrote out, Larry, but I'm not sure what it is actually supposed to do. When I run my scripts, I see no difference.

Share this post


Link to post
Share on other sites

Okay, here's what I was thinking: as written, only view_settings.php shows the users settings (i.e., shows the page using the user's preferences). The goal was to get the customize.php page to also reflect the user's preferences.

Share this post


Link to post
Share on other sites

H Larry,

 

I too am struggling to understand the objective of this task.

 

This is what i do understand from the orginal example:

 

The customize.php shows the drop-down boxes where the user picks the font size and color if cookies do not exist.

On submit(page load) it then checks to see if any cookies exist by checking the $msg variable.

If true it displays the message with link to view_settings.php.

View_setting.php then shows the formatted text with links back to customize.php and reset.php.

 

So, dosn't the view_settings.php page already reflex the users preferences, hence shows the formatted text?

 

Please explain as I'm a little confused :)

Share this post


Link to post
Share on other sites

Sorry for the confusion. I made a mistake in the above, and have edited it. To be more clear, the customize.php script as written does not reflect the user's preferences for font size and color. That makes sense when the page is first loaded and no cookies exist. But after the form has been submitted, the page could reflect the user's preferences then. And after the user goes to view_settings.php and comes back to customize.php, the customize.php script could reflect the user's preferences then, too.

Share this post


Link to post
Share on other sites

I did it a little differently. Also here's the code for "Make the form in customize.php sticky, so that it reflects the user's current choices." I'm a total newbie when it comes to this stuff and it took me awhile to figure it out. Some how it works though so :D

<?php // Script 9.3 - customize.php #2
// Handle the form if it has been submitted:
if (isset($_POST['font_size'], $_POST['font_color'])) {

// Send the cookies:
setcookie('font_size', $_POST['font_size'], time()+10000000, '/');
setcookie('font_color', $_POST['font_color'], time()+10000000, '/');
// Message to be printed later:
$msg = '<p>Your settings have been entered! Click <a href="view_settings.php">here</a> to see them in action.</p>';

} // End of submitted IF.
?><!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>Customize Your Settings</title>
 <style type="text/css">
body {
<?php // Script 9.2 - view_settings.php
// Check for a font_size value:
if (isset($_POST['font_size'])) { print "\t\tfont-size: " . htmlentities($_POST['font_size']) . ";\n"; }
elseif (isset($_COOKIE['font_size'])) {
print "\t\tfont-size: " . htmlentities($_COOKIE['font_size']) . ";\n";
} else {
print "\t\tfont-size: medium;";
}
// Check for a font_color value:
if (isset($_POST['font_size'])) {print "\t\tcolor: #" . htmlentities($_POST['font_color']) . ";\n";}
elseif (isset($_COOKIE['font_color'])) {
print "\t\tcolor: #" . htmlentities($_COOKIE['font_color']) . ";\n";
} else {
print "\t\tcolor: #000;";
}
?>
 }
		</style>
</head>
<body>
<?php // If the cookies were sent, print a message.
if (isset($msg)) {
print $msg;
}
?>
<p>Use this form to set your preferences:</p>
<form action="customize.php" method="post">
<select name="font_size">
<option value="">Font Size</option>
<option value="xx-small" <?php
		if (isset($_POST['font_size']) && $_POST['font_size'] == 'xx-small')
		{
			print "selected";
		}
		elseif (isset($_COOKIE['font_size']) && $_COOKIE['font_size']=='xx-small')
		{
			print "selected";
		}
		?>>xx-small</option>
<option value="x-small" <?php
if (isset($_POST['font_size']) && $_POST['font_size'] == 'x-small')
		{
			print "selected";
		}
		elseif (isset($_COOKIE['font_size']) && $_COOKIE['font_size']=='x-small')
		{
			print "selected";
		}

		?>>x-small</option>
<option value="small" <?php
if (isset($_POST['font_size']) && $_POST['font_size'] == 'small')
		{
			print "selected";
		}
		elseif (isset($_COOKIE['font_size']) && $_COOKIE['font_size']=='small')
		{
			print "selected";
		}

		?>>small</option>
<option value="medium" <?php
if (isset($_POST['font_size']) && $_POST['font_size'] == 'medium')
		{
			print "selected";
		}
		elseif (isset($_COOKIE['font_size']) && $_COOKIE['font_size']=='medium')
		{
			print "selected";
		}

		?>>medium</option>
<option value="large" <?php
if (isset($_POST['font_size']) && $_POST['font_size'] == 'large')
		{
			print "selected";
		}
		elseif (isset($_COOKIE['font_size']) && $_COOKIE['font_size']=='large')
		{
			print "selected";
		}

		?>>large</option>

<option value="x-large" <?php
if (isset($_POST['font_size']) && $_POST['font_size'] == 'x-large')
		{
			print "selected";
		}
		elseif (isset($_COOKIE['font_size']) && $_COOKIE['font_size']=='x-large')
		{
			print "selected";
		}

		?>>x-large</option>
<option value="xx-large" <?php
if (isset($_POST['font_size']) && $_POST['font_size'] == 'xx-large')
		{
			print "selected";
		}
		elseif (isset($_COOKIE['font_size']) && $_COOKIE['font_size']=='xx-large')
		{
			print "selected";
		}

		?>>xx-large</option>
</select>
<select name="font_color">
<option value="">Font Color</option>
<option value="999">Gray</option>
<option value="0c0">Green</option>
<option value="00f">Blue</option>
<option value="c00">Red</option>
<option value="000">Black</option>
</select>
<input type="submit" name="submit" value="Set My Preferences" />
</form>
</body>
</html>

Share this post


Link to post
Share on other sites

Sure thing, Alex. This is what I meant...

 

Here's the key code from view_settings.php:

 

<style type="text/css">
body {
<?php // Script 9.2 - view_settings.php

// Check for a font_size value:
if (isset($_COOKIE['font_size'])) {
print "		font-size: " . htmlentities($_COOKIE['font_size']) . ";\n";		
} else {
print "		font-size: medium;";
}

// Check for a font_color value:
if (isset($_COOKIE['font_color'])) {
print "		color: #" . htmlentities($_COOKIE['font_color']) . ";\n";
} else {
print "		color: #000;";
}

?>
}
</style>

 

So to get that to work in customize.php, which also needs to be aware of the posted form, the code would be:

 

 

<style type="text/css">
body {
<?php // Script 9.2 - view_settings.php

// Check for a font_size value:
if (isset($_COOKIE['font_size'])) {
print "		font-size: " . htmlentities($_COOKIE['font_size']) . ";\n";		
} elseif (isset($_POST['font_size'])) {
print "		font-size: " . htmlentities($_POST['font_size']) . ";\n";		
} else {
print "		font-size: medium;";
}

// Check for a font_color value:
if (isset($_COOKIE['font_color'])) {
print "		color: #" . htmlentities($_COOKIE['font_color']) . ";\n";
} elseif (isset($_POST['font_color'])) {
print "		color: #" . htmlentities($_POST['font_color']) . ";\n";
} else {
print "		color: #000;";
}

?>
}
</style>

 

Note that there's an error in the prompt, in that the form uses POST, not GET. Let me know if you still have questions about this.

 

When I tried the above, I found that when returning to customize.php from view_settings.php, the correct settings would be present on the "Use this form to set your preferences" text, but when selecting the font size and color on customize.php and hitting the "Set my preferences!" submit button, the previous settings would be in effect, not the ones just selected.

 

Was this due to the conditional structure which looks at the $_COOKIE array before the $_POST array? When I switched these and made it look at the $_POST array first, and then 'elseif' look at the $_COOKIE array, it behaved as expected.

 

I'm very new at this and am trying to make sure I understand...

Share this post


Link to post
Share on other sites

Hello Larry,

 

sorry to have to ask another question. I do understand that in html one

has to put a backslash to escape characters. However I do not understand

the use of

\t\tfont-size
and
\t\tfont-color

Is this supposed to be for two tabs? I tried with and without the \t\tfont-etc. and I don't

see any difference.

 

Many thanks

Share this post


Link to post
Share on other sites

Yes, that does create two tabs--as you suspect--but it does so in the HTML source of the page! Sorry for the confusion!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×