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
counting clicks
Moderator: General Moderators
counting clicks
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
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
Do it all in sql. you need a single table like so:
Then code something like this to put on each page:
Code: Select all
CREATE TABLE clicks (
page VARCHAR(255) NOT NULL unique,
hits int(8) NOT NULL default '0',
PRIMARY KEY(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
-
andylyon87
- Forum Contributor
- Posts: 168
- Joined: Sat Jan 31, 2004 5:31 am
- Location: Dundee
Click counter as previously mentioned:
(only works if you use include or require, it basically relies on one page reloading on every click)
this does work, but it isn't my scripting its someone elses off this forum
(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>"); }
?>