Php Switch/case From Files

Php Switch/case From Files
Let me explain what I am after…

Right now, I have 1 php file with sections of code in a switch / case structure.

I have a drop down where you choose the chart you want to “run”.

That selection sets the “action” for the switch statement and the code block resides in the case block terminated witn an exit();

I want to break out each block of code in the case and save it to a file say “some chart that does something.php.inc” and the files will reside in a dir say ./charts

I was also thinking of having a companion file of the same name but with either a “.xml” (If you think XML is the way to go) or “.conf” (If a simple text file will do)

So now I have the “container” php file with all the setup of libs etc and functions.
At runtime, I envision reading the “./charts” directory and read each “config” file for the needed information to later build a “dynamic” dropdown AND “include” the “.inc” files holding the code so when done, it works as if it was all in one file like it is now.

So if I have 25 charts in the directory, the dropdown will have 25 choices and the title and value came from the “conf” file and be able to execute the proper “case” as it did before.

Here is a “before” psuedo look at the single file method I have now:

<?php
/*==========================================================================*\
|| # ——————————————————————– # ||
\*==========================================================================*/

define(‘LOCATION’,’admin’); // <- Ignore this
require_once(‘./../../functions/config.php’); // <- Ignore this

// Auto-load all of our classes
include_once(‘config.inc.php’); // <- Ignore this

.
.
.
switch ($action)
{
case ’01’: // <- Value from “config file”
// ########## CHART 01 ####################################
// Right now, this holds the code to generate this “chart”
// I would like to “INCLUDE” this code here from external
// file and execute it.
break;

case ’02’: // <- Value from “config file”
// ########## CHART 02 ####################################
// Right now, this holds the code to generate this “chart”
// I would like to “INCLUDE” this code here from external
// file and execute it.
break;

.
.
.
}

// This is where we build the list of Charts

// Chart 01
$reportaction = ‘<select name=”action” style=”font-family: verdana”><option value=”01″‘; // <- Value from “config file”
if (isset($system->GPC[‘action’]) AND $system->GPC[‘action’] == ’01’) // <- Value from “config file”
{
$reportaction .= ‘ selected=”selected”‘;
}
$reportaction .= ‘>Chart01 </option>’; // <- Value from “config file”

// Chart 02
$reportaction .= ‘<option value=”02″‘; // <- Value from “config file”
if (isset($system->GPC[‘action’]) AND $system->GPC[‘action’] == ’02’) // <- Value from “config file”
{
$reportaction .= ‘ selected=”selected”‘;
}
$reportaction .= ‘>Chart02 </option>’; // <- Value from “config file”
// Chart 03
$reportaction .= ‘<option value=”03″‘; // <- Value from “config file”
if (isset($system->GPC[‘action’]) AND $system->GPC[‘action’] == ’03’) // <- Value from “config file”
{
$reportaction .= ‘ selected=”selected”‘;
}
$reportaction .= ‘>Chart03 </option>’; // <- Value from “config file”
.
.
.
.
// Chart 10
$reportaction .= ‘<option value=”10″‘; // <- Value from “config file”
if (isset($system->GPC[‘action’]) AND $system->GPC[‘action’] == ’10’) // <- Value from “config file”
{
$reportaction .= ‘ selected=”selected”‘;
}
$reportaction .= ‘>Chart10 </option>’; // <- Value from “config file”
$reportaction .= ‘>’.'</select>’;

// “$reportaction” holds the final dropdown”
// I need to build this dynamicaly from external “conf” files
// from “./charts” directory

// Various common functions follow below this point
.
.
.
?>

I hope this gives you a good idea of what I need to accomplish.

True PHP experts only please!

Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *