View Full Version : Querying MySQL and echo-ing variables
jgrietveld
08-09-2011, 02:43 PM
Hello!
I'm developing a front page for a vbulletin powered forum. On the front page I mention the main Forum titles and their sub-forums. I do not wish to hardcode the titles so the webmaster can change this when needed.
So I want to perform a MySQL query to retrieve the Forum titles:
$results = $db->query_read("SELECT title FROM forum WHERE forumid = 1");
This is fine I guess, although, this code is printed as is on the site. Adding <?php and ?> doesn't help either. :confused:
Also, I need to echo the contents of the $results variable. But how? What's the way to echo variables in vBulletin?
Thanks for your help!!
Jerry
So I want to perform a MySQL query to retrieve the Forum titles:
$results = $db->query_read("SELECT title FROM forum WHERE forumid = 1");
This is fine I guess, although, this code is printed as is on the site. Adding <?php and ?> doesn't help either. :confused:
When you say you're developing a front page, what are you doing exactly? Are you making a template? If so then yeah, you can't put php code in a template. The vbulletin way to do it is to write code in a plugin (or create your own page by writing a php file), then use templates to produce the output.
jgrietveld
08-09-2011, 02:54 PM
Hi kh99,
I'm developing a theme indeed. I've duplicated the default theme and I am restyling and recoding the header, footer and the cms template. I'm now working in the vbcms_page template.
Here I want to echo the forum titles with some nice styling around it.
If i understand you correct I can just create a regular php file where I start with <?php and close the code with ?> ?
This file then needs to be included as a plugin?
--------------- Added 1312905358 at 1312905358 ---------------
Or is there a direct variable which will output the forum titles?
If there isn't already a variable (and I'm not sure because I'm not familiar with that page), then you need to create a plugin, which is some php code that gets executed at some point in the vb script. That plugin code would have to do your query to set a variable, then register that variable in the vbcms_page template.
When you create a plugin you need to choose a hook location, which corresponds to a certain point in the vb script. Unfortunately I don't know much about that template so I don't know what hook location you should use. Maybe someone else does, or if I get time later I'll look. (Edit: Some people look at the list of hook locations and guess or fid one by trial and error, but if you know some php then I think the best way is to look at the vb php files and find a call to fetch_hook() that's in a place you can use).
BTW, you can also just edit the vb php files to do what you want, but it's much better to use plugins because you can then upgrade without making the same changes to a new set of files.
jgrietveld
08-09-2011, 03:43 PM
Thanks again for your fast response! I'm going to read into hooks and plugins now. If I find out about the hooks used in this particular template I'll post it here. If you find out anything, please let me know as well.
Many thanks!
--------------- Added 1312909240 at 1312909240 ---------------
Hi kh99,
To be honest, I don't quite get the whole idea of hooks. If i go to the theme editor
Styles & Templates > Style Manager > Edit Templates > vBulletin CMS templates > vbcms_page
I see the structure of a normal web page here. Some external parts are included like so:
{vb:raw header}
{vb:raw navbar}
Why can't I just enter code here? I know exactly where i want my forum titles to appear so why do I have to find a Hook and still create a plugin?
I went through the list of hooks for the cms, but I'm not even sure what I'm looking for.
Thanks!
daveaite
08-09-2011, 05:18 PM
The hooks are for inserting PHP code. If you want to insert directly into the tempalte it will have to be HTML, javascript or CSS references external sheets.
Why can't I just enter code here? I know exactly where i want my forum titles to appear so why do I have to find a Hook and still create a plugin?
Well, the answer really is because the template compiler doesn't allow php - in fact it checks for php code to stop you from doing that. I don't know why that decision was made, but that's the way it is. (There was a mod that allowed you to put php code in a template, but I don't know if it works with the latest versions).
One thing you can do is write an external php script to do what you want, then include it using instructions from the vbulletin manual: https://www.vbulletin.com/docs/html/main/templates_externalfiles . But that's really to allow existing php scripts to be used without rewriting them, and you probably wouldn't want to write new stuff that way.
But you could use the same idea - use that hook location, put in your query instead of the ob_start/ob_end thing, and when you have a variable set, use the preRegister line like in that example. The downside of that is that it will do that work on every page instead of just the page that is using it, but you could get it working then worry about the efficiency later.
Edit: like
$results = $db->query_read("SELECT title FROM " . TABLE_PREFIX . "forum WHERE forumid = 1");
while ($forum = $db->fetch_array($results))
{
// Do something to format the titles
$forumtitles .= $forum['title'] . "<BR/>";
}
vB_Template::preRegister('vbcms_page', array('forumtitles' => $forumtitles));
jgrietveld
08-09-2011, 09:01 PM
Thanks a million for your help and explanations, kh99. It is starting to make sense now. I will first try getting it to work the 'correct' way through hooks and plugins.
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.