Link to home
Create AccountLog in
Avatar of sabecs
sabecs

asked on

PHP - need help with selected item in list?

Hi,
I have a select list generated using PHP, but I can’t seem to the get the "echo 'selected'" working on all numbers.
Seems to work ok when 0.01, 0.02, .04, .05, .06 are selected but not .03 or .08???

Number looks like it is stored OK in the MySQL table so I am not sure what I am doing wrong?

Your help would be appreciated.

Thanks..

<div class="itemHeading">Weight (kg)></div>
<div class="itemHeadingDetails">
<select class="check_dimensions" name="weight" id="weight">
	<?php   
			$weight = $row_upd_item["weight"];
			for($i=0.10; $i <= 20.00 ; $i+=0.10){ ?>
			 <option value="<?php echo $i ;?>" <?php if($weight == $i) echo "selected"; ?>><?php echo number_format($i, 2, '.', ''); ;?></option>
	<?php    } ?>
</select>      
</div>	


which generates select list below:

<div class="itemHeading">Weight (kg)  1.70</div>
<div class="itemHeadingDetails">
<select class="check_dimensions" name="weight" id="weight">
				 <option value="0.1" >0.10</option>
				 <option value="0.2" >0.20</option>
				 <option value="0.3" >0.30</option>
				 <option value="0.4" >0.40</option>
				 <option value="0.5" >0.50</option>
				 <option value="0.6" >0.60</option>
				 <option value="0.7" >0.70</option>
				 <option value="0.8" >0.80</option>
				 <option value="0.9" >0.90</option>
				 <option value="1" >1.00</option>
				 <option value="1.1" >1.10</option>
				 <option value="1.2" >1.20</option>
				 <option value="1.3" >1.30</option>
				 <option value="1.4" >1.40</option>
				 <option value="1.5" >1.50</option>
				 <option value="1.6" >1.60</option>
				 <option value="1.7" >1.70</option>
				 <option value="1.8" >1.80</option>
				 <option value="1.9" >1.90</option>
				 <option value="2" >2.00</option>
				 <option value="2.1" >2.10</option>
				 <option value="2.2" >2.20</option>
				 <option value="2.3" >2.30</option>
				 <option value="2.4" >2.40</option>
				 <option value="2.5" >2.50</option>
				 <option value="2.6" >2.60</option>
				 <option value="2.7" >2.70</option>
				 <option value="2.8" >2.80</option>
				 <option value="2.9" >2.90</option>
				 <option value="3" >3.00</option>
				 <option value="3.1" >3.10</option>
				 <option value="3.2" >3.20</option>
				 <option value="3.3" >3.30</option>
				 <option value="3.4" >3.40</option>
				 <option value="3.5" >3.50</option>
				 <option value="3.6" >3.60</option>
				 <option value="3.7" >3.70</option>
				 <option value="3.8" >3.80</option>
				 <option value="3.9" >3.90</option>
				 <option value="4" >4.00</option>
				 <option value="4.1" >4.10</option>
				 <option value="4.2" >4.20</option>
				 <option value="4.3" >4.30</option>
				 <option value="4.4" >4.40</option>
				 <option value="4.5" >4.50</option>
				 <option value="4.6" >4.60</option>
				 <option value="4.7" >4.70</option>
				 <option value="4.8" >4.80</option>
				 <option value="4.9" >4.90</option>
				 <option value="5" >5.00</option>
				 <option value="5.1" >5.10</option>
				 <option value="5.2" >5.20</option>
				 <option value="5.3" >5.30</option>
				 <option value="5.4" >5.40</option>
				 <option value="5.5" >5.50</option>
				 <option value="5.6" >5.60</option>
				 <option value="5.7" >5.70</option>
				 <option value="5.8" >5.80</option>
				 <option value="5.9" >5.90</option>
				 <option value="6" >6.00</option>
				 <option value="6.1" >6.10</option>
				 <option value="6.2" >6.20</option>
				 <option value="6.3" >6.30</option>
				 <option value="6.4" >6.40</option>
				 <option value="6.5" >6.50</option>
				 <option value="6.6" >6.60</option>
				 <option value="6.7" >6.70</option>
				 <option value="6.8" >6.80</option>
				 <option value="6.9" >6.90</option>
				 <option value="7" >7.00</option>
				 <option value="7.1" >7.10</option>
				 <option value="7.2" >7.20</option>
				 <option value="7.3" >7.30</option>
				 <option value="7.4" >7.40</option>
				 <option value="7.5" >7.50</option>
				 <option value="7.6" >7.60</option>
				 <option value="7.7" >7.70</option>
				 <option value="7.8" >7.80</option>
				 <option value="7.9" >7.90</option>
				 <option value="8" >8.00</option>
				 <option value="8.1" >8.10</option>
				 <option value="8.2" >8.20</option>
				 <option value="8.3" >8.30</option>
				 <option value="8.4" >8.40</option>
				 <option value="8.5" >8.50</option>
				 <option value="8.6" >8.60</option>
				 <option value="8.7" >8.70</option>
				 <option value="8.8" >8.80</option>
				 <option value="8.9" >8.90</option>
				 <option value="9" >9.00</option>
				 <option value="9.1" >9.10</option>
				 <option value="9.2" >9.20</option>
				 <option value="9.3" >9.30</option>
				 <option value="9.4" >9.40</option>
				 <option value="9.5" >9.50</option>
				 <option value="9.6" >9.60</option>
				 <option value="9.7" >9.70</option>
				 <option value="9.8" >9.80</option>
				 <option value="9.9" >9.90</option>
				 <option value="10" >10.00</option>
				 <option value="10.1" >10.10</option>
				 <option value="10.2" >10.20</option>
				 <option value="10.3" >10.30</option>
				 <option value="10.4" >10.40</option>
				 <option value="10.5" >10.50</option>
				 <option value="10.6" >10.60</option>
				 <option value="10.7" >10.70</option>
				 <option value="10.8" >10.80</option>
				 <option value="10.9" >10.90</option>
				 <option value="11" >11.00</option>
				 <option value="11.1" >11.10</option>
				 <option value="11.2" >11.20</option>
				 <option value="11.3" >11.30</option>
				 <option value="11.4" >11.40</option>
				 <option value="11.5" >11.50</option>
				 <option value="11.6" >11.60</option>
				 <option value="11.7" >11.70</option>
				 <option value="11.8" >11.80</option>
				 <option value="11.9" >11.90</option>
				 <option value="12" >12.00</option>
				 <option value="12.1" >12.10</option>
				 <option value="12.2" >12.20</option>
				 <option value="12.3" >12.30</option>
				 <option value="12.4" >12.40</option>
				 <option value="12.5" >12.50</option>
				 <option value="12.6" >12.60</option>
				 <option value="12.7" >12.70</option>
				 <option value="12.8" >12.80</option>
				 <option value="12.9" >12.90</option>
				 <option value="13" >13.00</option>
				 <option value="13.1" >13.10</option>
				 <option value="13.2" >13.20</option>
				 <option value="13.3" >13.30</option>
				 <option value="13.4" >13.40</option>
				 <option value="13.5" >13.50</option>
				 <option value="13.6" >13.60</option>
				 <option value="13.7" >13.70</option>
				 <option value="13.8" >13.80</option>
				 <option value="13.9" >13.90</option>
				 <option value="14" >14.00</option>
				 <option value="14.1" >14.10</option>
				 <option value="14.2" >14.20</option>
				 <option value="14.3" >14.30</option>
				 <option value="14.4" >14.40</option>
				 <option value="14.5" >14.50</option>
				 <option value="14.6" >14.60</option>
				 <option value="14.7" >14.70</option>
				 <option value="14.8" >14.80</option>
				 <option value="14.9" >14.90</option>
				 <option value="15" >15.00</option>
				 <option value="15.1" >15.10</option>
				 <option value="15.2" >15.20</option>
				 <option value="15.3" >15.30</option>
				 <option value="15.4" >15.40</option>
				 <option value="15.5" >15.50</option>
				 <option value="15.6" >15.60</option>
				 <option value="15.7" >15.70</option>
				 <option value="15.8" >15.80</option>
				 <option value="15.9" >15.90</option>
				 <option value="16" >16.00</option>
				 <option value="16.1" >16.10</option>
				 <option value="16.2" >16.20</option>
				 <option value="16.3" >16.30</option>
				 <option value="16.4" >16.40</option>
				 <option value="16.5" >16.50</option>
				 <option value="16.6" >16.60</option>
				 <option value="16.7" >16.70</option>
				 <option value="16.8" >16.80</option>
				 <option value="16.9" >16.90</option>
				 <option value="17" >17.00</option>
				 <option value="17.1" >17.10</option>
				 <option value="17.2" >17.20</option>
				 <option value="17.3" >17.30</option>
				 <option value="17.4" >17.40</option>
				 <option value="17.5" >17.50</option>
				 <option value="17.6" >17.60</option>
				 <option value="17.7" >17.70</option>
				 <option value="17.8" >17.80</option>
				 <option value="17.9" >17.90</option>
				 <option value="18" >18.00</option>
				 <option value="18.1" >18.10</option>
				 <option value="18.2" >18.20</option>
				 <option value="18.3" >18.30</option>
				 <option value="18.4" >18.40</option>
				 <option value="18.5" >18.50</option>
				 <option value="18.6" >18.60</option>
				 <option value="18.7" >18.70</option>
				 <option value="18.8" >18.80</option>
				 <option value="18.9" >18.90</option>
				 <option value="19" >19.00</option>
				 <option value="19.1" >19.10</option>
				 <option value="19.2" >19.20</option>
				 <option value="19.3" >19.30</option>
				 <option value="19.4" >19.40</option>
				 <option value="19.5" >19.50</option>
				 <option value="19.6" >19.60</option>
				 <option value="19.7" >19.70</option>
				 <option value="19.8" >19.80</option>
				 <option value="19.9" >19.90</option>
	</select>      
</div>		

Open in new window

Avatar of mickey159
mickey159
Flag of United States of America image

Try this:

<?php   
	$weight = $row_upd_item["weight"];
	for($i=0.10; $i <= 20.00 ; $i+=0.10){ ?>
		 <option value="<?php echo $i;?>" <?php if($weight == $i) echo "selected"; ?>
		<?php echo ">".number_format($i, 2, '.', ''); ;?></option>
	<?php    } ?>

Open in new window


It seems that you put the > in the wrong place.

Good luck!
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
<ot>
Ray,
Could you not use HEREDOC to get rid of all the concatenation?
</ot>

Cd&
@Cd&: Yes, I think so.

<?php // RAY_temp_sabecs.php
error_reporting(E_ALL);

// SIMULATED DATA FROM THE DATA BASE
$weight = 0.3;

// NORMALIZE THIS TO A STRING
$weight = number_format($weight,2);

// THE SELECT OPTIONS
$opt = NULL;

for($i=0.10; $i <= 20.00 ; $i+=0.10)
{
    // NORMALIZE THIS TO A STRING
    $fmt = number_format($i,2);
    $sel = NULL;

    // CHOOSE THE 'SELECTED' VALUE
    if ($fmt == $weight)
    {
        $sel = ' selected';
    }
    $opt .= <<<EOD
<option value="$fmt"$sel>$fmt</option>
EOD;
}

// SHOW THE WORK PRODUCT
echo $opt;

Open in new window

Best to all, ~Ray
Avatar of sabecs
sabecs

ASKER

Thanks Ray, much appreciated.
Thanks for the points!  ~Ray