473,320 Members | 1,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

two-dimensional switch statement?

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.

Aug 22 '07 #1
2 1827
..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
Aug 22 '07 #2
Miyagi wrote:
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
Aug 22 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: SimonC | last post by:
I'm looking to do something similar to a feature found on Ticketmaster.com, where you select your seats at a venue, and then you have two minutes in which to take or leave them. QUESTION 1a....
8
by: John Grenier | last post by:
Hi, I have to determine the "standing" (WIN - TIE - LOSS) from confrontations between two teams on a contest. The table matchResults has fields cont_id, team_id and contest_result (int). ...
6
by: Willem | last post by:
Hi, I have a newbie question: is it possible to make a search form in asp that searches in two different databases (access)? Willem
6
by: Matt K. | last post by:
Hi there, I have a form in an Access project that contains a subform which displays the results of a query of the style "select * from where = #a certain date#". In the main part of the form...
7
by: Prabhudhas Peter | last post by:
I have two object instances of a same class... and i assigned values in both object instances (or the values can be taken from databse and assigned to the members of the objects)... Now i want to...
0
by: clintonG | last post by:
I applied aspnet_regsql to SQL2K which was working fine throughout Beta 2 development. After installing Visual Studio and SQL Express RTM my application has blown up. Logging in to the application...
9
by: Steven | last post by:
Hello, I have a question about strcmp(). I have four words, who need to be compared if it were two strings. I tried adding the comparison values like '(strcmp(w1, w2) + strcmp(w3, w4))', where...
9
by: dhable | last post by:
I just started working with Python and ran into an annoyance. Is there a way to avoid having to use the "from xxx import yyy" syntax from files in the same directory? I'm sure it's been asked a...
13
by: paul.joseph.davis | last post by:
Hi, I've just had my first encounter with two-phase lookup and I'm scratching my head a bit. The idea behind two phase look up is pretty easy to understand, but I have a case that fails to...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.