Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

PHP - need help with selected item in list?

Posted on 2013-05-25
6
302 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
ID: 39196619
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 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39196742
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
ID: 39196747
<ot>
Ray,
Could you not use HEREDOC to get rid of all the concatenation?
</ot>

Cd&
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39196784
@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
ID: 39197256
Thanks Ray, much appreciated.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39197264
Thanks for the points!  ~Ray
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
php56-php-mcrypt for rhel7 php56 1 48
PHP and JQuery Syntax question 4 26
Output to HTML in powershell 3 25
Set time on Session (ASP) 14 22
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 …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

839 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