Jump to content

Using Switch With Array of Cases


ShoeLace1291

Recommended Posts

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

<?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
}
?> 

Archived

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

×
×
  • 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.