Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP & MySQL Hit Counter

Status
Not open for further replies.

Snakeage

Programmer
Jun 12, 2003
4
GB
Hi there, can someone help me out with a php hit counter where the hits are stored in a MySQL database. I have had a look at a few examples of this type of hit counter before but none of them are exactly what I want. First of all, i can connect to the database and access the right table im just not sure of the query i should use and how to show the right data. The problem that I am having is that the pages of my website are loaded by the different variables which I put into the URL, i.e. This opens the Programming text file and adds it into the main page window using the $page variable. Is it possible to use this variable with the hit counter? So that when someone goes to the Programming page, the code searches for the Programming record and updates the hit by one, and I also want this to happen for all the other pages respectively. Can someone help me out with the code I would use for this as I am stuck on the type of query to use and how to actually work out the right page and display the right pages record on that page of the site. Thanks for any help, Sam Clark
 
Create a table with two columns: parameter and count.

The "parameter" column would include your input parameters, such as "Programming" from your example. The count, is, of course, your count.

To increment the counter for "Programming" issue:

update tablename set count = count + 1 where parameter = 'Programming'

Want the best answers? Ask the best questions: TANSTAAFL!
 
Hi there, thanks for the reply. Can i ask about your statement you made,

update tablename set count = count + 1 where parameter = 'Programming'

Is this the query I would use? And also would I be able to replace 'Programming' with $page so that the counter works on all the pages of my site?

Can I also ask how I would use the values in the database to display the results (How many hits from that page)?
 
Yes, that is the query you would use.

Yes, you can build that query using PHP's string concatenation operator (".":

If you wanted to fetch the number of hits for the page "Programming":

select count from tablename where parameter = 'Programming'

Want the best answers? Ask the best questions: TANSTAAFL!
 
Hi there, I have it working, i replaced the Programming with $page and it works fine

update counter set count = count + 1 where parameter = '$page'

I am now wondering if it is possible to create a new record of $page if one doesnt exist. So when the page index.php?page=Programming then this looks for the Programming record and updates it by one. But if i go to index.php?page=Home then this doesnt get a hit because there is not yet a record for it. Is it quick and easy to add something to the query for this or would it be quicker just too add all of the pages on the site into the database manually? Thanks
 
Try selecting where parameter = '$page' which means "Home"
and the result row would be 0 because there is no Home yet

so eg. if(mysq_num_rows($result)<1) { // page/parameter not found
use the query
$sql = Insert into counter values(1,'$page');

but what if the user simply enter the page, then you'll have a lot of bogus records

like


where text IAMGOD does not exist. You need to check whether the text file is loading properly / exist. Then after that you insert . I think!

Regards,

Namida
 
Thanks Namida, i see your point about having bogus records on the database so I have decided to add them all in myself. THe only thing is that I can't figure out how to display the number of hits. At the moment I have this;

$query = &quot;update counter set count = count + 1 where parameter = '$page'&quot;;
$result = mysql_query($query);
echo(&quot;$count&quot;);

The query works fine and the second statement seems to work fine too. Its just how to display the data. I thought i could just echo the $count variable which is the field from the database. How can I do this?
 
nope
you have to query from the database
$count doesn't exist yet

You need to :
$query = &quot;select count from counter where parameter = '$page'&quot;;
$resultcount = mysql_query($query);
$count = mysql_fetch_array($resultcount); //fetch the row from the result
echo $count[&quot;count&quot;]; // display the field name count from the row you fetched


Do this after you update to make sure that the current view is included in the count

Regards,

Namida
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top