vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   MySQL query to get total Thread Views? (https://vborg.vbsupport.ru/showthread.php?t=168781)

xlguy 01-26-2008 04:17 PM

MySQL query to get total Thread Views?
 
Hi guys, a quick question for those who know MySQL. I want to get the total number of thread views for an entire forum, within a specific number of days.

So, if I had a forum called 'Computer Chat' and I wanted to know how many times *all* the threads in that forum have been viewed in the last X days.

You already have total threads and total posts for each forum, I'm looking at a way of querying the database to get total thread views.

Hope that makes sense and just looking for a MySQL query I can run from shell/phpmyadmin.

Thanks in advance to anyone that can shed some light :)

cheesegrits 01-27-2008 02:44 AM

Well, it depends whether you have the 'threadviewslive' option set or not. So to account for either case, you'd need something like:

Code:

$totviews = 0;
if ($vbulletin->options['threadviewslive'])
{
        $query = "
                SELECT SUM(views) AS totviews
                FROM " . TABLE_PREFIX . "thread
                WHERE forumid = $forumid
        ";
}
else
{
        $query = "
                SELECT COUNT(*) AS totviews
                FROM " . TABLE_PREFIX . "threadviews AS threadviews
                INNER JOIN " . TABLE_PREFIX . "thread AS thread ON (thread.threadid = threadviews.threadid)
                WHERE thread.forumid = $forumid
        ";         
}
if ($result = $vbulletin->query_first_slave($query))
{
        $totviews = $result['totviews'];
}

This assumes that the forumid you want is in $forumid.

Untested. If the inner join doesn't work try a right join, I always get confused between those two. :erm:

-- hugh

Marco van Herwaarden 01-27-2008 07:22 AM

View information is not permanently stored by date-time, so without logging historic data into a new table, this is not possible.

cheesegrits 01-27-2008 08:17 PM

Ooops, I spaced on the "within a specific number of days" part. As Marco says, the hit data is not timestamped.

As Marco also says, you'd have to log the historic data. A cron job running once a day (or whatever) that pretty much executes the query I gave you, then updates a new table (say 'tothits') with forumid, timestamp and the delta between the current hit value and the previously recorded ones. I think this could be done with a single query, by using an inner SELECT to sum the previously stored deltas for that forum, and subtract it from the current value.

Then your display query would do something like "SELECT SUM(hits) FROM tothits WHERE timestamp > $cutoff", where $cutoff is set to "time() - (60 * 60 * 24 * $numdays)", where $numdays is how far back you want to go.

-- hugh

xlguy 01-28-2008 09:35 PM

Hi Hugh & Marco,

Thanks for the replies.

When viewing the forum listings from the index/home page it's possible to see the total number of threads and total number of posts. I assume this is done by adding up the stats for each thread in a specific forum? In that case, surely it's also possible to total the number of views for each thread in to a grand total?

cheesegrits 01-29-2008 01:27 AM

Yes, and the code I posted should do that. What you can't do is what you specified in your original post, which is "within a specific number of days". The hit data is not timestamped, so there is no way of selecting a date range.

BTW, the code I posted could be modified to do all forums in one query. For live views:

SELECT SUM(views) AS totviews, forumid FROM vb2_thread GROUP BY forumid

... should work. I don't have the other option enabled anywhere, or I'd test it and give you that one.

-- hugh


All times are GMT. The time now is 01:47 PM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.02059 seconds
  • Memory Usage 1,721KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)bbcode_code_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (6)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete