Forum Moderators: coopster

Message Too Old, No Replies

Using PHP codes for tracking the links

Is the coding logic correct?

         

Amaryllis

9:58 am on Jun 19, 2006 (gmt 0)

10+ Year Member



Hi all,
I need some help in the customized PHP server side coding. Can anyone tell me if my coding logic is correct?
I have a deals page which has got 10 links in it.
I want to write codes for a system that tracks these links.
Well my logic is that for every link I create a corresponding table in a database file that has fields: link_id, link_name, date, clicks. So every time the user clicks a particular link the link id and the system date is passed to the database from where the particular record is selected and the clicks field gets incremented. The table record needs to be populated every day. By retrieving the data from the table I could get to know how many user clicked a particular link over a period of time.
Is my logic correct? Is it ok to have 10 different tables for each particular link? Is there any other smarter logic to code for the above tracking system?
Thanks in advance for your response.

barns101

12:19 pm on Jun 19, 2006 (gmt 0)

10+ Year Member



Well my logic is that for every link I create a corresponding table in a database...

You should only need one table. If you only need the number of clicks, the table would contain the fields `linkID`, `link_name1 and `clicks`. Then some PHP code like this would update the table:


<?php

// Connect to database here

switch ($_GET[linkID])
{
case "1":
mysql_query("UPDATE `tablename` SET `clicks` = `clicks`+1 WHERE `linkID` = $_GET[linkID]");
header("Location: http://www.example.com");
break;

case "2":
mysql_query("UPDATE `tablename` SET `clicks` = `clicks`+1 WHERE `linkID` = $_GET[linkID]");
header("Location: http://www.example.com");
break;
?>

I'm sure that the code can be improved but it should give you an idea of what to so.

Amaryllis

12:10 am on Jun 20, 2006 (gmt 0)

10+ Year Member



Thanks Barns,
I would try out the above coding logic.
Regards,
Amaryllis.

eelixduppy

6:29 pm on Jun 20, 2006 (gmt 0)



Just to make scripting a little shorter, you can use the following:

$valid_ids = array(1,2,3,4,5,6,7,8,9);
if([url=http://us3.php.net/manual/en/function.in-array.php]in_array[/url]($_GET["linkID"],$valid_ids))
{
mysql_query("UPDATE tablename SET clicks = clicks+1 WHERE linkID = ".$_GET["linkID"]);
}

Good luck!

barns101

10:47 pm on Jun 20, 2006 (gmt 0)

10+ Year Member



But what about redirecting the user to the new URL? :)

eelixduppy

10:57 pm on Jun 20, 2006 (gmt 0)



I'm sorry, I forgot about the link. oops. Well there are a couple ways to do this, you can have each link hardcoded that correspond to the link Id's, or you can specify it in the url, and I'm sure there are more ways. Ill show you both:

hard coded:
/////////////////////
$valid_ids = array(1,2,3,4,5,6,7,8,9);
$links = array("link1.html","link2.html","link3.html","link4.html","link5.html,"link6.html","link7.html","link8.html","link9.html");
if(in_array($_GET["linkID"],$valid_ids))
{
mysql_query("UPDATE tablename SET clicks = clicks+1 WHERE linkID = ".$_GET["linkID"]);
header("Location: ".$links[$_GET["linkID"]-1]);
}
/////////////////////

or

through url: example http://www.example.com?linkID=2&url=http://google.com
////////////////////
$valid_ids = array(1,2,3,4,5,6,7,8,9);
if(in_array($_GET["linkID"],$valid_ids))
{
mysql_query("UPDATE tablename SET clicks = clicks+1 WHERE linkID = ".$_GET["linkID"]);
header("Location: ".$_GET["url"]);
}
////////////////////

Good luck ;)

[edited by: jatar_k at 11:32 pm (utc) on June 20, 2006]
[edit reason] examplified [/edit]