[nycphp-talk] State table implementation ideas

Glenn glenn310b at
Sat Feb 4 16:46:04 EST 2006

On Feb 3, 2006, at 1:03 PM, Cliff Hirsch wrote:

> I need to generate an "options list" that is based on several status 
> fields. Sort of like, "if you can rub your belly, scratch your head 
> and stand on one foot, you can proceed past the breathalyzer test, 
> join the circus or run for governor of Massachusetts.
> What started as a simple if statement has grown into multiple switch 
> statements with some if statements thrown in just to make things 
> completely confusing. While this "compresses"  a state diagram, its 
> getting unwieldy.
> So now I'm thinking of throwing the whole thing into a lookup table.
> Field A   Field B ... Field N        Options
> While this will result in a large array and many duplicate rows (since 
> many of the possibilities are actually the same), it should be really 
> easy to maintain and a great visual aid for those unlucky souls that 
> inherit my code.
> Any other  ideas? Or is a lookup table the best solution?

Fun problem Cliff...

I wrote this program to explore the possibilities...

Depending on the complexity of your situation, something like this 
might work.

We're setting up parameters and turning the options on and off till 
we've got a list of valid options.

This example is not very complex, but the concept could be expanded.

The trigger arrays could get more complex, and you could map actual 
field values to a 'y' or 'n' condition.

Two additional trigger arrays could be added, for cases when fields 
return false values

Glenn Powell



// example values for testing
$field_vals['field_a'] = 'y';
$field_vals['field_b'] = 'n';
$field_vals['field_c'] = 'y';
$field_vals['field_d'] = 'y';

// a list of all the fieldnames

$field_list = array('field_a','field_b','field_c','field_d');

// a list of all the options

$options = array('option_a','option_b','option_c');

// begin options maintenence area
// control options use here
// $trigger_include_option: if any of these fields are true,
// INclude the option on the options list
// $trigger_exclude_option: if any of these fields are true,
// EXclude the option from the options list

$trigger_include_option['option_a'] = 
$trigger_exclude_option['option_a'] = array('field_d');

$trigger_include_option['option_b'] = array('field_a','field_c');
$trigger_exclude_option['option_b'] = array('field_b');

$trigger_include_option['option_c'] = array('field_d');
$trigger_exclude_option['option_c'] = array('field_c');

// end of options maintenence area

// loop on the multidimensional array and load up two more arrays
// to tell us what to include and exclude

foreach ($trigger_include_option as $option=>$field_list) {

    $use_option[$option] = 'n';

    foreach ($field_list as $field) {
       if ($field_vals[$field] == 'y') {
          $use_option[$option] = 'y';


foreach ($trigger_exclude_option as $option=>$field_list) {

    foreach ($field_list as $field) {
       if ($field_vals[$field] == 'y') {
          $use_option[$option] = 'n';


// now show the available options

print "<ul>";

foreach($options as $option) {

    if ($use_option[$option] == 'y') {
       print "<li>$option</li>\n";


print "</ul>";

// cut and paste for debugging


<p>Program Execution Completed</p>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: text/enriched
Size: 3971 bytes
Desc: not available
URL: <>

More information about the talk mailing list