Jump to content

Incrementing Counter


doubledee

Recommended Posts

I am trying to keep track of how many times a page is loaded for debugging.

 

Here is my code but it doesn't seem to work?!

 

<?php
isset($counter) ? $counter++ : $counter=1;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

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

<body>
<h3>Add a Comment</h3>

<form action="Processing.php" method="post">
	<fieldset>
		<!-- Comment -->
		<label>Comment:</label><br />
		<textarea cols="50" rows="15"><?php echo $counter; ?></textarea>
		<br />

		<!-- Submit Form -->
		<input type="submit" name="addComment" id="addComment" value="Add a Comment"/>
	</fieldset>
</form>
</body>
</html>

 

What am I doing wrong?

 

 

Debbie

Link to comment
Share on other sites

<?php session_start();
if(isset($_SESSION['counter'])) {
            $_SESSION['counter']++;
        } else {
            $_SESSION['counter'] = 1;
        }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

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

<body>
<h3>Add a Comment</h3>

<form action="Processing.php" method="post">
	<fieldset>
		<!-- Comment -->
		<label>Comment:</label><br />
		<textarea cols="50" rows="15"><?php echo $_SESSION['counter']; ?></textarea>
		<br />

		<!-- Submit Form -->
		<input type="submit" name="addComment" id="addComment" value="Add a Comment"/>
	</fieldset>
</form>
</body>
</html>

 

James.

Link to comment
Share on other sites

<?php
isset($counter) ? $counter++ : $counter=1;
?>

 

the idea is right..

 

However, php doesn't store variables past the pages load. You will have to make use of sessions or cookies to achieve this.

 

My brain isn't working today.  :-[

 

How would I use a Session with this?

 

 

Debbie

 

Link to comment
Share on other sites

Why doesn't this work??

 

<?php
session_start();


isset($_SESSION['$counter']) ? $_SESSION['$counter']++ : $_SESSION['$counter']=1;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

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

<body>
<h3>Add a Comment</h3>

<form action="Processing.php" method="post">
	<fieldset>
		<!-- Comment -->
		<label>Comment:</label><br />
		<textarea cols="50" rows="15"><?php echo $_SESSION['counter']; ?></textarea>
		<br />

		<!-- Submit Form -->
		<input type="submit" name="addComment" id="addComment" value="Add a Comment"/>
	</fieldset>
</form>
</body>
</html>

 

 

Debbie

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.