MyBB Community Forums

Full Version: Add custom PHP variable to postbit
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm trying to figure out how to load and display a PHP variable from another file in the postbit.
For example, I have a PHP file that gets some data from a MySQL database, and I want to display that in a postbit.

I've tried using include() in the template editor to load the var from a PHP, but that didn't work, so, is there any way I can do this?
core file edit or plugin.

is the data you are trying to add in a separate table in the same or another database or is the data already in the existing post table? will it need to be obtained on a per post basis, per user basis or something else?
(2012-07-16, 10:59 PM)pavemen Wrote: [ -> ]core file edit or plugin.

is the data you are trying to add in a separate table in the same or another database or is the data already in the existing post table? will it need to be obtained on a per post basis, per user basis or something else?

It is on a separate table on another database and needs to be obtained on a per user basis
I would suggest that you use a plugin that runs functions at two locations.

At the start of the showthread code (showthread_start hook) query the myBB post table for all the DISTINCT UIDs that posted in the current TID and load up a global array of UIDs (with UID as the key). Then connect to your other database/table and get the data for those UIDs

e.g. $mydata[1] = "admin"; $mydata[234] = "something else"

The second plugin function runs during the build_postbit function (postbit hook) where you update $post array with your custom content based on the $post['uid'] value.

e.g. $post['mycustomvar'] = $mydata[$post['uid']];

and simply put {$post['mycustomvar']} in the postbit template where you want it.

Right now, this is the least resource intensive method in my opinion.

Another method is to use str_replace() after the fact. But since the data you need is in another DB/table, its not really optimal for you, so this is an alternative to the alternative

In the postbit template add <mycustomvar_{$post['uid']}> as a placeholder. This will make a unique to the author string you can replace.

Then at showthread_start, you can do the same as above. Don't do anything for postbit hook. Create the second function using showthread_end and do a str_replace for every item in the $mycustomvar array

foreach($$mycustomvar as $uid => $replace)
{
$showthread = str_replace('<mycustomvar_'.$uid.'>', $replace, $showthread);
}