Jump to content

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>


This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.