Jump to content

Archived

This topic is now archived and is closed to further replies.

Nooty

[SOLVED] Functions, classes etc..

Recommended Posts

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?

[b]Main Code[/b]
[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][/code]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
just in case anyone is researching this same subject, the aforementioned idea worked a charm.

[code]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][/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.