You are here: Re: two-dimensional switch statement? « PHP Programming Language « IT news, forums, messages
Re: two-dimensional switch statement?

Posted by Michael Fesser on 08/22/07 19:00

..oO(Miyagi)

>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.

>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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация