MyBB Community Forums

Full Version: Running a PHP code in the header
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Ok, so I was able to run this PHP code by placing it in the index.php, but how do I run it on my header. I tried placing the code on global.php but I ended up with a blank page on the website.

This code displays the top 3 streams on the database listed upon votes.

$parser = new postParser();
$parser_options = array(
    'allow_html' => 'no',
    'allow_mycode' => 'yes',
    'allow_smilies' => 'yes',
    'allow_imgcode' => 'yes',
    'filter_badwords' => 'yes',
    'nl2br' => 'yes'
);
$result = $db->query("SELECT * FROM <snipped for safety> WHERE online = '1' ORDER BY votes DESC LIMIT 0, 3");
while($row = $db->fetch_array($result))
  {
  if ($row['online'] == 1) {
  // set variables used in the template code here:
$stream_channel = $row['channel'];
$stream_preview = $row['preview'];
$streamid = $row['userid'];
$streamusername = get_user($row['userid']);
$formattedname = format_name($streamusername['username'], $streamusername['usergroup'], $streamusername['displaygroup']);
$desc = $parser->parse_message($row['description'], $parser_options);
eval("\<snipped for safety> .= \"".$templates->get("<snipped for safety>")."\";");
  }
  }

EDIT: I replaced some terms with <snipped for safety> as a security precaution
You can hook into global_start with a plugin, then assign your code to a variable in the hook and add that variable to header template with find_replace_templatesets() function during installation/activation.
(2014-07-02, 08:49 AM)Destroy666 Wrote: [ -> ]You can hook into global_start with a plugin, then assign your code to a variable in the hook and add that variable to header template with find_replace_templatesets() function during installation/activation.

Alright. I did make the plugin for it, but when I put the file in the "plugins" folder, the plugins page comes out as blank. Is there something wrong with my code perhaps?
From what is shown you do not have the _info, _install, _activate, _deactivate, and _uninstall functions.
I turned that code to a plugin, it works but when I enable it, the whole website turns into a white screen of death.

<?php
if(!defined("IN_MYBB"))
{
    die("You Cannot Access This File Directly. Please Make Sure IN_MYBB Is Defined.");
} 
function mystreams_info()
{
return array(
        "name"  => "My Streams",
        "description"=> "Exclusive for 1shotGG",
        "website"        => "http://1shotgg.com",
        "author"        => "Azelf",
        "authorsite"    => "http://1shotgg.com",
        "version"        => "1.0",
        "guid"             => "",
        "compatibility" => "16*"
    );
} 
function mystreams_activate() {
global $db;

$mystreams_group = array(
        'gid'    => 'NULL',
        'name'  => 'mystreams',
        'title'      => 'My Streams',
        'description'    => 'Settings For My Streams',
        'disporder'    => "1",
        'isdefault'  => "0",
    ); 
$db->insert_query('settinggroups', $mystreams_group);
$gid = $db->insert_id(); 
$mystreams_setting = array(
        'sid'            => 'NULL',
        'name'        => 'mystreams_enable',
        'title'            => 'Do you want to enable My Streams?',
        'description'    => 'If you set this option to yes, this plugin be active on your board.',
        'optionscode'    => 'yesno',
        'value'        => '1',
        'disporder'        => 1,
        'gid'            => intval($gid),
    ); 
    $db->insert_query('settings', $mystreams_setting);
  rebuild_settings();
} 
function mystreams_deactivate()
  {
  global $db;
 $db->query("DELETE FROM ".TABLE_PREFIX."settings WHERE name IN ('mystreams_enable')");
    $db->query("DELETE FROM ".TABLE_PREFIX."settinggroups WHERE name='mystreams'");
rebuild_settings();
 } 
$plugins->add_hook('global_start', 'mystreams_global_start'); 
function mystreams_global_start(){
global $mybb;
if ($mybb->settings['mystreams_enable'] == 1){
$parser = new postParser();
$parser_options = array(
    'allow_html' => 'no',
    'allow_mycode' => 'yes',
    'allow_smilies' => 'yes',
    'allow_imgcode' => 'yes',
    'filter_badwords' => 'yes',
    'nl2br' => 'yes'
);
$result = $db->query("SELECT * FROM mybb_streams WHERE online = '1' ORDER BY votes DESC LIMIT 0, 3");
while($row = $db->fetch_array($result))
  {
  if ($row['online'] == 1) {
  // set variables used in the template code here:
$stream_channel = $row['channel'];
$stream_preview = $row['preview'];
$streamid = $row['userid'];
$streamusername = get_user($row['userid']);
$formattedname = format_name($streamusername['username'], $streamusername['usergroup'], $streamusername['displaygroup']);
$desc = $parser->parse_message($row['description'], $parser_options);
eval("\$top4streams .= \"".$templates->get("top4headerboxes")."\";");
  }
  } 
}
} 
You have a few errors going on there.

You haven't declared $db as a global before using it.

You need to load the inc/class_parser.php file before you can create the object $parser.
require_once MYBB_ROOT . "inc/class_parser.php";

Next, for $parser_options you should be using boolean true and false or 1 for true and 0 for false.
(2014-07-03, 12:48 PM)dragonexpert Wrote: [ -> ]You have a few errors going on there.

You haven't declared $db as a global before using it.

You need to load the inc/class_parser.php file before you can create the object $parser.
require_once MYBB_ROOT . "inc/class_parser.php";

Next, for $parser_options you should be using boolean true and false or 1 for true and 0 for false.

I changed all that, but I am still getting a white screen.
Can you please provide an error log? That would help diagnose the problem.
[03-Jul-2014 11:11:59 America/Los_Angeles] PHP Fatal error: Call to a member function get() on a non-object in /home/*****/public_html/inc/plugins/*****.php on line 80

This is whats in line 80
eval("\$top4streams .= \"".$templates->get("top4headerboxes")."\";");
Also:
1. Use proper indentation... This code is really hard to read so I'm not wondering there may be errors.
2. Save your file as mystreams.php with UTF-8 without BOM encoding.
EDIT: 3. Globalise $templates (this causes the error)

And some irrelevant tips:
1. Use userid LEFT JOIN in query instead of get_user(). That way you will have 3 queries less.
2. I don't see any top4headerboxes template being created, nor $top4streams variable inserted into any other template, so I guess you added them manually?
3. Use (int) casting instead of intval(), it's faster.
Pages: 1 2