MyBB Community Forums

Full Version: PHP in pages with template?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Can anyone tell me how I can add a new page through page manager that contains PHP and HTML, but still have the page template involved?

I want to add a BF3 stats page to my clan site, however the only option I have in Page Manager to include PHP is to not use the site template, which leave me with a not very nice looking page.

How can I include PHP and the template?

Thanks in advance

EDIT: Ignore me.

I'm using pages with the page manager that copies over the site's theme and i can use PHP.
(2011-11-16, 04:07 PM)Techmonkey Wrote: [ -> ]Can anyone tell me how I can add a new page through page manager that contains PHP and HTML, but still have the page template involved?

I want to add a BF3 stats page to my clan site, however the only option I have in Page Manager to include PHP is to not use the site template, which leave me with a not very nice looking page.

How can I include PHP and the template?

Thanks in advance

You can use the templates I believe, just make sure to use something like the following (I don't use page manager, so this may not work):

global $headerinclude, $header, $footer;

// page content....

output_page($headerinclude.$header.$content.$footer);

As I said, that may well not work, but I believe it should.
Euantor > Thank you that is very much closer: http://h2hclan.net/misc.php?page=bf3stats

It's not quite right yet, any clues?

<?php
global $headerinclude, $header, $footer;

// page content....
output_page($headerinclude.$header);

// Created by @alexcroox of http://www.sarsclan.co.uk

$playerData = array('players' => array(), 'opt' => array());

// Our list of players
$playerData['players'][] 		= 'CadetUK';
$playerData['players'][] 		= 'TechMonkey_h2h';
$playerData['players'][] 		= 'NapalmUK';
$playerData['players'][] 		= 'H2Hfrisson';
//$playerData['players'][] 		= 'FacetiousOne';
$playerData['players'][] 		= 'ratman-gbuk';
$playerData['players'][] 		= 'Acsitmark';
$playerData['players'][] 		= 'mattwhi2';
$playerData['players'][] 		= 'Ranger7UK';
$playerData['players'][] 		= 'JackD_gb';
$playerData['players'][] 		= 'Baghead28';
$playerData['players'][] 		= 'HMPDEATH';
$playerData['players'][] 		= 'Nekozy79';
$playerData['players'][] 		= 'Giz88';
$playerData['players'][] 		= 'edgleyUK';
$playerData['players'][] 		= 'Baggit';
$playerData['players'][] 		= 'BoxingFluffy';
$playerData['players'][] 		= 'Notror';
$playerData['players'][] 		= 'H2HGunner';
$playerData['players'][] 		= 'Sajanus01';
$playerData['players'][] 		= 'TalHawkins81';


/* In this example we want as little information to come back from the API as possible.
** Therefore we are calling the "clear" function, which means we need to manually enable
** each stat group we need. This will help keep the size of the response down, and therefore execution time.
*/
$playerData['opt']['clear']		= true;

// Data we want to be returned
$playerData['opt']['scores']	= true;
$playerData['opt']['global']	= true;
$playerData['opt']['nextranks']	= true;
$playerData['opt']['rank']		= true;
$playerData['opt']['kits']		= true;
$playerData['opt']['imgInfo']	= true;

// Convert lists to JSON ready for the curl post request
$postData						= array();
$postData['players']			= json_encode($playerData['players']);
$postData['opt']				= json_encode($playerData['opt']);

// This example hardcodes "pc" players
$c = curl_init('http://api.bf3stats.com/pc/playerlist/');
curl_setopt($c, CURLOPT_HEADER, false);
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_USERAGENT, 'BF3StatsAPI/0.1');
curl_setopt($c, CURLOPT_HTTPHEADER, array('Expect:'));
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_POSTFIELDS, $postData);
$response 	= curl_exec($c);
$statusCode	= curl_getinfo($c, CURLINFO_HTTP_CODE);
curl_close($c);

// 200 means a successful call
if($statusCode == 200) 
{
	// Decode JSON Data into an array we can easily parse
	$data = json_decode($response, true);
	
	// Counter for our players array
	$i = 0;
	
	// Loop through each of our players we set above and see what data we have for them
	foreach($playerData['players'] AS $player)
	{
		// Does this player exist in the returned data?
		if(isset($data['list'][$player]))
		{
			// This checks to see if the player actually has any stats yet on the site
			if($data['list'][$player]['status'] == "data")
			{
				$stats[$i]['name'] 		= $data['list'][$player]['name'];
				$stats[$i]['rank'] 		= $data['list'][$player]['stats']['rank']['nr'];
				$stats[$i]['score'] 	= $data['list'][$player]['stats']['scores']['score'];				
				$stats[$i]['time'] 		= $data['list'][$player]['stats']['global']['time'];
				$stats[$i]['kills'] 	= $data['list'][$player]['stats']['global']['kills'];
				$stats[$i]['deaths'] 	= $data['list'][$player]['stats']['global']['deaths'];
				$stats[$i]['skill'] 	= $data['list'][$player]['stats']['global']['elo'];
				
				$stats[$i]['kits'] 		= array();
				
				$stats[$i]['kits'][] = array('name' => 'assault', 	'time' => $data['list'][$player]['stats']['kits']['assault']['time']);
				$stats[$i]['kits'][] = array('name' => 'engineer', 	'time' => $data['list'][$player]['stats']['kits']['engineer']['time']);
				$stats[$i]['kits'][] = array('name' => 'recon', 	'time' => $data['list'][$player]['stats']['kits']['recon']['time']);
				$stats[$i]['kits'][] = array('name' => 'support', 	'time' => $data['list'][$player]['stats']['kits']['support']['time']);
				
				// Work out which is the most used kit
				usort($stats[$i]['kits'], 'sortKits');
				
				$stats[$i]['class'] 	= $stats[$i]['kits'][0]['name'];
				
				$i++;
			}
		}
	}
	
	// Order players based on score
	usort($stats, 'sortPlayers');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<title>Scrollable HTML table</title>
	<meta http-equiv="Content-type" content="text/html; charset=UTF-8" />
	<script type="text/javascript" src="webtoolkit.sortabletable.js"></script>
 
	<style>
		table {
			text-align: left;
			font-size: 12px;
			font-family: verdana;
			background: #c0c0c0;
		}
 
		table thead  {
			cursor: pointer;
		}
 
		table thead tr,
		table tfoot tr {
			background: #c0c0c0;
		}
 
		table tbody tr {
			background: #f0f0f0;
		}
 
		td, th {
			border: 1px solid white;
		}
	</style>
</head>
 
<body>

	<!-- Lets output our players table -->
<table cellspacing="1" cellpadding="2" class="" id="myTable" width="500">
	    <thead>
			<tr>
				<th>#</th>
				<th>Player</th>
				<th>Rank</th>
				<th>Score</th>
				<th>K/D</th>        
				<th>Ratio</th>
				<th>Skill</th>
				<th>Time</th>
			</tr>
	    </thead>
	
	    <tbody>
	    <?
		for($out = 0; $out < count($stats); $out++):
			$pos = $out + 1; ?>
			
		        <tr>
		        	<td class="first"><?=$pos?></td>
		            <td class="soldier">
						<img src="http://h2hclan.net/testarea/bf3stats/kits/<?=$stats[$out]['class']?>.png" alt="" />
						<a title="click to view full stats" href="http://bf3stats.com/stats_pc/<?=$stats[$out]['name']?>" target="_blank" rel="nofollow">
							<?=$stats[$out]['name']?>
						</a>
					</td>
		            <td align="center">
		            	<img src="http://h2hclan.net/testarea/bf3stats/rankstiny/r<?=$stats[$out]['rank']?>.png" alt="<?=$stats[$out]['rank']?>" />
					</td>
		            <td><?=number_format($stats[$out]['score'])?></td>
		            <td><?=number_format($stats[$out]['kills'])?> / <?=number_format($stats[$out]['deaths'])?></td>
		            <td><?=round($stats[$out]['kills']/$stats[$out]['deaths'], 2)?></td>  
		            <td><?=round($stats[$out]['skill'])?></td>  
		            <td><?=sec2hms($stats[$out]['time'])?>h</td>      
		        </tr>	    
		<?
		endfor; ?>
				
	    </tbody>
	</table>
    
    <script type="text/javascript">
var t = new SortableTable(document.getElementById('myTable'), 100);
</script>
 
</body>
</html>
	
	<?
	// If you add ?debug=1 to the end of your script URL in the browser you can see what data is returned
	if(isset($_GET['debug']))
	{
		echo '<pre>';
			print_r($data);
		echo '</pre>';
	}	
} 
else 
{
	echo 'Error contacting API status code: '.$statusCode;
}

// Lets dump our functions down here, these would be better in a seperate include though

function sortKits($x, $y)
{
	if($x['time'] == $y['time'])
	{
		return 0;
	}
	elseif($x['time'] < $y['time'])
	{
		return 1;
	}
	else
	{
		return -1;
	}
}

function sortPlayers($x, $y)
{
	if($x['score'] == $y['score'])
	{
		return 0;
	}
	elseif($x['score'] < $y['score'])
	{
		return 1;
	}
	else
	{
		return -1;
	}
}

// Convert seconds to hours
function sec2hms($sec, $padHours = false) 
{
	$hms 	= "";
	$hours 	= intval(intval($sec) / 3600); 
	$hms 	.= ($padHours)? str_pad($hours, 2, "0", STR_PAD_LEFT). ':' : $hours;	
	return $hms;

}

output_page($content.$footer); 

?>
You should only call output_page() once Smile It's used to output the whole page. You'll also want to output a container as well in all likelihood. EG: output_page($headerinclude.$header.'<div class="container">'.$content.'</div'.$footer);

I haven't checked all your code though. You'll also want to remove all the HTML output you're doing as it'll cause problems.
Thank you again, although I am little confused?

I am guessing I am using $content to contain all the stuff I want on the page? then calling it in the Output_page

How do I put all the HTML and PHP in $content?
Sorry just re-read your post? If I remove all the HTML how will it know what to show on the page for the stats?
You can make additional pages without the page manager.

<?php
define("IN_MYBB", "1");
require_once "./global.php"
/* Your code goes here 
* To evaluate a templates values, use the eval function.
* Output the page at the end like this:
output_page($page);
?>

Replace $page with the value you used in the final eval function. Make sure the template you evaluate has {$headerinclude} in the head section and {$header} in the body section of it.