Trying to Create Record-Specific Session-Based Hit Counter
Posted: Sat Jun 23, 2007 2:35 pm
I am trying to create a kind of session-based hit counter that will work with an existing database table of mine. The current database table already contains a number of fields, one of which is the "Articles" field, in which are stored (appropriately enough) various articles that folks can read (by accessing the database via clicking on the appropriate link on my site).
I would like now to add a numeric field to my table and call it "Count" and to have it increment by 1 each time a new site visitor accesses the database record with which it is associated.
In other words, if five different visitors click on the following link on my site....
(id.php being a short script that displays the Article in question and associated info)...
I would like the database record that includes ID 27 to have a Count field that now reads 5 as a result of those visits.
It seemed relatively easy to me to do this at first (and I think I even had it working for a moment, but I sort of "lost my place") but I've been working on it all morning and can't get it right.
Below you will find my latest TEST.PHP file, where, as you see, I've manually assigned the ID field as '1' in the hopes of getting the following script to augment the "Count" field for my first record by '1' every time I run it (or rather every time I run the script after first clearing my session data from my browser, of course, so that I'm a new visitor from the script's point of view).
The following script will successfully echo the "Title" field, but nothing appears for the "Count" field (though I have, of course, already added the Count field to my table -- as a VARCHAR type).
(I found the session-related part of the following script online under "simple hit counter.")
When I run the above script, the Article Title appears correctly; however, nothing at all appears for "Count" -- not even a zero.
My goal, once again, is to have each record in my database contain a "Count" field whose value will indicate the number of visitors who have accessed the record with which it is associated (i.e., who have accessed the record by having clicked on the sort of sample html link that I showed above).
I hope I've made the situation at least somewhat clear to you guys. Please let me know if you need any clarifications. And thanks in advance for any and all help!
Brian
I would like now to add a numeric field to my table and call it "Count" and to have it increment by 1 each time a new site visitor accesses the database record with which it is associated.
In other words, if five different visitors click on the following link on my site....
Code: Select all
<a href="http://www.mysite.com/php/id.php?ID=27">Article Number 27</a>I would like the database record that includes ID 27 to have a Count field that now reads 5 as a result of those visits.
It seemed relatively easy to me to do this at first (and I think I even had it working for a moment, but I sort of "lost my place") but I've been working on it all morning and can't get it right.
Below you will find my latest TEST.PHP file, where, as you see, I've manually assigned the ID field as '1' in the hopes of getting the following script to augment the "Count" field for my first record by '1' every time I run it (or rather every time I run the script after first clearing my session data from my browser, of course, so that I'm a new visitor from the script's point of view).
The following script will successfully echo the "Title" field, but nothing appears for the "Count" field (though I have, of course, already added the Count field to my table -- as a VARCHAR type).
(I found the session-related part of the following script online under "simple hit counter.")
Code: Select all
<?php session_start();
include 'connect.php';
$ID = '1';
if (!session_is_registered("counted")){ mysql_query("UPDATE Articles SET count=(count + 1) WHERE ID LIKE '$ID'"); session_register("counted"); }
$sql="SELECT * FROM Articles WHERE ID LIKE '$ID'" ;
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
echo $row['Title'];
echo $row['count'];
}
?>My goal, once again, is to have each record in my database contain a "Count" field whose value will indicate the number of visitors who have accessed the record with which it is associated (i.e., who have accessed the record by having clicked on the sort of sample html link that I showed above).
I hope I've made the situation at least somewhat clear to you guys. Please let me know if you need any clarifications. And thanks in advance for any and all help!
Brian