Page 1 of 1

counting clicks

Posted: Sat Jul 10, 2004 5:03 am
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 ;)

Posted: Sat Jul 10, 2004 6:24 am
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.";

Posted: Sat Jul 10, 2004 2:21 pm
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

Posted: Sat Jul 10, 2004 2:23 pm
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

hey

Posted: Sat Jul 10, 2004 3:43 pm
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!! :)