Help - Search - Members - Calendar
Full Version: how to make a "today's most read story"-script?
Movable Type Community Forum > Additional Resources > Tips and Tricks
nikolaus
I use multiple MT-blogs to create a weblog magazine (http://magazine.zomp.nl). I use php to display the most recent post from each seperate blog on the main page.

Is it possible to make a "today's most read story"-script with php & MySQL?
Dead God
QUOTE
Is it possible to make a "today's most read story"-script with php & MySQL?


Is it possible? Absolutely. Is it easy? I'd suppose that would depend on the implementation.

As far as I am aware MovableType doesn't track how often a particular entry is read because it builds static pages whereas most CMS systems generate their pages on the fly. I'd imagine there's probably a dozen different ways you could code something like this; some involving adding a field to the MT database and hacking some of the templates and some that don't require such measures, but I don't know of anyone having already done such a thing.
bmk
I've been meaning to write something just like this, so I whipped something up this morning. smile.gif I think it's going to need some sort of hammer protection, because as it is now you can just sit there and hit refresh to make the counter go up. But other than that it does the trick quite nicely, and you can also use it on non-MT pages also. You can put your IP in to make it ignore your own visits.

So, add a table in your mysql database:
CODE
CREATE TABLE pageviews (
 pagename tinytext NOT NULL,
 viewcount int(11) NOT NULL default '0'
) TYPE=MyISAM;


And then create a new index template with the script:
CODE
<?
include '/full/path/to/connect/file.php';

$pagename = $REQUEST_URI;
$viewcount = 0;

$query = mysql_query("SELECT * FROM pageviews WHERE (pagename = '$pagename') LIMIT 1");
while($row = mysql_fetch_array($query)) {
$viewcount = $row['viewcount'];
}

if ( $REMOTE_ADDR != '64.xxx.xxx.xx') {
    $viewcount = $viewcount+1;
    if(mysql_num_rows($query) == 0) {
    $insert = mysql_query("INSERT INTO pageviews (pagename, viewcount) VALUES('$pagename', '$viewcount')");
    } else {
    $update = mysql_query("UPDATE pageviews SET pagename = '$pagename', viewcount = '$viewcount' WHERE pagename = '$pagename'");
    }
} // end IP

echo 'viewed: '.$viewcount;
?>


So then you'd include on all the pages you want to count, and then you can do just a regular query to make a list of the most popular.
bmk
(I put it on the bottom of my individual archives at emptypages so you can check it out there.)
kadyellebee
On the same idea... smile.gif I've been meaning to write something up too - I'm using the Refer script and integrating it into the http://mt-plugins.org pages - and you can see a link for the Popular Plugins on the sidebar smile.gif

Basically the same idea as Brenna's, just uses the Refer databases that I already had in place.

Kristine
nikolaus
Thanks a lot for all your advice & help! I'm surely gonna try this out! Wow, I had expected that it wouldn't be possible...

Nikolaus
bmk
Hey yeah Kristine! Somewhere, someplace in the last few weeks I was asking you about the mods you did to refer. smile.gif I knew I was thinking about this for a reason! Too, I really love the "popular archives" at mt-plugins (and seeing how many mine have, hee hee ha)
nikolaus
I added the "pageviews"-table to the database, and installed the count-script, but I keep getting an error on line 10. Any idea what I did wrong? I inserted my own ip-adress (10.0.0.150) and db.php is my connect script.

Or is it because there's no data in the database right now (I mean, I excluded my own ip from the counting)?

CODE
<?
include 'db.php';
$pagename = $REQUEST_URI;
$viewcount = 0;
$query = mysql_query("SELECT * FROM pageviews WHERE (pagename = '$pagename') LIMIT 1");
while($row = mysql_fetch_array($query)) {
$viewcount = $row['viewcount'];
}
if ( $REMOTE_ADDR != '10.0.0.150') {
    $viewcount = $viewcount+1;
    if(mysql_num_rows($query) == 0) {
    $insert = mysql_query("INSERT INTO pageviews (pagename, viewcount) VALUES('$pagename', '$viewcount')");
    } else {
    $update = mysql_query("UPDATE pageviews SET pagename = '$pagename', viewcount = '$viewcount' WHERE pagename = '$pagename'");
    }
} // end IP
echo 'viewed: '.$viewcount;
?>
russellwaiheke
QUOTE (bmk @ May 15 2003, 04:14 PM)
$pagename = $REQUEST_URI;
$viewcount = 0;

Fantastic script and I'm about to use it.
One query though if I can?
How do I remove or trim the URL returned, as $pagename, eg
/folder1/folder2/folder3/page.php
to give me
/folder3
*grin*
I've been looking and looking.
Any help would be great! smile.gif
Carina W-J
I fixed it!
girlie
Do you have your connect file set up and the path correct at the top of the script?
Carina W-J
Thanks Girlie!

I found out my own error, I missed localhost. Another question, how do I do an include the correct way so that the script actually counts?
girlie
Do you mean how do you include the code on all the pages you want to count?
Carina W-J
Yes thats what I mean. I also know nothing about php. The pages are all ending in .php so that is at least correct wink.gif But how to implement the script on the pages...
girlie
CODE
<?
include ("/full/path/to/page/views/script.php");
?>
Carina W-J
Thanks now I know how to do that later on.

The only problem now is that I receive this error and I am a big questionmark.
CODE
Warning: SAFE MODE Restriction in effect. The script whose uid is 65534 is not allowed to access /home/privateperson/gokboet/public_html/Album/counter.php owned by uid 1246 in /home/privateperson/gokboet/public_html/Album/arkiv/000845.php on line 194

Warning: Failed opening '/home/privateperson/gokboet/public_html/Album/counter.php' for inclusion (include_path='.:/usr/local/lib/php/') in /home/privateperson/gokboet/public_html/Album/arkiv/000845.php on line 194


I have made the counte.php writeable and also owned by me and not the server so I do not really understand the error message.
girlie
Hmmm. Try this.

Create a template module in your Album blog, and where it says "link this template to a file", type counter.php. Then hit SAVE.

If your script code shows up in the Module Body window, hit SAVE again.

Then try to access the page and see if you get the same error.
Carina W-J
No that did not solve the problem. I kept it as a module so then the MT wont make the file serverowned.

The page is here.

I better get some sleep, it is late here in Stockholm Sweden.

Thanks for the attempts if you come up with an idea or another that I should try, please tell me.
Carina W-J
Now I know why this happens, at least something to go on with.

The files that are created through MT get owned by the server so therefore the conflict with the ownership. How can I get ariund that problem?

I downloaded the index.php to test if this was a possible solution. I then deleted the index.php on on the server, reuploaded the downloaded one and then the script works.

Perhaps there is something in MT that I can configure so that the ownership doesn´t automatically become the servers.
girlie
Hmmm. So is the script owned by you, but the archive files are owned by the server?

This is why I suggested creating the script as a template in MT - hoping that it would give the same ownership to it as it does the other files.

You could try the same approach another way by creating the script as an Index Template, giving it a new name, and rebuilding it through MT.
Carina W-J
You think, I didn´t but now.´

Your suggestion is the easiest one of course, why didn´t I think of it myself when I sorted out the reason for the script not working.

I will try creating the script through MT and will report back.
Carina W-J
It worked! All it takes sometimes is a different angel aproaching the problem.

Thank You very much!

Regards Carina
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.