Wrox Programmer Forums
|
BOOK: Beginning PHP 6, Apache, MySQL 6 Web Development ISBN: 9780470391143
This is the forum to discuss the Wrox book Beginning PHP 6, Apache, MySQL 6 Web Development by Timothy Boronczyk, Elizabeth Naramore, Jason Gerner, Yann Le Scouarnec, Jeremy Stolz; ISBN: 9780470391143
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning PHP 6, Apache, MySQL 6 Web Development ISBN: 9780470391143 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 25th, 2010, 11:03 AM
Registered User
 
Join Date: Aug 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Exercise 6 for chapter 2

I have been trying to figure out how to implement the example code for the exercise that displays how many times a page has been viewed using functions. I have typed the code as shown but when I view the page it always just says I have viewed the page 1 time(s). I think somehow I need to initialize a counter for this to work. Below I have included the code for you to look at. I am enjoying the book so far and would greatly appreciate any help.

the code I have typed is as follows:
Code:
<?php
function display_times($num) {
echo '<h1>You have viewed this page ' . $num . ' time(s).</h1>';
}

//get the cookie value and add 1 to it for this visit
$num_times = 1;
if (isset($_COOKIE['num_times'])) {
$num_times = $_COOKIE['num_times'] + 1;
}

//set the value back to the cookie for the next time
setcookie('num_times', '&num_times', time() + 60);
?>
<html>
<head>
<title>Viewed Times</title>
</head>
<body>
<?php
display_times($num_times);
?>
</body>
</html>
thanks again
 
Old August 26th, 2010, 01:45 PM
Registered User
 
Join Date: Aug 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I GOT IT!!!!
I created a $_SESSION['counter'] = 0 in movie1.php
Then in moviesite.php I placed a condition on the $_SESSION['authuser'] that
would add 1 to $_SESSION['counter'] and save that value as a new variable "$viewed." Then the function reset the value of $_SESSION['counter'] to = $viewed.
It worked! PRODIGIOUSLY COOL!!!!
 
Old August 26th, 2010, 01:51 PM
Registered User
 
Join Date: Aug 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Scratch the celebration.
the counter increments okay. but it goes back to zero when the authorized user logs out. oh well. back to the drawing board.
 
Old August 27th, 2010, 11:24 AM
Registered User
 
Join Date: Aug 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

found a function that works. although i don't understand why it works without the curly brackets. nothing like parading your ignorance in front of the world.
here is the code that seems to work:
Code:
<?php
session_start();  
if(isset($_SESSION['views'])) 
    $_SESSION['views'] = $_SESSION['views']+ 1;

else 
    $_SESSION['views'] = 1;

echo "views = ". $_SESSION['views'];
?>
hope somebody gets some help from this.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 4 Exercise 4 Will BOOK: Beginning Microsoft Visual C# 2008 ISBN: 978-0-470-19135-4 2 September 18th, 2009 11:08 AM
chapter 3 exercise 4 walkamongus BOOK: Beginning JavaScript 3rd Ed. ISBN: 978-0-470-05151-1 0 September 10th, 2007 09:34 PM
Chapter 5 - Exercise 1 scgtman BOOK: Beginning Visual Basic 2005 Databases ISBN: 978-0-7645-8894-5 3 May 16th, 2006 08:10 PM
chapter 9 exercise michaelcode BOOK: Beginning ASP.NET 1.0 1 April 9th, 2006 03:56 PM
Chapter 3 Exercise 3 Matt WAXON BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 3 July 4th, 2005 02:19 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.