Connecting Tech Pros Worldwide Help | Site Map

two-dimensional switch statement?

  #1  
Old August 22nd, 2007, 07:35 PM
Miyagi
Guest
 
Posts: n/a
I have a bunch of conditions in which to determine different types of
questions to ask in a survey I am constructing.
Just was wondering if anybody has used a two-dimensional switch
statement. If this works, is this an efficient coding method or would
anybody else have another way around this?

$type = enters _POST as (employee, client, or peer)
$visit = enters _POST as ( 0 or 1 )
$userage = enters _POST as ( 1, 2, 3, or 4)
$ip is the IP address of the user

function determineUser($type, $userage, $ip, $visit)
{

switch ($type){
case 'employee':
switch ($userage){
case '1':
case '2': if($visit){$surveyType=1;}else{$surveyType=2;} break;
case '3':
case '4': $surveyType=3; break;
case 'client': if($visit){$surveyType=4;}else{$surveyType=5;}
break;
case 'peer': if($visit){$surveyType=6;}else{$surveyType=7;} break;
}

$query = "INSERT INTO user(userip, userage, idsurvey)
VALUES('$ip','$userage','$surveyType')";
mysql_query($query);

return (getUserId($ip));
}

Thanks.

  #2  
Old August 22nd, 2007, 08:05 PM
Michael Fesser
Guest
 
Posts: n/a

re: two-dimensional switch statement?


..oO(Miyagi)
Quote:
>I have a bunch of conditions in which to determine different types of
>questions to ask in a survey I am constructing.
>Just was wondering if anybody has used a two-dimensional switch
>statement.
Not yet.
Quote:
>If this works, is this an efficient coding method or would
>anybody else have another way around this?
Should work. Just some notes:

* I would use the ternary operator instead of the if-statements to keep
the code a bit shorter (and more readable for me).
* You should initialize the $surveyType variable, just in case a switch
statement will fail.
* There was a brace and a "break" missing after the nested switch.
* Don't quote numeric values in a switch statement or in a query.
* Don't forget to validate the $ip and $userage before passing them to
the function. They are used in a database query. If you don't make
sure they only contain allowed values, this may lead to an SQL
injection hole.
You could also use PDO with prepared statements to avoid this risk.

Your function slightly modified:

function determineUser($type, $userage, $ip, $visit) {
$surveyType = 0;
switch ($type) {
case 'employee':
switch ($userage) {
case 1:
case 2: $surveyType = $visit ? 1 : 2; break;
case 3:
case 4: $surveyType = 3;
}
break;
case 'client': $surveyType = $visit ? 4 : 5; break;
case 'peer': $surveyType = $visit ? 6 : 7;
}
if ($surveyType != 0) {
$query = "
INSERT INTO user (userip, userage, idsurvey)
VALUES ('$ip', $userage, $surveyType)";
mysql_query($query);
return getUserId($ip);
} else {
// something went wrong
}
}

HTH
Micha
  #3  
Old August 22nd, 2007, 08:45 PM
gosha bine
Guest
 
Posts: n/a

re: two-dimensional switch statement?


Miyagi wrote:
Quote:
I have a bunch of conditions in which to determine different types of
questions to ask in a survey I am constructing.
Just was wondering if anybody has used a two-dimensional switch
statement. If this works, is this an efficient coding method or would
anybody else have another way around this?
>
$type = enters _POST as (employee, client, or peer)
$visit = enters _POST as ( 0 or 1 )
$userage = enters _POST as ( 1, 2, 3, or 4)
$ip is the IP address of the user
>
function determineUser($type, $userage, $ip, $visit)
{
>
switch ($type){
case 'employee':
switch ($userage){
case '1':
case '2': if($visit){$surveyType=1;}else{$surveyType=2;} break;
case '3':
case '4': $surveyType=3; break;
case 'client': if($visit){$surveyType=4;}else{$surveyType=5;}
break;
case 'peer': if($visit){$surveyType=6;}else{$surveyType=7;} break;
}
>
$query = "INSERT INTO user(userip, userage, idsurvey)
VALUES('$ip','$userage','$surveyType')";
mysql_query($query);
>
return (getUserId($ip));
}
>
Thanks.
>
I think using an explicit configuration / lookup table would make the
code far more maintainable. Something along the lines of


function get_survey_type($type, $age, $visit)
{
// lookup key is either 'type-age-visit'
// or 'type-visit' (assumes any age)

static $map = array(
'employee-1-1' =1,
'employee-2-1' =1,
//
'client-1' =4,
'client-0' =5,
);

$key = "$type-$age-$visit";
if(isset($map[$key]))
return $map[$key];

$key = "$type-$visit";
if(isset($map[$key]))
return $map[$key];

return default or error;

}



--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
switch-statement ??? Chris answers 14 November 16th, 2005 01:10 PM
Which of switch statement and if-else statement takes less time to execute? swaroophr@gmail.com answers 18 November 15th, 2005 01:30 AM
Regarding Switch statement prafulla answers 17 November 14th, 2005 03:51 AM
case/switch statement? Joe Stevenson answers 26 July 19th, 2005 03:27 AM
Switch statement (was: Lambda going out of fashion) Skip Montanaro answers 2 July 18th, 2005 08:46 PM