Jump to content


need some help w/ this click counter


5 replies to this topic

#1 markc1822

    Young Padawan

  • Members
  • Pip
  • 33 posts
  • Gender:Male
  • Location:New York

Posted 24 January 2007 - 08:11 PM

<?
$url = $_SERVER['REQUEST_URI'];
$a123 = explode("?url=", $url);
$url = $a123[1];
$date = date("d/m/y"); // Grabs date

/* MYSQL CONNECT */
$db_host = "localhost";
$db_username = "username"; // Will most likely need your username as a prefix
$db_password = "password";
$db_name = "database"; // Will most likely need your username as a prefix
mysql_connect($db_host,$db_username,$db_password) or die(mysql_error()); // This just connects to your server, will give an error message if it cant
mysql_select_db($db_name) or die(mysql_error()); // This selects the database to work on, this will give an error if it cant aswell

$query = "SELECT * FROM clicks WHERE url='$url'"; // Query to find the url if it already exists
$result = mysql_query($query);
while($r=mysql_fetch_array($result))
{
$clicks = "$r[clicks]";
$clicks++; // Adds one click to the total clicks
$end = "okay"; // This just tells the script that the url already exists and doesnt need to add a new entry
$query = "UPDATE `clicks` SET `date` = '$date', `clicks` = '$clicks' WHERE `id` = '$r[id]' LIMIT 1"; // Query to add a click to the url
mysql_query($query); // Add one more click to the url
}
if ($end != okay) { // If the url isnt already in the databse run the following lines
$query = "INSERT INTO clicks (url, clicks, date)
VALUES ('$url','1','$date')"; // Adds the url to the databse to start recoding clicks
mysql_query($query);
}
header("Location: $url"); // Go To the link that you clicked on


// For a tutorial on displaying the clicks with a nice per page system please ask me to add it :o
?>

i need some hep with this script from http://zulumonkey.org. How would i display how many times the link was clicked on my site.

Edited by markc1822, 24 January 2007 - 08:12 PM.


#2 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 24 January 2007 - 09:33 PM

Basic MySQL functions.

$query = mysql_query("SELECT COUNT(*) FROM `clicks` WHERE `url` = '$url'") or die(mysql_error());
$clicks = mysql_fetch_row($query);
echo "This link has been clicked <strong>{$clicks[0]}</strong> times.";

Of course would be put into a loop for echoing each one if that's how you are using the thing.

#3 markc1822

    Young Padawan

  • Members
  • Pip
  • 33 posts
  • Gender:Male
  • Location:New York

Posted 25 January 2007 - 08:05 PM

thanks for the reply, What you just said is so alien to me i have no idea where to start with that. When it comes to PHP i am a noob, if you can explain it alittle more i would appreciate it.

Thanks

#4 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 25 January 2007 - 08:44 PM

Oops, I didn't read how your setup works, I wrote the wrong SQL query, lol.

$query = mysql_query("SELECT `clicks` FROM `clicks` WHERE `url` = '$url'") or die(mysql_error()); // Grabs the clicks count from the database where the url is equal to the one clicked (infering you have $url set to the url that was clicked)

$clicks = mysql_fetch_row($query); // Grabs that result and puts it into an easy to use array

echo "This link has been clicked <strong>{$clicks[0]}</strong> times."; // Displays text with the clicks in bolded lettering

This is basically hardly any PHP at all, mostly basic SQL syntax. Simply search for basic MySQL integration in the PHP tutorials here. Its an easy and self-explanatory script to be honest.

Edited by Demonslay, 25 January 2007 - 08:44 PM.


#5 markc1822

    Young Padawan

  • Members
  • Pip
  • 33 posts
  • Gender:Male
  • Location:New York

Posted 29 January 2007 - 02:15 PM

This is what i have so far.

<?php
$db_host = "db_host";
$db_username = "username";
$db_password = "password";
$db_name = "dbname";
mysql_connect($db_host,$db_username,$db_password) or die(mysql_error());
mysql_select_db($db_name) or die(mysql_error()); 

$query = mysql_query("SELECT `clicks` FROM `clicks` WHERE `url` = '$url'") or die(mysql_error()); // Grabs the clicks count from the database where the url is equal to the one clicked (infering you have $url set to the url that was clicked)

$clicks = mysql_fetch_row($query); // Grabs that result and puts it into an easy to use array

echo "This link has been clicked <strong>{$clicks[0]}</strong> times."; // Displays text with the clicks in bolded lettering
?>

But on the page all its shows is This link has been clicked times.

What am I doing wrong? I am not sure what specifically I have to do to show the number of clicks for each page.

#6 markc1822

    Young Padawan

  • Members
  • Pip
  • 33 posts
  • Gender:Male
  • Location:New York

Posted 31 January 2007 - 09:35 PM

i have been looking around and i found the script below. Is that what you ment by defining $URL

<?php
if (defined('CONSTANT')) {
   echo CONSTANT;
}
?> 






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users