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
https://forums.phpfreaks.com/topic/246293-incrementing-counter/
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.

<?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

 

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

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.