------------------------------------------------------------------------------
MC logo
Counter Demo
[^] PHP Listings
------------------------------------------------------------------------------
counter.php       countform.html
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Count Results</title>
    <link rel="stylesheet" href="../lit/genstyle.css" type="text/css" />
    <style>
      p { text-align: center; }
    </style>
  </head>
  <body>
   <h1>Count Status</h1>
   <?php
	// Open the database file.  The first parameter is the file name of
	// the database, the "c" means to create it if does not previously
	// exist, and "db4" is the type of datbase file used.  (Mostly, you
	// just have to pick something that's installed on the system you're
	// using.)
	$db = dba_open("/var/wwwwrite/bennet/counts", "c", "db4");
	if(!$db) {
		echo "<p style='color: red'>Database open failed</p>";
		echo "</body></html>";
		exit;
	}

	// Read the values we're intrested in.
	$tot = dba_fetch("total", $db);
	$cnt = dba_fetch("cnt", $db);

	// Update the values.
	$tot += $_REQUEST["amt"];
	$cnt = $cnt + 1;

	// Save them back to the database.
	dba_replace("total", "$tot", $db);
	dba_replace("cnt", "$cnt", $db);

	// The system will close automatically at the end of the script,
	// but since we're done with it now it's better to say so.  This
	// allows other threads to use the file if they need to.
        dba_close($db);

	// Display.
	$av = $tot/$cnt;
	echo <<<END
	<p>
	  This page has counted $cnt submissions for a total of $tot.
	  The average submission is $av.
	</p>
END;

        // If present, provide a return link.
	if($_SERVER["HTTP_REFERER"]) {
		echo '<p><a href="'.$_SERVER['HTTP_REFERER'].
			'">Return to Form</a></p>';
	}
    ?>
  </body>
</html>