PDA

View Full Version : Using External PHP Pages


nima6
08-31-2011, 02:58 PM
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.


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:

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
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:


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:

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

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

vB_Template::preRegister('nabvar',array('includedp hp ' => $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
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
Sorry, I probably confused you with my error. This should work:

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 1314819916 at 1314819916 ---------------

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:


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
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
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
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

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:

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

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




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

$result = mysql_query("SELECT * FROM ajax_chat_online");
$num_chatting = mysql_num_rows($result);
vB_Template::preRegister('navbar',array('num_chatt ing' => $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
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

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?


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 1314893620 at 1314893620 ---------------

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 1314962655 at 1314962655 ---------------

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:

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!!