Jump to content

[SOLVED] Functions, classes etc..


Nooty

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.