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

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.