Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 01-26-2008, 04:17 PM
xlguy's Avatar
xlguy xlguy is offline
 
Join Date: Feb 2004
Location: UK
Posts: 168
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default 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
Reply With Quote
  #2  
Old 01-27-2008, 02:44 AM
cheesegrits's Avatar
cheesegrits cheesegrits is offline
 
Join Date: May 2006
Posts: 500
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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
Reply With Quote
  #3  
Old 01-27-2008, 07:22 AM
Marco van Herwaarden Marco van Herwaarden is offline
 
Join Date: Jul 2004
Posts: 25,415
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

View information is not permanently stored by date-time, so without logging historic data into a new table, this is not possible.
Reply With Quote
  #4  
Old 01-27-2008, 08:17 PM
cheesegrits's Avatar
cheesegrits cheesegrits is offline
 
Join Date: May 2006
Posts: 500
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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
Reply With Quote
  #5  
Old 01-28-2008, 09:35 PM
xlguy's Avatar
xlguy xlguy is offline
 
Join Date: Feb 2004
Location: UK
Posts: 168
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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?
Reply With Quote
  #6  
Old 01-29-2008, 01:27 AM
cheesegrits's Avatar
cheesegrits cheesegrits is offline
 
Join Date: May 2006
Posts: 500
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 11:36 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.03491 seconds
  • Memory Usage 2,207KB
  • Queries Executed 13 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)bbcode_code
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (6)post_thanks_box
  • (6)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (6)post_thanks_postbit_info
  • (6)postbit
  • (6)postbit_onlinestatus
  • (6)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.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/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete