mittu1 Posted June 21, 2010 Share Posted June 21, 2010 Hi All, Hopefully someone can point me in the right direction with this. I am new to PHP and am probably doing something obvious wrong so any help is much appreciated. This is for an eye prescription. I'm trying to fill a select statement with a while loop starting from -4 going to +4 in 0.25 increments. However at 0 I want the option to be Plano, followed by Balance and then Infinity before starting again at +0.25 on to +4. It all seems to work but the dropdown selection at Balance is followed by an option float(0.25) and then the Infinity option is followed by a float(0.5). I have no idea where these values are coming from. Please see the attached picture to see what I mean. Here is the code <select name="cstLeftcyl" type="text"> <?php $i=-4; while($i<=4.5) { if ($i == $cstLeftcyl) { ?> <option value="<?php echo ($i);?>" selected="selected"><?php switch ($i) { case 0: echo ("Plano"); ?></option><?php break; case 0.25: echo ("Balance"); ?><option><?php break; case 0.5: echo ("Infinity"); ?><option><?php break; case ($i<0): echo ($i); ?></option><?php break; default: echo ("+" . ($i-0.5)); ?></option><?php } } else { ?> <option value="<?php echo ($i);?>"><?php switch ($i) { case 0: echo ("Plano"); ?></option><?php break; case 0.25: echo ("Balance"); ?><option><?php break; case 0.5: echo ("Infinity"); ?><option><?php break; case ($i<0): echo ($i); ?></option><?php break; default: echo ("+" . ($i-0.5)); ?></option><?php } } $i=$i+0.25; } ?> </select> [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/205368-switch-statement-problem/ Share on other sites More sharing options...
phpchamps Posted June 21, 2010 Share Posted June 21, 2010 i am not sure what exactly you want but try replace your code with the below <select name="cstLeftcyl" type="text"> <?php $i=-4; while($i<=4.5){ if ($i == $cstLeftcyl) { ?> <option value="<?php echo ($i);?>" selected="selected"><?php }else{ ?> <option value="<?php echo ($i);?>"><?php } switch ($i) { case 0: echo ("Plano"); ?></option><?php break; case 0.25: echo ("Balance"); ?><option><?php break; case 0.5: echo ("Infinity"); ?><option><?php break; case ($i<0): echo ($i); ?></option><?php break; default: echo ("+" . ($i-0.5)); ?></option><?php } $i=$i+0.25; } ?> </select> Quote Link to comment https://forums.phpfreaks.com/topic/205368-switch-statement-problem/#findComment-1074836 Share on other sites More sharing options...
ToonMariner Posted June 21, 2010 Share Posted June 21, 2010 it mat be more beneficial to create an array of these like so... $lenses = array('-4'=>'-4','-3.5'=>'-3.5' ..... 'Plano'=>0,'Balance'=>'+0.25' .... '+2.25'=>'+2.25' ...); creating this from a database record set or similar should be trivial... the advantage is then you can simply use: <select name="cstLeftcyl" type="text"> <?php foreach($lenses as $key => $value){ $selected = $value == $cstLeftcyl ? 'selected="selected"' : NULL; ?> <option value="<?php echo $value;?>" <?php echo $selected; ?>><?php echo $key; ?></option> <?php } ?> </select> this would mean that should any new type be developed then you only need add the data to the array - the rest will take care of itself. Quote Link to comment https://forums.phpfreaks.com/topic/205368-switch-statement-problem/#findComment-1074846 Share on other sites More sharing options...
mittu1 Posted June 21, 2010 Author Share Posted June 21, 2010 Thanks guys for looking at this problem. phpchamps - your code produces the same problem I was having with mine. There are blank options after Balance and Infinity and I am at a loss as to where they are coming from. ToonMariner - your solution will work so I will use your array method. Many thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/205368-switch-statement-problem/#findComment-1074937 Share on other sites More sharing options...
phpchamps Posted June 21, 2010 Share Posted June 21, 2010 that was just a simple typo error.. try this <select name="cstLeftcyl" type="text"> <?php $i=-4; while($i<=4.5){ if ($i == $cstLeftcyl) { ?> <option value="<?php echo ($i);?>" selected="selected"><?php }else{ ?> <option value="<?php echo ($i);?>"><?php } switch ($i) { case 0: echo ("Plano"); ?></option><?php break; case 0.25: echo ("Balance"); ?></option><?php break; case 0.5: echo ("Infinity"); ?></option><?php break; case ($i<0): echo ($i); ?></option><?php break; default: echo ("+" . ($i-0.5)); ?></option><?php } $i=$i+0.25; } ?> </select> Quote Link to comment https://forums.phpfreaks.com/topic/205368-switch-statement-problem/#findComment-1074947 Share on other sites More sharing options...
mittu1 Posted June 21, 2010 Author Share Posted June 21, 2010 phpchamps - that's worked now. Thank you so much. I will try to figure out where I was going wrong! Quote Link to comment https://forums.phpfreaks.com/topic/205368-switch-statement-problem/#findComment-1074957 Share on other sites More sharing options...
phpchamps Posted June 21, 2010 Share Posted June 21, 2010 instead of closing </option> you used <option> in your switch case Quote Link to comment https://forums.phpfreaks.com/topic/205368-switch-statement-problem/#findComment-1074960 Share on other sites More sharing options...
ToonMariner Posted June 21, 2010 Share Posted June 21, 2010 all those cases will come to hurt you - build the array - hopefully programatically from some data source and you won't need to worry about it ever again Quote Link to comment https://forums.phpfreaks.com/topic/205368-switch-statement-problem/#findComment-1074965 Share on other sites More sharing options...
phpchamps Posted June 21, 2010 Share Posted June 21, 2010 yup i am also in favour or array.. use array's solution instead... Quote Link to comment https://forums.phpfreaks.com/topic/205368-switch-statement-problem/#findComment-1074967 Share on other sites More sharing options...
mittu1 Posted June 21, 2010 Author Share Posted June 21, 2010 OK, I will go with the array solution. Excellent work guys. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/205368-switch-statement-problem/#findComment-1074971 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.