PHP in database

Tabo

New Member
I have a CMS, where, as you might have guessed the content of a page is grabbed from a mysql database.

My problem is some of the pages require PHP. Vbull does something like:

PHP:
<if condition="$whatever">echo "whatever";</if>

Can one of you please help me by providing me with or linking me to the source of this that is easy enough for me to understand?

Thanks a lot.
 

Hoxxy

New Member
If you have a PHP or HTML file that you want to include in your vBulletin forum, create a plugin that references that file. Then add a variable to the template of your choice where that file's contents should appear.


Including an HTML file:

1. Create a Plugin for global_start with this code:

PHP:
$includedhtml = implode('', file('path/to/this/file/myfile.html'));
replacing the path and filename with the correct path and filename of the HTML file you want to include. The contents of myfile.html will be copied to the variable
PHP:
$includedhtml

2. Place
PHP:
$includedhtml
in one of your templates, such as header, navbar, FORUMHOME, depending upon where you want the contents of your HTML file to appear.

Note: The Plugin system must be enabled in vBulletin Options -> Plugin System for plugins to work. It is disabled by default.


Including a PHP file:
1. Create a Plug-in for global_start with these contents:
PHP:
ob_start();
   include('path/to/this/file/myfile.php');
   $includedphp = ob_get_contents();
ob_end_clean();
replacing the path and filename with the correct path and filename of the PHP file you want to include. The code in myfile.php will execute and any output generated by this script will be stored in $includedphp.

2. Place
PHP:
$includedphp
in one of your templates, such as header, navbar, FORUMHOME, depending upon where you want the contents of your PHP file to appear.

Note: The Plugin system must be enabled in vBulletin Options -> Plugin System for plugins to work. It is disabled by default.

WARNING: Plugins that contain invalid or malicious code may cause your forum to stop functioning or even lead to data loss. If a Plugin has made your forum inaccessible, disable the plugins.

What are ob_start(), ob_get_contents(), ob_end_clean(), etc? What is Output Buffering?
Most PHP files that you might wish to include in your forum contain echo or other output statements in your PHP file, it will break vBulletin because it is still in the process of initializing when it loads your PHP file. All echo and other output commands must be output buffered using ob_start, ob_clean, etc. commands. The output of your PHP script will be buffered for later use and inserted into a variable. All other statements in the PHP script will execute normally.

A word about variables.
It is very important that any variables initialized in your PHP script do not overlap built-in vBulletin variables or you will get unpredictable results. It may be advisable to create a PHP script just for inclusion in your forum rather than including a larger script used by another part of your website.

Which hook should I use?
The hook I've used above (global_start) makes your HTML or PHP file available in almost every template on your vBulletin forum. You may wish to include a PHP file or HTML file only on certain pages or parts of your forum. You'll need to select the correct hook where your code should be loaded. To determine which hook you should use, turn on Debug and then make this change to the appropriate functions php file.

Note: If you wish to include() multiple PHP files, make sure you use ob_clean() before each include() to reset the buffer.
 

Tabo

New Member
Sorry, I forgot to mention it is out of VB. Thanks for them help, by the way by clicking thanks will it close the thread?
 

Hoxxy

New Member
Ah ok so you just want to include php to your site, soory must have read the original post wrong...lol
I'm no php guru but you could try browsing here:
PHP Tutorial

Clicking the thanks button will not close the thread ;)
 

Tabo

New Member
I mean, something like:

<?php

$content = mysql_query("SELECT content FROM cms WHERE id = ".mysql_real_escape_string($_POST['id'])." LIMIT 0,1") or die();

while($a = mysql_fetch_assoc($content)){
echo $a['content'];
}

?>

where $a['content'] is the following string:

"Hello <?php echo 'world' ?>"

However the problem I have is that it is coming from a db. I can't use eval or anything either.
 

bluescorpion

New Member
Hoxxy gave you the right answer. You have to create a plugin and put ALL you php code in it. You can not use php code in vb any other way. echo is not allowed in the vb code and won't work.

You have to get your code to work in the plugin and eval it as a $variable in the vb code.
 

Hoxxy

New Member
bluescorpion said:
Hoxxy gave you the right answer. You have to create a plugin and put ALL you php code in it. You can not use php code in vb any other way. echo is not allowed in the vb code and won't work.

You have to get your code to work in the plugin and eval it as a $variable in the vb code.

Nah I think he just wants to fetch info from a database this has nothing to do with vBulletin just PHP in general.......
 
Top