View Full Version : help writing simple plugin
aebstract
02-01-2012, 11:53 AM
Essentially I just want to do this:
$result = mysql_query("SELECT COUNT userid FROM userban WHERE adminid = '1'") or die(mysql_error());
while($r=mysql_fetch_array($result)) {
$var = "$r['userid']";
}
and be able to use the variable $var in my template where needed. I'm lost at how I'm suppose to do it since I don't understand hooks that much and am unsure how to code geared for a plugin.
What you'd want to do is register the variable to your template. If the template is being rendered somewhere else, then you want to use "preRegister", like this:
vB_Template::preRegister('templatename', array('var' => $var));
then in the template you'd put {vb:raw var} wher eyou want it to appear.
There's another issue with what you posted because you might have more than one value returned from your query.
As for which hook location to use, that depends on which template you want to display the data in and where it's rendered.
aebstract
02-01-2012, 02:02 PM
Okay I have this code:
$result = mysql_query("SELECT COUNT(userid) FROM userban WHERE adminid = '1'") or die(mysql_error());
while($r=mysql_fetch_array($result)) {
vB_Template::preRegister('header', array('bancount' => $r['userid']));
}
I place it in my template like:
{vb:raw bancount}
Product: vbulletin
Hook Location: global_start
It's not displaying a number or any error on the page at all. I was trying global_start as per your suggestion but is there some sort of method I need to be using to try and find the correct hook location or is this a problem with how the code is written?
I'm thinking maybe you need to use $r['COUNT(userid)'] to access the results.
As for the hook, when you get it working you might want to find a hook location where your code will only be executed when you're going to display it (global start will be executed on every page). To do that you really need to look at the code, but some people seem to do OK by looking at the list of hook names and guessing or using trial and error. Of course you can ask here as well.
aebstract
02-01-2012, 02:23 PM
Awesome, adding count in the variable fixed it. I set my hook to parse_templates because an avatar plugin I am using had the same hook and it seems to work. Thanks for your help.
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.