Building an *efficient* if/then interface for non-technical users to build flow-control in PHP
Posted
by Brendan
on Stack Overflow
See other posts from Stack Overflow
or by Brendan
Published on 2010-04-12T18:50:02Z
Indexed on
2010/04/12
18:52 UTC
Read the original article
Hit count: 374
I am currently building an internal tool to be used by our management to control the flow of traffic. I have built an if/then interface allowing the user to set conditions for certain outcomes, however it is inefficient to use the switch statement to control the flow. How can I improve the efficiency of my code? Example of code:
if($previous['route_id'] == $condition['route_id'] && $failed == 0) //if we have not moved on to a new set of rules and we haven't failed yet
{
switch($condition['type'])
{
case 0 :
$type = $user['hour'];
break;
case 1 :
$type = $user['location']['region_abv'];
break;
case 2 :
$type = $user['referrer_domain'];
break;
case 3 :
$type = $user['affiliate'];
break;
case 4 :
$type = $user['location']['country_code'];
break;
case 5 :
$type = $user['location']['city'];
break;
}
$type = strtolower($type);
$condition['value'] = strtolower($condition['value']);
switch($condition['operator'])
{
case 0 :
if($type == $condition['value']);
else $failed = '1';
break;
case 1 :
if($type != $condition['value']);
else $failed = '1';
break;
case 2 :
if($type > $condition['value']);
else $failed = '1';
break;
case 3 :
if($type >= $condition['value']);
else $failed = '1';
break;
case 4 :
if($type < $condition['value']);
else $failed = '1';
break;
case 5 :
if($type <= $condition['value']);
else $failed = '1';
break;
}
}
© Stack Overflow or respective owner