counting clicks

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
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

counting clicks

Post by fresh »

hi,

sorry to ask so many questions, but I was wanting to count clicks and display the amount of clicks on my page... I seen examples of this, but, none of them were sql. I want to make a table named 'attempts' and throw in it an integer starting at 0 and upon each user click the script would create a file if not existant and would add 1 to the value of that file after each click... I plane to do this with many links, each with there own txt file... anyone know of any good tutorials with examples, or feel like posting any??? I'd appreciate it... thanks ;)
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Do it all in sql. you need a single table like so:

Code: Select all

CREATE TABLE clicks (
   page VARCHAR(255) NOT NULL unique,
   hits int(8) NOT NULL default '0',
   PRIMARY KEY(page)
);
Then code something like this to put on each page:

Code: Select all

//database connection code here

$page = $_SERVER['PATH_INFO']
$result = mysql_query("SELECT count(hits) FROM clicks WHERE page = '$page'");
$result = mysql_fetch_assoc($result);
if($result['count(hits)']){
   mysql_query("UPDATE clicks SET hits = hits+1 WHERE page = '$page'");
}else{
   mysql_query("INSERT INTO clicks (page) VALUES ('$page')");
}

echo $result['count(hits)']." people have visited this page.";
andylyon87
Forum Contributor
Posts: 168
Joined: Sat Jan 31, 2004 5:31 am
Location: Dundee

Post by andylyon87 »

I use a click counter that isnt it actually just adds one everytime the index page reloads and that works without sql thanks to the guys on here
andylyon87
Forum Contributor
Posts: 168
Joined: Sat Jan 31, 2004 5:31 am
Location: Dundee

Post by andylyon87 »

Click counter as previously mentioned:
(only works if you use include or require, it basically relies on one page reloading on every click)

Code: Select all

<?php
function click(){
   //   Hit Counter 
$click_file = 'php/form_data/clicks.txt';
if (file_exists($click_file)){
$click = file_get_contents($click_file);
if (is_numeric($click)){$click++;}else{$click = 0;}
$fp = fopen($click_file, 'w');
fwrite($fp, $click);
fclose($fp);}
print("<B>Clicks:[$click]</B>"); }
?>
this does work, but it isn't my scripting its someone elses off this forum
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

hey

Post by fresh »

thanks guys, I'm just wanting to count how many times somone views a page, via href= click, I think this script will work fine, and it's SQL!!! Awesome, thanks!! :)
Post Reply