[nycphp-talk] State table implementation ideas
cliff
cliff at pinestream.com
Sun Feb 5 09:52:26 EST 2006
Glenn:
Very cool solution.
I tried writing a couple of simple cases. Here's what I came up with:
This one is very clean, but it breaks up the search key and options into two
tables. This provides a lot of flexibility, but is a bit hard on the eyes --
easy to make a mistake in the options table.
$lookup = array(
array('pending','pending','pending'),
array('pending','pending','inprocess'),
array('pending','pending','completed'),
array('pending','inprocess','inprocess'),
array('pending','completed','inprocess'));
$options = array('dothis',
'dothat',
'dotheotherthing',
'dothis',
'anotheroption');
$currentstate = array('pending','pending','completed');
$key = array_search($state, $lookup);
if ($key === false)
echo 'not in lookup table';
else
echo $options[$key];
This one combines the "needle" with the resultant process . Very easy to
follow and maintain, but the computational load for the implodes concerns
me. I guess some sort of precompile could be implemented.
$lookup2 = array(
implode(array('pending','pending','pending')) => 'do this',
implode(array('pending','pending','inprocess')) => 'do that',
implode(array('pending','pending','completed')) => 'do something',
implode(array('pending','inprocess','inprocess')) => 'do another',
implode(array('pending','completed','inprocess')) => 'do whatever');
$state2 = implode(array('pending','pending','completed'));
if (array_key_exists($state2, $lookup2))
echo $lookup2[$state2];
else
echo 'not in lookup table';
More information about the talk
mailing list