vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   Creating a simple hit counter? (https://vborg.vbsupport.ru/showthread.php?t=202224)

Simon Lloyd 01-18-2009 11:40 AM

Creating a simple hit counter?
 
Hi all i have created a new database called mycounter and then created a plugin like this
Product : VBulletin
Hook Location :Global_Start
Title: Hit Counter
Execution Order : 5
Plugin PHP Code :
PHP Code:

<if condition="$show['member']">
$result mysql_query("SELECT count FROM mycounter"); 
$mycounter mysql_result($result0) + 1
$result mysql_query("UPDATE mycounter SET count = count + 1");
</if> 

i then added this to my desired page:
PHP Code:

<table class="tborder" cellpadding="$stylevar[cellpadding]cellspacing="$stylevar[cellspacing]border="0" width="10%" align="center">
<
td class="tcat"><center>Hit Counter</td>
<
tr align="center"><td font size="+2" color="blue">$mycounter 
</td></tr>
</
table

My problem is that it won't show the count unless i remove the if condition, i'm no coder, but what i am trying to do is just count the number of members who visit the page not guests, can anyone help?

Dismounted 01-19-2009 03:27 AM

You cannot use template conditionals in plugins. You need to use PHP code in plugins.
PHP Code:

if ($show['member'])
{
    
$result $vbulletin->db->query_first("SELECT count FROM mycounter LIMIT 1");
    
$mycounter $result['count'] + 1;
    
$vbulletin->db->query_write("UPDATE mycounter SET count = $mycounter");



Simon Lloyd 01-19-2009 05:12 AM

Thanks for the code but the counter still doesn't show anything when using the code you supplied, it will only show a figure if i use
PHP Code:

$result mysql_query("SELECT count FROM mycounter"); 
$mycounter =  mysql_result($result0) + 1
$result =  mysql_query("UPDATE mycounter SET count = count + 1"); 

The counter is at the bottom of this page http://www.thecodecage.com/forumz/helpvideos.php

Dismounted 01-19-2009 05:23 AM

PHP Code:

if ($vbulletin->userinfo['userid'])
{
    
$result $vbulletin->db->query_first("SELECT count FROM mycounter LIMIT 1");
    
$mycounter $result['count'] + 1;
    
$vbulletin->db->query_write("UPDATE mycounter SET count = $mycounter");



Simon Lloyd 01-19-2009 06:19 AM

Thanks, thats it!, seems to work well!

--------------- Added [DATE]1232394416[/DATE] at [TIME]1232394416[/TIME] ---------------

The counter is working, but it seems too well, it seems that its counting every members hit on every page. is it possible to just lock it down to my custome page?

pein87 01-20-2009 10:52 PM

when you run it in a template use the this but warn you that you must define the script in the top of the custom pages template.

<if condition="THIS_SCRIPT == 'my_custom_page'">
<!-- execute this code -->

<!-- /execute this code -->
</if>

That should work so it only displays it on that page and I`m sure you can pass that definition to your php code so it only respnds to hits on that page and with the conditional above it only displays that info on that page if you add it to lets say the footer no need for a custom template.

I would use

if (THIS_SCRIPT == 'mycustom_page')
{
//start code

//end code
}

I wouldnt use an else unless you really need the option.

Hope it helps you.

Dismounted 01-21-2009 03:15 AM

No need to use any template conditionals. (The HTML is only put in the custom template.) Just change the plugin.
PHP Code:

if (THIS_SCRIPT == 'yourscript' AND $vbulletin->userinfo['userid'])
{
    
$result $vbulletin->db->query_first("SELECT count FROM mycounter LIMIT 1");
    
$mycounter $result['count'] + 1;
    
$vbulletin->db->query_write("UPDATE mycounter SET count = $mycounter");



Simon Lloyd 01-21-2009 07:58 AM

Thanks again, but i couldn't get it to work!, my php script is helpvideos.php and my custom template is helpvids.

Thanks for continuing support for this.

EDIT: As i said i am no coder, i tried helpvideos.php and helpvids in place of your script.

Dismounted 01-21-2009 08:17 AM

At the top of "helpvideos.php" there should be something like:
PHP Code:

define('THIS_SCRIPT''XXXXXXXX'); 

Put that "XXXXXXXX" in place of "yourscript".

Simon Lloyd 01-21-2009 08:31 AM

Dismounted, you definitely know what you're doing! :) your changes to the code worked, i had the script defined as helpvideos, used that and the counter is showing again, so i have reset it to zero and will post back.....thanks very much!!!!

--------------- Added [DATE]1232535496[/DATE] at [TIME]1232535496[/TIME] ---------------

UPDATE: Yep, that seems to work perfect, i logged out and accessed the custom page several times, i was shown the hit counter but no figures, then logged back in, clicked the page and the counter figures were seen and incremented.

1 question, can i use the IF condition you posted in my script around the counter stuff to prevent non members even seeing the counter box?


All times are GMT. The time now is 03:43 AM.

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.01728 seconds
  • Memory Usage 1,762KB
  • 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
  • (7)bbcode_php_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (10)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
  • pagenav_page
  • pagenav_complete
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete