Jump to content

Most Popular Links On Website?


pmhabo
 Share

Recommended Posts

I want to code something like this on my website, but have no idea how to do it. It shows the most popular links on your site, with the most popular link having the largest font and is listed first, while the least poplular link is listed last and is the smallest font.Can someone tell me how?Thanks!

Edited by pmhabo
Link to comment
Share on other sites

First you need to keep track of which links they click on. One way to do that is to use a link counter, but I don't think the Ohio site is doing that. More likely they're probably analyzing the log files to count up the most popular URLs that people type in, get from search engines, etc. A link counter would only count the links that they click on your site, not anything they type themselves, bookmark, get from a search engine, or a link from another site. In order to catch all of those they need to analyze the log files for the web server.The different font sizes are just some basic math, figuring out the max and min hit count for the links, then determining what the max and min font sizes will be, and using percentages for the rest.

Link to comment
Share on other sites

yeah, they're probably just analyzing their log files to see which pages are clicked on the most, and then just applying a font-size style to the more popular links, and lower and lower.Automating this process would probably take a combination of some PHP and Javascript.

Link to comment
Share on other sites

I'm not sure if you'll be able to use PHP to open the server's log files, PHP might not be allowed to do that on your server. You'll need to contact your host and tell them you want to have a script analyze the log files and ask them if that's possible. They might say they already have stats packages installed, but those aren't what you're looking for. If it is possible, you'll need to find out where the log files are kept on the server. From there you can set up the program to analyze them and output a file listing all of the URLs and their popularity, and then the PHP page can read that file and get the list to loop through.

Link to comment
Share on other sites

Or (this may seem even more complex), could could set up a table on a database that has a counter for each page and have AJAX make a call on each link click to add one to the database depending on the page.Example, if you have 3 pages, pageone.php, pagetwo.php, and pagethree.php, you could have the database look like:

|id|page		 |clicks||1 |pageone.php  |0	 ||2 |pagetwo.php  |0	 ||3 |pagethree.php|0	 |

Have your AJAX function pass the page name, something like this (assume ajaxObj is a previous created HTTP XML object with another function):function pageCounter(pagename){ var url = "pagecounter.php?page=" + pagename; ajaxObj.open("GET", url, true); ajaxObj.send(null);}Then have pagecounter.php look like:<?php//connect information$page = mysql_real_escape_string($_GET['page']);$oldcount = mysql_fetch_array(mysql_query("SELECT * FROM pagecounter WHERE page = '$page'"));$newcount = $oldcount['clicks'] + 1;mysql_query("UPDATE pagecounter SET clicks = '$newcount' WHERE page = '$page'");?>And finally design your links like:<a href="pageone.php" onclick="pageCounter('pageone.php');">Page One</a>There could be losts of spelling and syntax mistakes, but something like that, if you didn't have access to logs.Or you could make your own logs using a similar system, except instead of databsing use file streams.

Link to comment
Share on other sites

Or (this may seem even more complex), could could set up a table on a database that has a counter for each page and have AJAX make a call on each link click to add one to the database depending on the page.Example, if you have 3 pages, pageone.php, pagetwo.php, and pagethree.php, you could have the database look like:
|id|page		 |clicks||1 |pageone.php  |0	 ||2 |pagetwo.php  |0	 ||3 |pagethree.php|0	 |

Have your AJAX function pass the page name, something like this (assume ajaxObj is a previous created HTTP XML object with another function):function pageCounter(pagename){ var url = "pagecounter.php?page=" + pagename; ajaxObj.open("GET", url, true); ajaxObj.send(null);}Then have pagecounter.php look like:<?php//connect information$page = mysql_real_escape_string($_GET['page']);$oldcount = mysql_fetch_array(mysql_query("SELECT * FROM pagecounter WHERE page = '$page'"));$newcount = $oldcount['clicks'] + 1;mysql_query("UPDATE pagecounter SET clicks = '$newcount' WHERE page = '$page'");?>And finally design your links like:<a href="pageone.php" onclick="pageCounter('pageone.php');">Page One</a>There could be losts of spelling and syntax mistakes, but something like that, if you didn't have access to logs.Or you could make your own logs using a similar system, except instead of databsing use file streams.

Hey ragae, you give me headache with all these code. I wonder where you learn it.
Link to comment
Share on other sites

I have a javascript code that list 5 random links out of a list links. All I have to do is plug that into my sites HTML and i'm done.It seems far less complex than what you guys are suggesting. I guess i'll stick with using that even tho it isn't what I really want to do.

Link to comment
Share on other sites

I'm not sure if you'll be able to use PHP to open the server's log files, PHP might not be allowed to do that on your server. You'll need to contact your host and tell them you want to have a script analyze the log files and ask them if that's possible. They might say they already have stats packages installed, but those aren't what you're looking for. If it is possible, you'll need to find out where the log files are kept on the server. From there you can set up the program to analyze them and output a file listing all of the URLs and their popularity, and then the PHP page can read that file and get the list to loop through.
yeah I figured for simplicities sake one could use a counter method for obtaining page views, which it appears has been described very nicely in above posts.
Link to comment
Share on other sites

Yeah, you could use a counter also. Looking at the logs may be ideal, but you'd have to process them on a regular basis and you'd also have to filter out URLs you don't want, like images, stylesheets, the actual link page, etc. You wouldn't necessarily have to use ajax though, with ajax you could count the number of times people actually click on links, or you could just include a PHP counter on each page and it would record the number of times each page is actually loaded, regardless how they got there. That would be a hit counter for each page. That could be something like this:

if (mysql_num_rows(mysql_query("SELECT * FROM hits WHERE url='{$url}'")) == 0)  mysql_query("INSERT INTO hits (url) VALUES ('{$url}')");else  mysql_query("UPDATE hits SET counter=counter + 1 WHERE url='{$url}'");

Link to comment
Share on other sites

What justsomeguy has suggested works good as well. If it doesn't matter how the user gets to the page (i.e. through search engines or other sites) then a MySQL code on each page that updates the database by 1 each time the page loads would be fine. But note that this also gets updated if the user refreshes the page as well.If you want one that counts link clicks or doesn't count refreshes, you'll have to get creative.And I learned all my coding through one of two places, w3schools tutorials, or here in these forums. justsomeguy has especially aided me in my learning. He's very smart and knows his way around the languages. I just take his knowledge and tweak it to my needs.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...