PDA

View Full Version : Creating a simple hit counter?


Simon Lloyd
01-18-2009, 11:40 AM
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 :
<if condition="$show['member']">
$result = mysql_query("SELECT count FROM mycounter");
$mycounter = mysql_result($result, 0) + 1;
$result = mysql_query("UPDATE mycounter SET count = count + 1");
</if> i then added this to my desired page:
<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.
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$result = mysql_query("SELECT count FROM mycounter");
$mycounter = mysql_result($result, 0) + 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
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 1232394416 at 1232394416 ---------------

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.
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:
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 1232535496 at 1232535496 ---------------

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?

Dismounted
01-21-2009, 09:27 AM
Add this around the counter HTML code in your template:
<if condition="$vbulletin->userinfo['userid']">
COUNTER HTML
</if>

Simon Lloyd
01-21-2009, 11:57 AM
Excellent!
Can't thank you enough!