darkfreaks Posted April 28, 2009 Share Posted April 28, 2009 is this possible ??? i have something like: <?php if($_POST['variable']==""){ $error['variable']="this variable is empty"} else{ unset($error['variable']);}?> i have made a switch statement that does the same thing pretty much but im unsure how to unset the errors Quote Link to comment https://forums.phpfreaks.com/topic/156030-solved-unsetting-arrayed-errors-in-switch-statements/ Share on other sites More sharing options...
premiso Posted April 28, 2009 Share Posted April 28, 2009 $error = array(); // start with a clean bill of health if (empty($_POST['variable'])) { $error['variable'] = "This variable was empty."; if (count($error) > 1) { echo "You had errors which were: <br />"; echo implode("<br />", $error); }else { echo "No errors, woohoo!"; } No need to unset it, if you do not set it in the first place. Quote Link to comment https://forums.phpfreaks.com/topic/156030-solved-unsetting-arrayed-errors-in-switch-statements/#findComment-821416 Share on other sites More sharing options...
darkfreaks Posted April 28, 2009 Author Share Posted April 28, 2009 so pretty much what i was doing was useless because i wasnt arraying it ??? Quote Link to comment https://forums.phpfreaks.com/topic/156030-solved-unsetting-arrayed-errors-in-switch-statements/#findComment-821417 Share on other sites More sharing options...
premiso Posted April 28, 2009 Share Posted April 28, 2009 so pretty much what i was doing was useless because i wasnt arraying it ??? Yes. It probably would have thrown an Undefined index notice, to be honest. Quote Link to comment https://forums.phpfreaks.com/topic/156030-solved-unsetting-arrayed-errors-in-switch-statements/#findComment-821419 Share on other sites More sharing options...
darkfreaks Posted April 28, 2009 Author Share Posted April 28, 2009 its not working right. it doesnt display the form just says my outpt is zero and that i am healthy. <?php function is_int_val($data) { if (is_int($data) === true) return true; elseif (is_string($data) === true && is_numeric($data) === true) { return (strpos($data, '.') === false); } return false; } $height= trim($_POST['height']); $weight=trim($_POST['weight']); $heighttwo= $height*$height; @$total=$weight/$heighttwo; $newtotal= $total*703; $output = sprintf('%01.2f',$newtotal); switch($_POST['height']){ case($_POST['height']==""||empty($_POST['height'])): $error['height']="<font color=red>Please enter a height!</font>"; break; case(!is_numeric($_POST['height'])): $error['height']="<font color=red>Only Numbers Allowed!</font>"; case(!is_int_val($_POST['height'])): $error['height']="<font color=red>Only Numbers Allowed!</font>"; } switch($_POST['weight']){ case($_POST['weight']==""||empty($_POST['weight'])): $error['weight']="<font color=red>Please enter a height!</font>"; break; case(!is_numeric($_POST['weight'])): $error['weight']="<font color=red>Only Numbers Allowed!</font>"; case(!is_int_val($_POST['weight'])): $error['weight']="<font color=red>Only Numbers Allowed!</font>"; } if($error){?> <form method="POST"> <b>Height(inches):</b><input name="height" type="text" size="3"><?php if ($_POST) { echo $error['height'];} ?><b>Weight(lbs.):</b><input name="weight" type="text" size="3"><?php if ($_POST) { echo $error['weight'];} ?> <input type="submit" name="Calculate!" value="Calculate!"> </form> <?php }else{ switch($newtotal) { case ($newtotal < 18): $msg = "<font color=red>You're Underweight! Youre BMI is:$output%</font>"; break; case ($newtotal >= 18 && $newtotal < 24.9): $msg = "<font color=green>You're Healthy! Youre BMI is:$output%</font>"; break; case ($newtotal >= 24.9 && $newtotal < 29.9): $msg = "<font color=orange>You're Overweight! Youre BMI is:$output%</font>"; break; default: $msg = "<font color=red>You Are Obese Please seek Immediate Medical Help!</font>"; break; } echo $msg; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/156030-solved-unsetting-arrayed-errors-in-switch-statements/#findComment-821425 Share on other sites More sharing options...
premiso Posted April 28, 2009 Share Posted April 28, 2009 switch. I do not believe you can use conditional cases like you are. Use IF/Else If/Else instead. If you can do that, then yea. Why not just use an if/else if/else statement. It would be much less code and easier to read, in my opinion. Quote Link to comment https://forums.phpfreaks.com/topic/156030-solved-unsetting-arrayed-errors-in-switch-statements/#findComment-821427 Share on other sites More sharing options...
darkfreaks Posted April 28, 2009 Author Share Posted April 28, 2009 you can too use conditional statements with cases Quote Link to comment https://forums.phpfreaks.com/topic/156030-solved-unsetting-arrayed-errors-in-switch-statements/#findComment-821429 Share on other sites More sharing options...
premiso Posted April 28, 2009 Share Posted April 28, 2009 you can too use conditional statements with cases But why. It makes more code for you to write. I do not see the purpose of using switch case in this scenario. It just does not make sense to me, as it is much more code than if/else if/else would take. EDIT: As to your problem: <?php if ($_POST) { echo $error['height'];} ?><b>Weight(lbs.):</b><input name="weight" type="text" size="3"><?php if ($_POST) { echo $error['weight'];} ?> You are checking if $_POST and echoing an error? Why ??? how does $_POST = an error? This is probably what you were intending to do: <?php echo isset($error['height'])?$error['height']:''; ?> You should be checking if the error index has been set, not if $_POST as that will execute no matter what if the form has been submitted. EDIT: EDIT: Also: if($error){?> Why do you not use proper checking conditions in the if statement? if (count($error) > 1 || !isset($_POST['Calculate!'])){?> That way, only display the form if there have been errors recorded to the array. And you will know that the condition will not fluke out just because $error has been set. EDIT: Added the !isset to the calculate. I would suggest not using non-alpha numeric characters in your form item names. This way, if there were no errors or if the form has not been submitted then show the form, else show the msg. Quote Link to comment https://forums.phpfreaks.com/topic/156030-solved-unsetting-arrayed-errors-in-switch-statements/#findComment-821430 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.