vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB4 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=252)
-   -   Using External PHP Pages (https://vborg.vbsupport.ru/showthread.php?t=269457)

nima6 08-31-2011 02:58 PM

Using External PHP Pages
 
Im trying to include an external php file in the VB template. I found this tutorial. I've added the following plugin with Global_Start as the Hook Location.

Plugins are Enabled in VB Options and the plugin itself is activated too.

Code:

ob_start();  include('/home/.../bh_includes/head_common.php');
  $includedheader = ob_get_contents();
  ob_end_clean();

And then I'm calling for {vb:raw includedheader} in the FORUMDISPLAY file.

Nothing is showing up in my template though.

kh99 08-31-2011 03:07 PM

You need to add this:

PHP Code:

vB_Template::preRegister('FORUMDISPLAY', array('includedheader' => $includedheader)); 


nima6 08-31-2011 03:31 PM

Thanks

Where would i include that?

Lynne 08-31-2011 03:37 PM

<a href="https://vborg.vbsupport.ru/showthread.php?t=242454" target="_blank">[vBulletin 4] Simple way of including an external PHP file</a>

kh99 08-31-2011 03:51 PM

Quote:

Originally Posted by nima6 (Post 2240461)
Thanks

Where would i include that?

At the end, as in the article Lynne linked to. And sorry, BTW, what I posted actually had an error in it. :o

nima6 08-31-2011 04:25 PM

So this is what i have right now:

Code:

ob_start();  include('/home/XXXX/public_html/bh_includes/head_common.php');
  $includedphp = ob_get_contents();
  ob_end_clean();
vB_Template::preRegister('header', array('includedheader' => $includedheader));

And using {vb:raw includedheader} in the "header" file in the template.

Its still not working

kh99 08-31-2011 04:46 PM

Sorry, I probably confused you with my error. This should work:

PHP Code:

ob_start();  
include(
'/home/XXXX/public_html/bh_includes/head_common.php');
$includedphp ob_get_contents();
ob_end_clean();
vB_Template::preRegister('header', array('includedheader' => $includedphp)); 


with this in the header template: {vb:raw includedheader}

(the variable names needed to match in the ob_get_contents() and preRegister lines).

ICThawk 08-31-2011 05:40 PM

I have a very similar question. My site is www.wavingthewheat.com

I am using a php script to count the number of rows in a table on the database. The script is loaded at www.wavingthewheat.com/chatuser.php

I am trying to have the output of that file displayed in my navbar template. I have created a plugin with the following code

PHP Code:

ob_start();
  include(
'/home/bluepr12/public_html/wavingthewheat.com/chatuser.php');
  
$includedphp ob_get_contents();
  
ob_end_clean();

vB_Template::preRegister('nabvar',array('includedphp ' => $includedphp)); 

In my navbar template I have {vb:raw includedphp} as part of the Chat link.

The php script works and displays the number of online users, however I can't get anything to display in the template using the plugin.

kh99 08-31-2011 05:50 PM

Quote:

Originally Posted by ICThawk (Post 2240523)
The php script works and displays the number of online users, however I can't get anything to display in the template using the plugin.

It looks like you've misspelled 'navbar' in your preregister call.

nima6 08-31-2011 06:14 PM

Quote:

Originally Posted by kh99 (Post 2240504)
Sorry, I probably confused you with my error. This should work:

PHP Code:

ob_start();  
include(
'/home/XXXX/public_html/bh_includes/head_common.php');
$includedphp ob_get_contents();
ob_end_clean();
vB_Template::preRegister('header', array('includedheader' => $includedphp)); 


with this in the header template: {vb:raw includedheader}

(the variable names needed to match in the ob_get_contents() and preRegister lines).



That worked. Thanks a bunch.

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

I must the most idiot on this forums.

I'm trying to include a second php file (for footer this time) and am having problems again.

This time this is what im using:

Code:

ob_start(); 
include('/home/XXXX/public_html/bh_includes/footer-common.php');
$includedfphp = ob_get_contents();
ob_end_clean();
vB_Template::preRegister('footer', array('includedfooter' => $includedfphp));

and {vb:raw includedfooter}

That doesnt work either. I changed the includedphp to includedfphp because using just the includedphp didnt work. I assumed it was because that variable was already being used elsewhere so i changed it (nothing happened though)

ICThawk 08-31-2011 06:50 PM

Quote:

Originally Posted by kh99 (Post 2240528)
It looks like you've misspelled 'navbar' in your preregister call.

Good catch. Sadly with that fixed it still doesn't work. Ideas?

nima6 08-31-2011 07:14 PM

It is official. I am an idiot. I had a spelling error as well

kh99 08-31-2011 07:41 PM

Quote:

Originally Posted by ICThawk (Post 2240550)
Good catch. Sadly with that fixed it still doesn't work. Ideas?

Which hook are you using? Try using parse_templates if you aren't already.

ICThawk 09-01-2011 12:16 AM

Quote:

Originally Posted by kh99 (Post 2240583)
Which hook are you using? Try using parse_templates if you aren't already.

I don't know what parse templates are.

I used the hook global_start. execution order 5.

Does the PHP code need to be contained in <?php tags?

Here is the code in my php file in case it matters.

PHP Code:

<?php

require_once('./global.php');

// Query the database and get the count 
$result mysql_query("SELECT * FROM ajax_chat_online"); 
$num_rows mysql_num_rows($result); 

// Display the results 
echo $num_rows


?>


kh99 09-01-2011 04:39 AM

You don't need the <?php in plugin code.

'parse_templates' is a different hook location, but 'global_start' will work so you're OK.

I think you had another typo in the code you posted above (there's an extra space in 'includedphp ', which I now see is a typo in the "Rendering Templates..." article).

Try this:

PHP Code:

ob_start();
  include(
'/home/bluepr12/public_html/wavingthewheat.com/chatuser.php');
  
$includedphp ob_get_contents();
ob_end_clean();

vB_Template::preRegister('navbar',array('includedphp' => $includedphp)); 




BTW, not to confuse things further, but you could also do something like this in the plugin:

PHP Code:

$result mysql_query("SELECT * FROM ajax_chat_online");  
$num_chatting mysql_num_rows($result);  
vB_Template::preRegister('navbar',array('num_chatting' => $num_chatting)); 


and {vb:raw num_chatting} in the template, and you wouldn't need the external file.

ICThawk 09-01-2011 01:55 PM

kh99 thanks a ton for your help. I decided to go with your last option and have the plugin execute the php.

I have a couple more questions though.

1.) What hook do I need to use so that this will display on the front page. It works on the forum, but not on the CMS.

2.) I would like to list who is online on the forum home. In that ajax_chat_online table each online user is stored in a row. I would like to display the column userName for all entries separated by a comma. Any thoughts?

Again, thank you very much for all your help so far!

kh99 09-01-2011 02:23 PM

Quote:

Originally Posted by ICThawk (Post 2240825)
1.) What hook do I need to use so that this will display on the front page. It works on the forum, but not on the CMS.

Try global_bootstrap_init_start

Quote:

2.) I would like to list who is online on the forum home. In that ajax_chat_online table each online user is stored in a row. I would like to display the column userName for all entries separated by a comma. Any thoughts?
PHP Code:

global $vbulletin;

$results $vbulletin->db->query_read_slave("SELECT userName FROM ajax_chat_online");
while (
$row $vbulletin->db->fetch_array($results))
    
$chat_userlist[] = $row['userName'];
$chat_userlist implode(','$chat_userlist);
$vbulletin->db->free_result($results);
vB_Template::preRegister('navbar', array('chat_userlist' => $chat_userlist)); 


Obviously, if you're using the same plugin as you are for the count you could combine both so you're not doing two queries.

ICThawk 09-01-2011 02:51 PM

Thanks! You had a typo but I caught it. Thanks for all of your help!!

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

Man you have no idea how helpful you have been. With your help I have put together the following Mod Thread https://vborg.vbsupport.ru/showthread.php?p=2240829 to help everyone else who is in the same boat as me.

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

Well I have found one hiccup. With the second plugin. The one that displays the userNames.

If no one is in the chat and the code returns a NULL result then it breaks the site. Gives me a cookie in header already sent error. Thoughts??

ICThawk 09-02-2011 06:08 PM

Bump

kh99 09-02-2011 06:19 PM

Sorry, I always seem to miss when posts get auto-merged.

Anyway, yeah, didn't think of that. You want something like this:

PHP Code:

global $vbulletin

$results $vbulletin->db->query_read_slave("SELECT userName FROM ajax_chat_online"); 
while (
$row $vbulletin->db->fetch_array($results)) 
    
$chat_userlist[] = $row['userName']; 
if (
is_array($chat_userlist))
{
    
$chat_userlist implode(','$chat_userlist); 
    
$vbulletin->db->free_result($results); 
}
else
{
    
// set $chat_userlist to a "no one chatting" message if you want, or leave blank.
    
$chat_userlist '';
}
vB_Template::preRegister('navbar', array('chat_userlist' => $chat_userlist)); 


ICThawk 09-06-2011 03:38 PM

Perfect! Thanks!!


All times are GMT. The time now is 02:08 PM.

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.01191 seconds
  • Memory Usage 1,816KB
  • 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
  • (3)bbcode_code_printable
  • (9)bbcode_php_printable
  • (8)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (21)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
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete