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
(2014-07-03, 06:16 PM)Destroy666 Wrote: [ -> ]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.

Edit 3 fixed the problem.

I added the templates manually, im just converting the hard-coded script into a plugin so its much easier to manage. Problem is, the template isn't showing up on the header.

By the way, I renamed top4headerboxes -> streams_top4
You need to globalise $db, $top4streams and $templates in the mystreams_global_start function.
function mystreams_global_start(){
global $mybb, $db, $top4streams, $templates;
still not showing :/

<?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_header = array(
        'sid'            => 'NULL',
        'name'        => 'mystreams_enable_header',
        'title'            => 'Do you want to enable My Streams on header?',
        '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_header);
    rebuild_settings();
} 
function mystreams_deactivate()
  {
	  global $db;
	  $db->query("DELETE FROM ".TABLE_PREFIX."settings WHERE name IN ('mystreams_enable_header')");
	  $db->query("DELETE FROM ".TABLE_PREFIX."settinggroups WHERE name='mystreams'");
	  rebuild_settings();
 } 
$plugins->add_hook('global_end', 'mystreams_global_start'); 
function mystreams_global_start(){
        global $mybb, $db, $top4streams, $templates; 
	if ($mybb->settings['mystreams_enable_header'] == 1){
		$result = $db->query("SELECT * FROM mybb_streams WHERE online = '1' ORDER BY votes DESC LIMIT 0, 4");
		while($row = $db->fetch_array($result))
		  {
		  if ($row['online'] == 1) {
		  // set variables used in the template code here:
		$streamchannel = $row['channel'];
		$streampreview = $row['preview'];
		$stream_id = $row['userid'];
		$stream_username = get_user($row['userid']);
		$formatted_name = format_name($streamusername['username'], $streamusername['usergroup'], $streamusername['displaygroup']);
		$desc = $row['description'];
		eval("\$top4streams .= \"".$templates->get("streams_top4")."\";");
		  }
  } 
}
} 
Your missing a closing brace for the if inside the while...
if ($row['online'] == 1) {
// Code here...
}
(2014-07-04, 07:12 AM)JordanMussi Wrote: [ -> ]Your missing a closing brace for the if inside the while...
if ($row['online'] == 1) {
// Code here...
}

Not sure what you mean by that. It is closed I believe.
That's why I told you to use proper intendation (TABbed)... Took me one minute to correct it and now every brace is clearly visible:
<?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_header = array(
		'sid' => 'NULL',
		'name' => 'mystreams_enable_header',
		'title' => 'Do you want to enable My Streams on header?',
		'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_header);
	rebuild_settings();
}

function mystreams_deactivate()
{
	global $db;
	$db->query("DELETE FROM ".TABLE_PREFIX."settings WHERE name IN ('mystreams_enable_header')");
	$db->query("DELETE FROM ".TABLE_PREFIX."settinggroups WHERE name='mystreams'");
	rebuild_settings();
}

$plugins->add_hook('global_end', 'mystreams_global_start');
function mystreams_global_start()
{
	global $mybb, $db, $top4streams, $templates; 
	if ($mybb->settings['mystreams_enable_header'] == 1)
	{
		$result = $db->query("SELECT * FROM mybb_streams WHERE online = '1' ORDER BY votes DESC LIMIT 0, 4");
		while($row = $db->fetch_array($result))
		{
			if ($row['online'] == 1) 
			{
				// set variables used in the template code here:
				$streamchannel = $row['channel'];
				$streampreview = $row['preview'];
				$stream_id = $row['userid'];
				$stream_username = get_user($row['userid']);
				$formatted_name = format_name($streamusername['username'], $streamusername['usergroup'], $streamusername['displaygroup']);
				$desc = $row['description'];
				eval("\$top4streams .= \"".$templates->get("streams_top4")."\";");
			}
		} 
	}
}

Also, as I said in my first post in this topic, which you didn't follow carefully as well:
(2014-07-02, 08:49 AM)Destroy666 Wrote: [ -> ]hook into global_start
Some templates like header are eval'd before the global_end hook so your variable would be always empty there with this code:
$plugins->add_hook('global_end', 'mystreams_global_start');

If still nothing is displayed after this change, please screen the whole streams_top4 template and location of $top4streams variable in other template. And make sure your setting is enabled and there is at least 1 online stream.
I managed to figure out the problem by adding this after the loop.
echo $top4streams;

But its echoing through the footer, and unexpected places. I want it so it only echos on the HEADER.


Destroy666 solved my problem. Thank you Smile
Pages: 1 2