Hit Counter

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
vchris
Forum Contributor
Posts: 204
Joined: Tue Aug 30, 2005 7:53 pm
Location: Canada, Quebec

Hit Counter

Post by vchris »

How would I create a hit counter that works with a db.

I tried to create one where everytime a user opened the index.php page it updated the countid value by 1 and then I was doing a select to display the countid but it didn't really work and I'm sure there is an easier way.

Anyone got an idea?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

rough idea

Code: Select all

$q = mysql_query('SELECT `viewCount` FROM `pageViews` WHERE `pageID` = \'23\'') or die(mysql_error());
$count = mysql_fetch_assoc($q);
$count = intval(array_shift($count))+1;
$u = mysql_query('UPDATE `pageViews` SET `viewCount` = `viewCount` + 1 WHERE `pageID` = \'23\'') or die(mysql_error());

echo $count;
where 23 is the ID you've given the "this" page..


oops... made some boo-boos on the code.
Last edited by feyd on Fri Sep 23, 2005 6:58 pm, edited 1 time in total.
vchris
Forum Contributor
Posts: 204
Joined: Tue Aug 30, 2005 7:53 pm
Location: Canada, Quebec

Post by vchris »

So the record pageid would by a different id for every page in my site so I can know where all the users are going?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

depending on how you do logging yes.. but the counter will merely count for each page id you give it..
vchris
Forum Contributor
Posts: 204
Joined: Tue Aug 30, 2005 7:53 pm
Location: Canada, Quebec

Post by vchris »

It works perfectly! :D

Thanks buddy!
Post Reply