andybarr Posted April 23, 2008 Share Posted April 23, 2008 Hi all, A bit of a newbie, but I like to play. I wrote some code to work out points for a cricket game based on runs, wickets and overs. For the new season I need to allow a different scoring system for better teams, so I though I would use a checkbox to be ticked by the better clubs. So edited the form code to include the checkbox (code 1) included the posted variable into the Processform.php (code 2) and created a nested switch section based on the result from the checkbox. My problem is I cannot seem to find out what the value of the checkbox is, to check that the initial switch statement works. Please help Code 1 <p> BCL Points Calculator <br> <br> Please complete all the fields and press the Calculate button to find out your points </p> <Form action=\"modules.php?name=PointsCalc&file=processform\" method=\"POST\"> <br> Premier Criteria Team <input type=\"checkbox\" value=\"1\" name=\"premcriteria\"> (tick for yes) <br> Team Batting First: <br> <input type=\"text\" style=\"WIDTH: 30px\" maxlength=\"3\" name=\"b1runs\"> runs for <input type=\"text\" style=\"WIDTH: 30px\" maxlength=\"2\" name=\"b1wickets\"> wickets, in <input type=\"text\" style=\"WIDTH: 30px\" maxlength=\"4\" name=\"b1overs\"> overs. <br> Team Batting Second <br> <input type=\"text\" style=\"WIDTH: 30px\" maxlength=\"3\" name=\"b2runs\"> runs for <input type=\"text\" style=\"WIDTH: 30px\" maxlength=\"2\" name=\"b2wickets\"> wickets, in <input type=\"text\" style=\"WIDTH: 30px\" maxlength=\"4\" name=\"b2overs\"> overs. <input type=\"hidden\" name=\"test_input\" value=\"ok\"> <br> <br> <input type=\"submit\" value=\"Calculate\"> "; Code 2 <? // get data from form $premcriteria = $_POST['premcriteria']; $b1runs = $_POST['b1runs']; $b1wickets = $_POST['b1wickets']; $b1overs = $_POST['b1overs']; $b2runs = $_POST['b2runs']; $b2wickets = $_POST['b2wickets']; $b2overs = $_POST['b2overs']; $rain = $_POST['rain']; //echo ("<br>rain value is $rain"); // Calculations $b1runrate = $b1runs/$b1overs; $b2runrate = $b2runs/$b2overs; //echo ("<br>Side Batting first run rate is $b1runrate"); //echo ("<br> Side Batting second run rate is $b2runrate"); //Match Results // Team Batting First Switch ( true ) { case ($b1runs >= $b2runs) AND ($b1overs > $b2overs) AND ($b1runrate < $b2runrate) and ($b2wickets <> 10): $b1result = "Losing Draw"; $b1matchpoints = 0; break; case ($b1runs >= $b2runs) AND ($b1overs < $b2overs) AND ($b1runrate > $b2runrate) and ($b2wickets <> 10): $b1result = "Winning Draw"; $b1matchpoints = 0; break; case ($b1runs > $b2runs) AND ($b2wickets == 10) : $b1result = "Win"; $b1matchpoints = 25; break; case ($b1runs > $b2runs) AND ($b2wickets < 10) : $b1result = "Winning Draw"; $b2matchpoints = 0; break; case ($b1runs < $b2runs) : $b1result = "Lost"; $b1matchpoints = 0; break; case ($b1runs == $b2runs) AND ($b2wickets == 10) : $b1result = "Tied Match"; $b1matchpoints = 10; break; case ($b1runs == $b2runs) AND ($b1overs == $b2overs) : $b1result = "Equal Draw"; $b1matchpoints = 7; break; default: echo ("<br> Problem calculating result, Please check results!"); } // echo ("<br> Team Batting First $b1result"); // Team Batting Second Switch ( true ) { case ($b1runs >= $b2runs) AND ($b1overs > $b2overs) AND ($b1runrate < $b2runrate) and ($b2wickets <> 10): $b2result = "Winning Draw"; $b2matchpoints = 0; break; case ($b1runs >= $b2runs) AND ($b1overs < $b2overs) AND ($b1runrate > $b2runrate) and ($b2wickets <> 10): $b2result = "Losing Draw"; $b2matchpoints = 0; break; case ($b1runs > $b2runs) AND ($b2wickets == 10) : $b2result = "Lost"; $b2matchpoints = 0; break; case ($b1runs > $b2runs) AND ($b2wickets < 10) : $b2result = "Losing Draw"; $b2matchpoints = 0; break; case ($b1runs < $b2runs) : $b2result = "Win"; $b2matchpoints = 20; break; case ($b1runs == $b2runs) AND ($b2wickets == 10) : $b2result = "Tied Match"; $b2matchpoints = 10; break; case ($b1runs == $b2runs) AND ($b1overs == $b2overs) : $b2result = "Equal Draw"; $b2matchpoints = 7; break; default: echo ("<br> Problem calculating result, Please check results!"); } //echo ("<br> Team Batting Second $b2result"); //Batting Points switch ( true ) { Case $premcriteria = 0 : //Team Batting First (non Prem criteria) Switch ( true ) { case $b1runs >= 175 : $b1batpoints = 5; break; case $b1runs >= 150 : $b1batpoints = 4; break; case $b1runs >= 125 : $b1batpoints = 3; break; case $b1runs >= 100 : $b1batpoints = 2; break; case $b1runs >= 75 : $b1batpoints = 1; break; default: $b1batpoints = 0; break; } //echo ("<br> Side Batting First's Batting Points is $b1batpoints"); //Team Batting Second (non Prem criteria) Switch ( true ) { case $b2runs >= 175 : $b2batpoints = 5; break; case $b2runs >= 150 : $b2batpoints = 4; break; case $b2runs >= 125 : $b2batpoints = 3; break; case $b2runs >= 100 : $b2batpoints = 2; break; case $b2runs >= 75 : $b2batpoints = 1; break; default: $b2batpoints = 0; break; } //echo ("<br> Side Batting Second's Batting Points is $b2batpoints"); Break; Case $premcriteria = 1 : //Team Batting First (Prem criteria) Switch ( true ) { case $b1runs >= 200 : $b1batpoints = 5; break; case $b1runs >= 175 : $b1batpoints = 4; break; case $b1runs >= 150 : $b1batpoints = 3; break; case $b1runs >= 125 : $b1batpoints = 2; break; case $b1runs >= 100 : $b1batpoints = 1; break; default: $b1batpoints = 0; break; } //echo ("<br> Side Batting First's Batting Points is $b1batpoints"); //Team Batting Second (Prem criteria) Switch ( true ) { case $b2runs >= 200 : $b2batpoints = 5; break; case $b2runs >= 175 : $b2batpoints = 4; break; case $b2runs >= 150 : $b2batpoints = 3; break; case $b2runs >= 125 : $b2batpoints = 2; break; case $b2runs >= 100 : $b2batpoints = 1; break; default: $b2batpoints = 0; break; } //echo ("<br> Side Batting Second's Batting Points is $b2batpoints"); Break; } //Bowling Points //Team Batting First Switch ( true ) { case $b2wickets == 10 : $b1bowlpoints = 5; break; case $b2wickets >= 8 : $b1bowlpoints = 4; break; case $b2wickets >= 6 : $b1bowlpoints = 3; break; case $b2wickets >= 4 : $b1bowlpoints = 2; break; case $b2wickets >= 2 : $b1bowlpoints = 1; break; default: $b1bowlpoints = 0; break; } //echo ("<br> Side Batting First's Bowling Points is $b1bowlpoints"); //Team Batting Second Switch ( true ) { case $b1wickets == 10 : $b2bowlpoints = 5; break; case $b1wickets >= 8 : $b2bowlpoints = 4; break; case $b1wickets >= 6 : $b2bowlpoints = 3; break; case $b1wickets >= 4 : $b2bowlpoints = 2; break; case $b1wickets >= 2 : $b2bowlpoints = 1; break; default: $b2bowlpoints = 0; break; } //echo ("<br> Side Batting Second's Bowling Points is $b2bowlpoints"); // Calculate Draw methods Switch ( true ) { case $b1result == "Losing Draw" : $rundiff = ($b1runrate/$b2runrate)*100; // echo ("<br> 1. Runrate diff percentage is $rundiff"); break; case ($b2result == "Losing Draw" ) AND ($b1overs == $b2overs) : $rundiff = ($b2runs/$b1runs)*100; // echo ("<br> 2. Runs diff percentage is $rundiff"); break; case ($b2result == "Losing Draw" ) AND ($b1overs <> $b2overs) : $rundiff = ($b2runrate/$b1runrate)*100; // echo ("<br> 3. Runrate diff percentage is $rundiff"); break; default: $rundiff = 0; break; } Switch ( true ) { case $rundiff >= 95 : $ldrawpoints = 5; break; case $rundiff >= 90 : $ldrawpoints = 4; break; case $rundiff >= 85 : $ldrawpoints = 3; break; case $rundiff >= 80 : $ldrawpoints = 2; break; case $rundiff >= 75 : $ldrawpoints = 1; break; Default: $ldrawpoints = 0; break; } //echo ("<br> Losing draw points is $ldrawpoints"); // Winning Draw Points Switch ( true ) { case $b1result == "Winning Draw": $b1draw = 15 - $ldrawpoints; $b2draw = $ldrawpoints; // echo ("<br> Batting First Wining draw points $b1draw"); // echo ("<br> Batting Second Losing draw points $b2draw"); break; case $b2result == "Winning Draw": $b2draw = 15 - $ldrawpoints; $b1draw = $ldrawpoints; // echo ("<br> Batting Second Wining draw points $b2draw"); // echo ("<br> Batting First Losing draw points $b1draw"); break; default: break; } // Bonus Bowling Points switch ( true ) { case ($b1wickets == 10) AND ($b2result == "Win"): $b2bowlbonus = 5 - $b1bowlpoints; // echo ("<br> Bonus Bowling Points is $b2bowlbonus"); break; default: break; } // Total points // Team Batting First $b1TotalPoints = $b1bowlpoints + $b1batpoints + $b1matchpoints + $b1draw; // echo ("<br> Team batting First total points equals $b1TotalPoints"); // Team Batting Second $b2subTotalPoints = $b2bowlpoints + $b2batpoints + $b2matchpoints + $b2bowlbonus + $b2draw ; Switch ( true ) { case $b2subTotalPoints > 30: $b2TotalPoints = 30; $b2limited = "*"; $limittext = "* Points limited (30 points maximum for batting second)"; break; default: $b2TotalPoints = $b2subTotalPoints; $b2limited = ""; $limittext = ""; break; } Link to comment https://forums.phpfreaks.com/topic/102507-solved-nested-switch-statements/ Share on other sites More sharing options...
blackcell Posted April 23, 2008 Share Posted April 23, 2008 Are you displaying your variables after $_POST['name_of_checkbox'] to ensure the form works properly? Also, I have never tried to use a switch like this but usually I switch based on the variable. You switch () will declare what value will be checked in each case, I think. Link to comment https://forums.phpfreaks.com/topic/102507-solved-nested-switch-statements/#findComment-524842 Share on other sites More sharing options...
andybarr Posted April 23, 2008 Author Share Posted April 23, 2008 I tried displaying the variables and they all display apart from the checkbox one. I am hoping my switching working ? Initial switch check should check to see if the check box is ticked so the variable should be the checkbox value. Then the 2nd switch should produce the points based on the scores. A working example (with out the checkbox) can be seen here http://www.berkshirecricketleague.com/modules.php?name=PointsCalc Thanks Andy Link to comment https://forums.phpfreaks.com/topic/102507-solved-nested-switch-statements/#findComment-524899 Share on other sites More sharing options...
DarkWater Posted April 23, 2008 Share Posted April 23, 2008 Use well-written if statements, not switch statements here. You can't do this (I don't think you can..at least): Switch ( true ) { case ($b1runs >= $b2runs) AND ($b1overs > $b2overs) AND ($b1runrate < $b2runrate) and ($b2wickets <> 10): $b1result = "Losing Draw"; ............... Because switch checks the value of the variable it receives (true is not a variable) against the values of the case statements...it doesn't check if the case statements are logically true. Use if statements. Link to comment https://forums.phpfreaks.com/topic/102507-solved-nested-switch-statements/#findComment-524902 Share on other sites More sharing options...
blackcell Posted April 24, 2008 Share Posted April 24, 2008 I was thinking what Darkwater stated. The switch statements don't act right if you utilize them the way you are. The same can be attained by well organized if statements and they may be easier manage(visually). Link to comment https://forums.phpfreaks.com/topic/102507-solved-nested-switch-statements/#findComment-525584 Share on other sites More sharing options...
andybarr Posted April 24, 2008 Author Share Posted April 24, 2008 Currently the switch statements are working, so could I use the if statement to check the status of the checkbox then use the switch statement to do the calculation? I.E. If checkbox = <value> then Switch ( true ) { case ($b1runs >= $b2runs) AND ($b1overs > $b2overs) AND ($b1runrate < $b2runrate) and ($b2wickets <> 10): $b1result = "Losing Draw"; ... Else Switch ( true ) ... other code for opposite <value> for checkbox If that makes any sense? Andy Link to comment https://forums.phpfreaks.com/topic/102507-solved-nested-switch-statements/#findComment-525803 Share on other sites More sharing options...
andybarr Posted April 28, 2008 Author Share Posted April 28, 2008 I got it sorted thanks for all your help, here is the final code to help any others <p> BCL Points Calculator <br> <br> Please complete all the fields and press the Calculate button to find out your points </p> <Form action=\"modules.php?name=PointsCalc&file=processform\" method=\"POST\"> <br> Premier Criteria Team <input type=\"checkbox\" value=\"1\" name=\"premcriteria\"> (tick for yes) <br> Team Batting First: <br> <input type=\"text\" style=\"WIDTH: 30px\" maxlength=\"3\" name=\"b1runs\"> runs for <input type=\"text\" style=\"WIDTH: 30px\" maxlength=\"2\" name=\"b1wickets\"> wickets, in <input type=\"text\" style=\"WIDTH: 30px\" maxlength=\"4\" name=\"b1overs\"> overs. <br> Team Batting Second <br> <input type=\"text\" style=\"WIDTH: 30px\" maxlength=\"3\" name=\"b2runs\"> runs for <input type=\"text\" style=\"WIDTH: 30px\" maxlength=\"2\" name=\"b2wickets\"> wickets, in <input type=\"text\" style=\"WIDTH: 30px\" maxlength=\"4\" name=\"b2overs\"> overs. <input type=\"hidden\" name=\"test_input\" value=\"ok\"> <br> <br> <input type=\"submit\" value=\"Calculate\"> "; and the processform <? // get data from form if (isset($_POST['premcriteria'])) $premcriteria = $_POST['premcriteria']; else $premcriteria = "0"; $b1runs = $_POST['b1runs']; $b1wickets = $_POST['b1wickets']; $b1overs = $_POST['b1overs']; $b2runs = $_POST['b2runs']; $b2wickets = $_POST['b2wickets']; $b2overs = $_POST['b2overs']; $rain = $_POST['rain']; //echo ("<br>rain value is $rain"); // Calculations $b1runrate = $b1runs/$b1overs; $b2runrate = $b2runs/$b2overs; //echo ("<br>Side Batting first run rate is $b1runrate"); //echo ("<br> Side Batting second run rate is $b2runrate"); //Match Results // Team Batting First Switch ( true ) { case ($b1runs >= $b2runs) AND ($b1overs > $b2overs) AND ($b1runrate < $b2runrate) and ($b2wickets <> 10): $b1result = "Losing Draw"; $b1matchpoints = 0; break; case ($b1runs >= $b2runs) AND ($b1overs < $b2overs) AND ($b1runrate > $b2runrate) and ($b2wickets <> 10): $b1result = "Winning Draw"; $b1matchpoints = 0; break; case ($b1runs > $b2runs) AND ($b2wickets == 10) : $b1result = "Win"; $b1matchpoints = 25; break; case ($b1runs > $b2runs) AND ($b2wickets < 10) : $b1result = "Winning Draw"; $b2matchpoints = 0; break; case ($b1runs < $b2runs) : $b1result = "Lost"; $b1matchpoints = 0; break; case ($b1runs == $b2runs) AND ($b2wickets == 10) : $b1result = "Tied Match"; $b1matchpoints = 10; break; case ($b1runs == $b2runs) AND ($b1overs == $b2overs) : $b1result = "Equal Draw"; $b1matchpoints = 7; break; default: echo ("<br> Problem calculating result, Please check results!"); } // echo ("<br> Team Batting First $b1result"); // Team Batting Second Switch ( true ) { case ($b1runs >= $b2runs) AND ($b1overs > $b2overs) AND ($b1runrate < $b2runrate) and ($b2wickets <> 10): $b2result = "Winning Draw"; $b2matchpoints = 0; break; case ($b1runs >= $b2runs) AND ($b1overs < $b2overs) AND ($b1runrate > $b2runrate) and ($b2wickets <> 10): $b2result = "Losing Draw"; $b2matchpoints = 0; break; case ($b1runs > $b2runs) AND ($b2wickets == 10) : $b2result = "Lost"; $b2matchpoints = 0; break; case ($b1runs > $b2runs) AND ($b2wickets < 10) : $b2result = "Losing Draw"; $b2matchpoints = 0; break; case ($b1runs < $b2runs) : $b2result = "Win"; $b2matchpoints = 20; break; case ($b1runs == $b2runs) AND ($b2wickets == 10) : $b2result = "Tied Match"; $b2matchpoints = 10; break; case ($b1runs == $b2runs) AND ($b1overs == $b2overs) : $b2result = "Equal Draw"; $b2matchpoints = 7; break; default: echo ("<br> Problem calculating result, Please check results!"); } //echo ("<br> Team Batting Second $b2result"); //Batting Points if ($premcriteria == 0) //Team Batting First (non Prem criteria) { Switch ( true ) { case $b1runs >= 175 : $b1batpoints = 5; break; case $b1runs >= 150 : $b1batpoints = 4; break; case $b1runs >= 125 : $b1batpoints = 3; break; case $b1runs >= 100 : $b1batpoints = 2; break; case $b1runs >= 75 : $b1batpoints = 1; break; default: $b1batpoints = 0; break; } // hctiws //echo ("<br> Side Batting First's Batting Points is $b1batpoints"); //Team Batting Second (non Prem criteria) Switch ( true ) { case $b2runs >= 175 : $b2batpoints = 5; break; case $b2runs >= 150 : $b2batpoints = 4; break; case $b2runs >= 125 : $b2batpoints = 3; break; case $b2runs >= 100 : $b2batpoints = 2; break; case $b2runs >= 75 : $b2batpoints = 1; break; default: $b2batpoints = 0; break; } // hctiws //echo ("<br> Side Batting Second's Batting Points is $b2batpoints"); } // fi else { //Team Batting First (Prem criteria) Switch ( true ) { case $b1runs >= 200 : $b1batpoints = 5; break; case $b1runs >= 175 : $b1batpoints = 4; break; case $b1runs >= 150 : $b1batpoints = 3; break; case $b1runs >= 125 : $b1batpoints = 2; break; case $b1runs >= 100 : $b1batpoints = 1; break; default: $b1batpoints = 0; break; } // hctiws //echo ("<br> Side Batting First's Batting Points is $b1batpoints"); //Team Batting Second (Prem criteria) Switch ( true ) { case $b2runs >= 200 : $b2batpoints = 5; break; case $b2runs >= 175 : $b2batpoints = 4; break; case $b2runs >= 150 : $b2batpoints = 3; break; case $b2runs >= 125 : $b2batpoints = 2; break; case $b2runs >= 100 : $b2batpoints = 1; break; default: $b2batpoints = 0; break; } // hctiws } // esle I hope this is some help to people in the future. Andy Link to comment https://forums.phpfreaks.com/topic/102507-solved-nested-switch-statements/#findComment-528918 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.