ShoeLace1291 Posted November 8, 2010 Share Posted November 8, 2010 I've been looking for a faster(both to code and to load) so I tried storing all of my cases in an array. I want to use a foreach statement to load all the information for the appropriate case which includes classfile, class name, and methods within that class. I guess you can't use a foreach loop inside of a switch function. Does anyone know of any easier way of doing something like this? <?php require('config.php'); require(INCLUDE_ROOT.'/classes/Category.php'); $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 = $_GET['action']; switch($currentPage){ foreach($viewPages as $action => $settings){ case $action: require(INCLUDE_ROOT.'/classes/'.$ettings['classfile']); $this->$class = new $settings['classname']; $function = $_REQUEST['do']; $this->$class->$function(); loadTemplate($this->viewFile, $this->messages); break; } ?> Link to comment https://forums.phpfreaks.com/topic/218082-using-switch-with-array-of-cases/ Share on other sites More sharing options...
sasa Posted November 8, 2010 Share Posted November 8, 2010 <?php require('config.php'); require(INCLUDE_ROOT.'/classes/Category.php'); $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 = $_GET['action']; if(array_key_exist($currentPage, $viewPages)) { $settings = $viewPages[$currentPage]; require(INCLUDE_ROOT.'/classes/'.$ettings['classfile']); $this->$class = new $settings['classname']; $function = $_REQUEST['do']; $this->$class->$function(); loadTemplate($this->viewFile, $this->messages); } else { //show deafult page } ?> Link to comment https://forums.phpfreaks.com/topic/218082-using-switch-with-array-of-cases/#findComment-1131749 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.