Solved

PHP - need help with selected item in list?

Posted on 2013-05-25
6
291 Views
Last Modified: 2013-05-25
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

0
Comment
Question by:sabecs
6 Comments
 
LVL 6

Expert Comment

by:mickey159
Comment Utility
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!
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
The problem almost certainly occurs because PHP cannot compare floating point numbers with 100% accuracy.  There is a man page with a warning about this.  The strategy shown here normalizes these floats to a predictable string so the comparison can be made reliably.

As a sidebar note, just because your script can jump in and out of PHP doesn't make it a good idea.  In practice, the smallest number of echo statements is the best number!

Please see view-source: http://www.laprbass.com/RAY_temp_sabecs.php

<?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);

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

// SHOW THE WORK PRODUCT
echo $opt;

Open in new window

Best regards, ~Ray
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
Comment Utility
<ot>
Ray,
Could you not use HEREDOC to get rid of all the concatenation?
</ot>

Cd&
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
@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
0
 

Author Closing Comment

by:sabecs
Comment Utility
Thanks Ray, much appreciated.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Thanks for the points!  ~Ray
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now