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

Reply
 
Thread Tools Display Modes
  #1  
Old 08-09-2011, 02:43 PM
jgrietveld jgrietveld is offline
 
Join Date: Jul 2011
Posts: 4
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Querying MySQL and echo-ing variables

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:

Code:
$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.

Also, I need to echo the contents of the
Code:
$results
variable. But how? What's the way to echo variables in vBulletin?

Thanks for your help!!

Jerry
Reply With Quote
  #2  
Old 08-09-2011, 02:47 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by jgrietveld View Post
So I want to perform a MySQL query to retrieve the Forum titles:

Code:
$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.
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.
Reply With Quote
  #3  
Old 08-09-2011, 02:54 PM
jgrietveld jgrietveld is offline
 
Join Date: Jul 2011
Posts: 4
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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 [DATE]1312905358[/DATE] at [TIME]1312905358[/TIME] ---------------

Or is there a direct variable which will output the forum titles?
Reply With Quote
  #4  
Old 08-09-2011, 03:03 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
  #5  
Old 08-09-2011, 03:43 PM
jgrietveld jgrietveld is offline
 
Join Date: Jul 2011
Posts: 4
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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 [DATE]1312909240[/DATE] at [TIME]1312909240[/TIME] ---------------

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:

Code:
{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!
Reply With Quote
  #6  
Old 08-09-2011, 05:18 PM
daveaite's Avatar
daveaite daveaite is offline
 
Join Date: Jul 2009
Location: Florida
Posts: 1,890
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
  #7  
Old 08-09-2011, 05:38 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by jgrietveld View Post
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/..._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

PHP Code:
$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)); 
Reply With Quote
  #8  
Old 08-09-2011, 09:01 PM
jgrietveld jgrietveld is offline
 
Join Date: Jul 2011
Posts: 4
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
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 12:01 AM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04721 seconds
  • Memory Usage 2,241KB
  • Queries Executed 11 (?)
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
  • (4)bbcode_code
  • (1)bbcode_php
  • (2)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (8)post_thanks_box
  • (8)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (8)post_thanks_postbit_info
  • (8)postbit
  • (8)postbit_onlinestatus
  • (8)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_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