Jump to content

Using variables that are appended to urls...


kyleabaker

Recommended Posts

Sorry if the title is confusing but I didn't know how else to word it. I am trying to find a way to basically keep count of links on my website that are clicked so users can see the most used links. I have all of the links in a database and the attributes for each entry include link number, number of clicks, link url, link name, and a type so I can organize them when the page is generated. I thought there might be a way to append something on each link on the pages so they are like...

http://www.mydomain.com/linkredirect.php?link=x

...where the linkredirect.php page will load with minimal elements and use the link number in the database ("x" in the url) to navigate to that link and using the link number it can at the same time increment the link clicked value in the database. The only wall I've run into is how can I make the page generate and write and redirect tag into the header of the file that will let me use the link=x value to find the link in the database, increment the click count and then write the redirect tag to the document plugging in the url that it fetches? Any small examples would be very helpful.

Thanks in advanced Gurus, haha.
Is it possible for me to increment the link clicks using this method? If so I might be able to bypass using a linkredirect.php page all together which would be much better! I'm just not positive if this javascript function will do what I need it to do. It is called within the onClick event of the url (onClick="linkClick(x)").

< - script type="text/JavaScript" language="JavaScript">
function linkClick(n) {
<-?php
$query ="SELECT number,visits";
$query.=" FROM links ORDER BY number ASC";
$result=mysql_query($query);
while (list($number,$visits) =
mysql_fetch_row($result)) {
if ($number==n) {
$result=mysql_query('UPDATE links SET $visits=$visits+1');
}
}
?>
}
< / - script>
php (serverside) cannot be run within javascript (clientside) without having javascript make another request to the server.

Of course you could use some simple [url=http://marc.theaimsgroup.com/?l=php-general&m=112198633625636&w=2]ajax[/url] trickery to have javascript make this request almost silently.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.