Jump to content

Help with Classes


LejeuneBay

Recommended Posts

I have 2 classes, a template class and a bumper class (the bumper class redirects a user away from a page which is useless to the user but vital to internal server functions to add, edit, or delete from MySQL). Basically what problem I'm having is simplifying my programing code from 7 lines to 1 line.

 

The major problem resides with trying to call "globals" and a list of given variables to replace on a template.

 

For instance, when I put down

 

(Index.php)

$VARS = "\$SITE_TITLE, \$SITE_DESCRIPTION";

template("templates/default/index.tpl"…

 

The website appears just fine with the {SITE_TITLE} and {SITE_DESCRIPTION} replaced on the template.

 

But when I try to implement classes it doesn't replace any of the {VARIABLES} that are listed. Here is the code:

 

class template{


function display($file, $vars, $x) {

	if($x == 1 || $x == 2){

		$line	=	@implode(file($file),"");

	}else{

		$line	=	file_get_contents($file);
	}
	eval("global $vars;");
	$vars = str_replace(", ","",$vars);
	$vars_list = explode("\$",$vars);
	for($i =0; $i <= sizeof($vars_list); $i++) {    
		$line = str_replace("{".$vars_list[$i]."}",$$vars_list[$i],$line);
	}
	if($x == 1){
		print $line;
	}elseif($x == 2 || $x == 3){
		return $line;
	}
}

}

class bumper extends template{

var $bumperTemplate;
//var $vars;

// Constructor
function bumper($bumperTemplate){

	/*
	 * Bumper Logic Here
	 * 
	 */
	$this->bumperTemplate 	= $bumperTemplate;
	//$this->vars				= $vars;

}

function redirect($url, $message, $delay, $classId){

	$PHP_SELF		=		basename($_SERVER['PHP_SELF']);

	$MESSAGE 		=		"<span id=\"".$classId."\">".$message."</span>
							<br><br>Redirecting...<br>
							If the page doesn't redirect automatically <a href=\"".$url."\">Click here</a>.";

	$LOCATION 		=		$url;
	$DURATION 		= 		$delay;
	$vars = "\$MESSAGE, \$LOCATION, \$DURATION, \$PHP_SELF";
	parent::display($this->bumperTemplate,$vars,1);

}

}

$template = new template();
$bumper = new bumper();

$bumper->redirect("index.php","You have successfully logged into your account.",3,"BumperCorrect");

 

 

The code should be self explanatory. Basically that "display" function under the template class is not getting the following variable values: $PHP_SELF, $MESSAGE, $LOCATION, $DURATION which are listed correct for the display function in the $vars variable.

 

The display function outputs the file, and erases all the {VAR_NAME_HERE} type variables located on the template.

 

Does any one have any suggestions? Or need me to explain it in a different way?

Link to comment
Share on other sites

This may or may not be the issue.  But it would certainly make it easier to follow what you're doing.  Try passing your list of variables as an array:

 

$vars['MESSAGE'] = "This is my message";

$vars['LOCATION']= $URL;

$vars['DURATION']=$duration;

$vars['PHP_SELF']=$php_self;

 

Then you don't have to worry about trying to explode the string out into an array.

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.