On Apr 15, 2008, at 5:26 PM, tedd wrote:
At 10:41 AM -0500 4/15/08, Steve Marquez wrote:
Greetings,
Can any of you point me in the right direction on how to use PHP to
create a
most viewed or most clicked articles list? Can it be done with PHP?
Thank you so much,
Steve Marquez
smarquez@xxxxxxxxxxxxxxx
Steve:
There are a couple of ways to do this: 1) use mysql to record each
download; 2) use a file to record each download.
All you do is pass the user's click to a php script that records the
transaction -- incrementing a counter in either mysql or a file and
delivers the content to the user. You can include that number in
your html to show how many transactions, if you want.
This is an example of using a file:
http://www.webbytedd.com/b/readwrite-download/
Cheers,
tedd
If you want to take it a step further and only record a *click* if a
person hasn't clicked this article before, you have several options.
You can save a cookie on their computer saying they've clicked that
article - of course they can delete the cookie, which may skew the
count. Or you can save their IP address in a database (or file, if you
want to choose that method) - only increment if that IP address hasn't
clicked that article before. And of course, it's also very possible
that their IP address can change. However, it should work pretty well.
To get you started (and for the archives)....
<?php
$article_id = mysql_real_escape_string ($_GET['article_id']);
// Cookie way
if (!$_COOKIE["article-$article_id"]) {
setcookie ("article-$article_id", true, time()+60*60*24*365); //
expire 1 year from now
mysql_query ("UPDATE `clicked_articles` SET `article_count` =
`article_count`+1 WHERE (`article_id` = '$article_id')");
$result = mysql_query ("SELECT `article_count` FROM
`clicked_articles`");
$clicked_count = mysql_result ($result, 0);
}
// IP-Database way
$ip = $_SERVER['REMOTE_ADDR'];
$result = mysql_query ("SELECT `ip_address` FROM `clicked_articles`
WHERE (`ip_address` = '$ip') LIMIT 1");
if (!mysql_num_rows ($result)) {
mysql_query ("INSERT INTO `clicked_articles` (`article_id`,
`ip_address`) VALUES ('$article_id', '$ip')");
$result = mysql_query ("SELECT COUNT(`ip_address`) FROM
`clicked_articles` WHERE (`article_id` = '$article_id')");
$clicked_count = mysql_result ($result, 0);
}
?>
If you want to find the top 5 clicked articles, do the following
respectively compared to above:
<?php
// If using Cookies
$result = mysql_query ("SELECT `article_id` FROM `clicked_articles`
ORDER BY `article_count` DESC LIMIT 0,5");
while (list ($id) = mysql_fetch_row ($result)) {
$article_ids[] = $id;
}
// If using IPs
$result = mysql_query ("SELECT COUNT(`article_id`) AS
`article_id_count`, `article_id` FROM `clicked_articles` GROUP BY
`article_id` ORDER BY `article_id_count` DESC LIMIT 0,5");
while (list ($count, $id) = mysql_fetch_row ($result)) {
$article_ids[] = $id;
// OR to give count for each id
$article_ids[$id] = $count;
}
?>
Hope this helps. Note, the usual email-code disclaimer.....
~Philip
PS... Sorry I'm two days late. Just now catching up. =D
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php