Jump to content

Trying New Approach With Classes And Methods


ShoeLace1291

Recommended Posts

So, I'm starting a new project and thought I would try a new approach at loading files and classes to process output.  I think my logic is correct, but for some reason I'm getting a blank page.  Basically instead of having seperate pages to output different functions and such, I store everything in one page.  It's the same deal as the switch case approach, but it uses a foreach loop to cycle through the actions, classes, and functions which are stored in an array.

 

Here's my index.php file:

<?php

require('config.php');
include('languages/english.php');
include('classes/Template.php');

$template = new Template;

$viewPages = array( 
'category' => array('classfile' => 'Category.php', 'classname' => 'category', 'functions' => array('create', 'delete', 'modify', 'merge')),
'questions' => array('classfile' => 'Question.php', 'classname' => 'Question', 'functions' => array('create', 'delete', 'modify', 'votegood', 'votebad'))
);


$currentPage = $_REQUEST['action'];	

	foreach($viewPages as $action => $settings){

		if($currentPage == $action){
			require(INCLUDE_ROOT.'/classes/'.$settings['classfile']);

			$class = new $settings['classname'];
			$function = $_REQUEST['do'];
			$class->$function();

			$template->loadTemplate($settings['classname']->viewFile, $vars = array());
			if($template->message == FALSE){
				die($template->message);
			}

		}

	}

?>	

 

Here's the Template class file:

<?php

class Template {

var $file;
var $vars;
var $message;

function loadTemplate($file, $vars){

	if(empty($file) || empty($vars)){
		$this->message = LANG_ERR_7;
	} else if($file = file_get_contents(INCLUDE_ROOT.'/'.$file)){
		$this->message = LANG_ERR_7;
	} else {
		foreach($vars as $key => $val)
			$file = str_replace('{'.$key.'}', $val, $file);
		}

	$this->message = FALSE;
	return $file;

}

}

?>

 

I tried visiting the category action(index.php?action=category;do=create).  This is the classfile for that

<?php

class category {

var $id;
var $title;
var $description;
var $uri;
var $message = array();
var $vars = array();
var $viewFile = '';

function create(){

	if($_POST['submit']){

	//Review the user input and make ure everything is ok
	$messages = array();
	if($this->title == FALSE){
		$messages['title'] = LANG_ERR_1;
	} else if(strlen($this->title) > 30 || strlen($this->title < 5)){
		$messages['title'] = LANG_ERR_2;
	} else if($this->description == FALSE){
		$messages['description'] = LANG_ERR_3;
	} else if(strlen($this->description) > 400 || strlen($this->description) < 10){
		$messages['description'] = LANG_ERR_4;
	} else {
		$messages = FALSE;
	}

	if($messages != FALSE){
		$this->messages = $messages;
		$this->viewFile = INCLUDE_ROOT.'/template/index_body.tpl';
	} else {
		$this->title = htmlentities($this->title);
		$this->title = stripslashes($this->title);
		$this->title = htmlspecialchars($this->title);

		$this->description = htmlentities($this->description);
		$this->description = htmlspecialchars($this->description);
		$this->description = nl2br($this->description);

		$query = "
			INSERT INTO categories
				(c_title, c_desc)
			VALUES('".$this->title."', '".$this->desscription."')";

		if(mysql_query($query)){
			$this->message['succes'] = LANG_ERR_5;
		} else {
			$this->message['fail'] = LANG_ERR_6;
		}

	$this->viewFile = INCLUDE_ROOT.'/template/message_body.tpl';

	}

	} else {

		$this->viewFile = INCLUDE_ROOT.'/template/create_category.tpl';

	}

}

}

?>

 

Now if you look at the Category class, the template file it sets is the "category_create.tpl" file, which I haven't created yet.  I'm expecting there to be an error associated with the template class and the file_get_contents function that calls the file, but like I said, I'm just getting a blank page.

 

I've never tried this approach before, so I have no idea what's causing this.  Any help would be greatly appreciated.

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.