Jump to content

Switch statement problem


mittu1

Recommended Posts

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]

Link to comment
https://forums.phpfreaks.com/topic/205368-switch-statement-problem/
Share on other sites

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>


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.

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.

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>


Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.