WordPress.org

Ready to get started?Download WordPress

Forums

REQ: Post read X times counter (29 posts)

  1. Laughinglizard
    Member
    Posted 10 years ago #

    I wrote a script which determines the popularity of a post (and outputs the most popular posts) if thats what you are looking for.
    Here is the link to the forum discussion:
    http://dinki.mine.nu/word/index.php?p=108&more=1&c=1

  2. _mf_
    Member
    Posted 10 years ago #

    Not exactly. Here's an example:
    http://www.lynnrockwell.com/weblog.php/
    Look at the end of each post, just before the "link me" permalink, it says "XX views".
    I guess this is my lazyweb request of the day...

  3. Laughinglizard
    Member
    Posted 10 years ago #

    That is a great idea! Ill see what I can come up with! :-)

  4. Laughinglizard
    Member
    Posted 10 years ago #

    I have a solution. You can see how it looks at http://dinki.mine.nu/weblog/ and you can download the hack instructions at: http://dinki.mine.nu/word/index.php?p=108&more=1&c=1

  5. _mf_
    Member
    Posted 10 years ago #

    Exactly what I wanted :) Cool... Why don't you display it on your home page (I only see it in the comments/full post pages) ?

  6. Laughinglizard
    Member
    Posted 10 years ago #

    Its on the home page. However, nothing is displayed when a post has not received any hits yet. :-)
    Glad you like it.
    Peace

  7. LoganSix
    Member
    Posted 10 years ago #

    Running into an error:

    Warning: mysql_affected_rows(): A link to the server could not be established in /home/deanlog/public_html/blog/wp-includes/wp-db.php on line 173

    $this->rows_affected = mysql_affected_rows();
    I think it has something to do with the $wpdb call.
    Is there a way to close the $wpdb connection?
    I use it in another hack, so I could use this script the way it was created.
    My revised script:

    function add_count($p_number) {
    global $wpdb;
    $result = $wpdb->get_results("SELECT post_ID, postView_cnt FROM b2postView WHERE post_ID = $p_number");
    $test = 0;
    if($result){
    foreach ($result as $result){
    $newCount = $result->postView_cnt + 1;
    $postquery = ("update b2postView SET postView_cnt = $newCount WHERE post_ID = $result->post_ID");
    $results = $wpdb->query($postquery);
    $this->results = false;
    $test = 1;
    }
    }
    else{
    $postquery = ("insert INTO b2postView VALUES($p_number, 1)");
    $results = $wpdb->query($postquery);
    $this->results = false;
    }
    }

  8. Chris J. Davis
    Member
    Posted 10 years ago #

    Why are you calling $wpdb in the add_count function? You don't need to.

  9. davidchait
    Member
    Posted 10 years ago #

    a few quick comments:
    - great idea! ;)
    - it'd help if everyone would write hacks to use $wpdb at all times. I have to modify all hacks otherwise, as I have a lot of debug/performance tracking systems embedded in wpdb... It also means there's only one open of the database occurring per page.
    -the addCount thing could use to do an atomic operation. Otherwise it's possible two accesses will cancel each other out. I'm looking into this for other reasons, will report back when I figure it out.
    - my other mod to this will/would be to have it store the data as an extra column in the wp_posts table, rather than creating a new one. The advantage being that the new data will come implicitly in the post loop without a second query or a join or anything. ;) yeah, it means modifying the base structure of the table -- but since I've already done that once... ;) :)
    Some great work here -- I'm looking forward to figuring out how to pull some data from my referral tracking db to 'initialize' my counts to something reasonable, then start it all up. Keep up the hacks guys!
    -d
    http://www.chait.net

  10. Laughinglizard
    Member
    Posted 10 years ago #

    @david: I would like to clarify my reasons for not using $wpdb everywhere.
    1) Using mysql_query does not mean a new database connection. This hack, for example, uses the existing database pointer and does not open a new one
    2) $wpdb performs a bunch of extra data manipulation tasks that are not always required
    3) $wpdb does not have a lot of the built in MySql/php features implemented such as count_tables etc (thus a lot of the vanilla WordPress code still contains native MySql calls, as of 1.0 )
    However, I do agree that we should try to code within the confines of wpdb as far as possible to make the hacks follow the WP coding style guidelines.
    http://wordpress.org/docs/developer/#coding-style

  11. davidchait
    Member
    Posted 10 years ago #

    I'm with you -- and of course agree all your reasons are extremely legit. some quick notes back at ya (since we help the populace learn by tossing out examples):
    for (1): I've seen cases where people were re-opening connections to the db -- I noted that just for clarity, not that you were! ;)
    for (2): there's likely more overhead to the SQL lookup itself. also, as I mentioned I've got db debug stuff, which basically tracks every query call coming through wpdb including a string name of caller, good for tracking too-many-queries or not-enough-caching. :)
    and for (3): all the more reason to enhance wpdb to expose needed functionality! ;) In addition to coding guidelines (some of which I don't agree with, especially some of the formatting! ;) ), it also forwards the ability to remove dependence on MySQL in the future... :) Almost worth starting a new topic on enhancing wpdb for that reason alone...
    I just basically cloned your idea but embedded into the posts table. One update query does the +1, the count is already in $post when needed, and popular posts queries one db. But again, that's great for someone like me who's already hacked up the tables -- for 99% of users out there, your approach wins hands down, no question... well, and I haven't tested mine yet.
    Bitch that I didn't have this in place two months ago -- reconstructing counts from referrer log wasn't overly useful, as it's easily missing dozens or even hundreds of views... but gotta start somewhere!
    By the way Mark, want to reinforce again cool idea and sample! Keeps me thinking about methods for enhancing my site, and continues to push me to learn more SQL tricks. ;) Keep up the good work!
    -d
    http://www.chait.net

  12. LoganSix
    Member
    Posted 10 years ago #

    I had to switch it because I was getting more errors the other way. This hack was breaking my wp-people hack.
    Now I only get the one error, I just want it to go away. Please let me know what you figure out.
    It only shows up when you click on the post to view it.

  13. Laughinglizard
    Member
    Posted 10 years ago #

    @david: Dude, you are awesome! I really was not criticising you, but referring to some of the weaknesses of $wpdb I love your site, BTW Read it when I need some good tech-browsing material. :-)
    @Logan: For my hack, try to modify it this way:
    Replace every occurance of $row, $result and $results, with $row123, $result123 and $results123 respectively. Let us know what transpires.

  14. LoganSix
    Member
    Posted 10 years ago #

    Reverted add_count back to what you had.
    Added suggested changes.
    Still no luck.
    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/deanlog/public_html/blog/my-hacks.php on line 14

  15. Laughinglizard
    Member
    Posted 10 years ago #

    Quick and (really) dirty fix: Find the mysql_fetch_array() function in question and add an @ in front of it, like so:
    @mysql_fetch_array()

  16. Chris J. Davis
    Member
    Posted 10 years ago #

    I have run into a potential... shall we say snag in this hack. I am not sure but I think that I am getting infalted results from search engine spiders.
    Some of my posts are at around 150+ views in 1 - 2 days, while some are still at 0 or 1 -3. I actually get a decent amount of traffic, but I wouldn't think I am getting that much on just one or two posts.
    I am going to look at hacking in some spider-handling to this, and I will let you guys know what happens.
    Chris

  17. LoganSix
    Member
    Posted 10 years ago #

    I haven't made any changes to get this working properly, but I want to point out that you should also put a IP tracker in it, so that refreshing the page doesn't add to the count.
    Just a thought.

  18. davidchait
    Member
    Posted 10 years ago #

    This is a pageview counter. If you really want a unique-views counter, you'll want to take something like the refererLib stuff, extend it to allow null referers, and verify that a given IP with a blank referral and a given post page aren't in the last say 24h of entries.
    I've noticed my post count goes up weirdly while I'm editing draft/private posts. Now, I am using my own 'variation' on this theme, but it uses the same basic thought process. It also has a bail-out check for admin users (which should mean no accessing I do should ever cause a read-hit...). I think there's something wacky, maybe my field is somehow set to autoincr or something like that.. hmnm....
    -d
    http://www.chait.net

  19. LoganSix
    Member
    Posted 10 years ago #

    Call me nit picky or something, but I think using the referrerLib is probably better for getting a more accurate count. What's the point of putting a count out, if you can't make it as accurate as possible.
    Now, I can see were having the count shoot up like a Saturn V rocket would help a person's ego, but I prefer to get a more realistic view of who's checking out my posts.
    Thanks for the work David, I look forward to the updated version to try out.

  20. Prim3r
    Member
    Posted 8 years ago #

    i really dont understand what im doing wrong... :(

    i followed every step , the obly changes i made was changing the table name to wp_mostAccessed , and changed every time i saw mostAccessed to wp_table mostAccessed...

    But nothing is showing...not even errors! . . . the code to show how many time the "read more" is suposto to be :

    <?php show_post_count($post->ID, $before="(Visited ", $after=" times)"); ?>

    right?

    i put it , and its supost to appear after the :

    "by Prim3r"
    the website is here :

    http://www.prim3r.nextdesigns.net/

    btw , i chech the table on mysqladmin and its empty....

  21. Mark (podz)
    Support Maven
    Posted 8 years ago #

    Do you have this line:
    <?php if ($single) { add_count($post->ID); } ?>
    right underneath this line:
    `<?php if (have_posts()) : while (have_posts()) : the_post(); ?>'
    in your main template ?

  22. Prim3r
    Member
    Posted 8 years ago #

    yes like this :

    <div id="content" class="narrowcolumn">
    <?php if (have_posts()) : ?>

    <?php while (have_posts()) : the_post(); ?>
    <?php if ($single) { add_count($post->ID); } ?>

    .....

    and where is supost to show is like this :

    <?php comments_popup_link('Leave a Comment ', '1 Comment ', '% Comments '); ?><br><?php show_post_count($post->ID, $before="(Visited ", $after=" times)"); ?></div><div class="clear"></div>

    maybe the error is on database...?

  23. Mark (podz)
    Support Maven
    Posted 8 years ago #

    I have this:
    <?php if (function_exists('show_post_count')) show_post_count($post->ID, $before="Read (", $after=")"); ?>
    so it could be... the table is still empty in the database ?

  24. Prim3r
    Member
    Posted 8 years ago #

    right now im getting conection is refused...some isp problem i guess :(

    that code is diferent...maybe i should copy it and past over my code...?

    if not...mabye creating a temp admin acount if anyone want to help.

  25. Prim3r
    Member
    Posted 8 years ago #

    onme question a little of topic :

    all my tables begin with : wp_

    so this one should have the same before the name right?...

  26. Mark (podz)
    Support Maven
    Posted 8 years ago #

    no - mine is just called 'mostAccessed' too..

  27. Mark (podz)
    Support Maven
    Posted 8 years ago #

  28. Prim3r
    Member
    Posted 8 years ago #

  29. Prim3r
    Member
    Posted 8 years ago #

    ok . . . its no use!

    I activated the plug in i put the code :

    <?php if($id > 0) { add_count($id);} ?>

    after the : <?php while (have_posts()) : the_post(); ?>

    and to appear :

    <?php comments_popup_link('Leave a Comment ', '1 Comment ', '% Comments '); ?><br><?php show_post_count($post->ID, $before="(Visited ", $after=" times)"); ?>

    the result is...nothing! nothing appear on website :(

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.