Jump to content


Photo

[SOLVED] Functions, classes etc..


  • Please log in to reply
2 replies to this topic

#1 Nooty

Nooty
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 12 October 2006 - 05:03 PM

Firstly hello all, im new here. (It's also my birthday today so all well wishers welcome ;))

And now to my problem.

I decided to make a template system, inspired by the way vBullein works, where i have a template in one table, and the page data in another, i pull out the template and the page data and use eval to slap it all together intoa  variable, then print the result. This all works peachy.

Satisfied with this i decided to develop the code further, and impliment a plugin system. Again, the plugin code (which was plain PHP) was stored in the database which i pulled out and used eval to run it. I wanted to have plugin locations within my code - like at the start, the beginning etc. I wrote the code and it worked fine, but it looked messy, not to mention a pain copy+paste the plugin code wherever i wanted it.

So i decided to make a function out of the code, but unless i declare each variable i use as global in the template, i cannot use them outside the function.

Any help?

Main Code
<?
include("./config.php");
include("./global.php");

$symplyte = new symplyte;
symplyte::database($username, $password, $host, $database);

//Which Page?
$resource = $_GET['resource'];
if($resource == ""){
$resource = 1; //This is the homepage id
}

//Get Page Data
$sql = "SELECT * FROM " . $prefix . "pagedata WHERE resource = '$resource' LIMIT 1";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$content = $row[pagedata]; // <-My Page Content

//Get Template
$sql = "SELECT name FROM " . $prefix . "template WHERE id = '$row[template]' LIMIT 1";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$usetemplate = $row[name]; <-The Template

//This is the start of the plugin code
$sql = "SELECT code FROM " . $prefix . "plugin WHERE location = 'main_end'";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)){
	$pluginCode = $row[code];
	if( FALSE === @eval($pluginCode)){
		print("There was a problem running plugin: $row[id]<br/>");
	}
}
//And this is the end of the plugin code

eval('$template = "' . symplyte::fetch_template($usetemplate) . '";'); //Put the template with all the variables together

print($template); //Output it all
?>
[/CODE]
[b]Functions etc[/b]
[CODE]
<?
class symplyte
{	
	//Fetch Template From Database
	function fetch_template($template)
	{
		global $prefix;
	
		$sql = "SELECT * FROM " . $prefix . "template WHERE name = '$template' LIMIT 1";
		$result = mysql_query($sql);
		$row = mysql_fetch_array($result);
		$template = addslashes($row[template]);
		return($template);
	}
	//Database Functions
	function database($username, $password, $host, $database)
	{
		if(!@mysql_connect($host, $username, $password)){
			database_error(mysql_error());
		}
		if(!@mysql_select_db($database)){
			database_error(mysql_error());
		}
	}
	
	function database_error($error)
	{
		print("Something went wrong, and i was unable to connect to the database. The error returned was: $error");
	}
}
?>
[/code]
a template would include html with a $content variable for the main content, then $testPlugin1 etc for any plugin variables, and then a plugin allong the lines of

[CODE]
$testPlugin1 = date("d/m/Y h:i");[/CODE]


#2 Nooty

Nooty
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 12 October 2006 - 07:01 PM

what if i were to make a function and then take all of the plugin code, and store it all as a string and then return that string, THEN eval it from the main code? ie $pluginCode = symplyte::loadplugins('main_end');

im a little under the influence atm, but i find having a few shandys helps me be creative - what do you think?

#3 Nooty

Nooty
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 12 October 2006 - 09:34 PM

just in case anyone is researching this same subject, the aforementioned idea worked a charm.

function load_plugins($location)
	{
		global $prefix;
		
		$sql = "SELECT code FROM " . $prefix . "plugin WHERE location = 'main_end'";
		$result = mysql_query($sql);
		while($row = mysql_fetch_array($result)){
			$pluginCode .= $row[code] . "\n\n";
		}
		
		return($pluginCode);
		
	}[/code]

and..

[code]$plugins = symplyte::load_plugins('main_end');
eval($plugins);[/code]





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users