Solved

PHP - loop though posted values and format them to 2 decimal places

Posted on 2014-12-02
3
144 Views
Last Modified: 2014-12-03
Hi,

I would like to loop though posted values and format them to nnnnn.nn, if current value is 0 or null then set $itemPrice[$key] equal to previous value, if no previous value then set to 99.99

for example if the following where submited, $str would be equal to '12.34,1999.47,1999.47,1999.47';

$itemPrice[0] = "12.34";
$itemPrice[1] = "1999.4678";
$itemPrice[2] = "0";
$itemPrice[3] = "";

for example if the following where submitted, $str would be equal to '99.99,19.48,12.30,12.30';

$itemPrice[0] = "";
$itemPrice[1] = "19.48";
$itemPrice[2] = "12.3";
$itemPrice[3] = "";

I hope that makes sense.

Thanks in advance for your feedback.

<?php error_reporting(E_ALL);

setlocale(LC_MONETARY, 'en_US');

if ((isset($_POST["update_form"])) && ($_POST["update_form"] == "Y")) {

		
	 //strip non dollar charaters and
	 $itemPrice = preg_replace("/[^0-9.]/", "", $_POST['itemPrice']);
		
		foreach ($itemPrice as $key => $val){
			if ($val == NULL) $itemPrice[$key] = '99.99';
		}
		
		// COLLAPSE THE ARRAY INTO COMMA-SEPARATED VALUES
		$str = implode(',', $itemPrice);
		var_dump($str);
		

}


$itemPrice[0] = "12.34";
$itemPrice[1] = "1999.4";
$itemPrice[2] = "0";
$itemPrice[3] = "";

?>
<html>
<head>
<title>Test</title>
</head>

<body>

<form id="form1" name="form1" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?> ">
            Normal Price 1&nbsp;<input name="itemPrice[]" type="text" id="itemPrice" value="<?php  if ($itemPrice[0] > 0) echo money_format('%(#6n',$itemPrice[0]); ?>" size="8"><br>
            Normal Price 2&nbsp;<input name="itemPrice[]" type="text" id="itemPrice" value="<?php if ($itemPrice[1] > 0)  echo money_format('%(#6n',$itemPrice[1]); ?>" size="8"><br>
            Normal Price 3&nbsp;<input name="itemPrice[]" type="text" id="itemPrice" value="<?php if ($itemPrice[2] > 0)  echo money_format('%(#6n',$itemPrice[2]); ?>" size="8"><br>
            Normal Price 4&nbsp;<input name="itemPrice[]" type="text" id="itemPrice" value="<?php if ($itemPrice[3] > 0)  echo money_format('%(#6n',$itemPrice[3]); ?>" size="8"><br>
 
  <input name="update_form" type="hidden" id="update_form" value="Y">
  <input name="Update Prices" type="submit">
</form>


</body>



</html>

Open in new window

0
Comment
Question by:sabecs
  • 2
3 Comments
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 40478233
Please see: http://iconoun.com/demo/temp_sabecs.php

Note that this mungs the input array, so you might want to copy it before injecting it into the foreach() loop.

<?php // demo/temp_sabecs.php
error_reporting(E_ALL);

/**
 * SEE http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28573451.html
 *
 * for example if the following where submited, $str would be equal to '12.34,1999.47,1999.47,1999.47';
 *
 * $itemPrice[0] = "12.34";
 * $itemPrice[1] = "1999.4678";
 * $itemPrice[2] = "0";
 * $itemPrice[3] = "";
 *
 * for example if the following where submitted, $str would be equal to '99.99,19.48,12.30,12.30';
 *
 * $itemPrice[0] = "";
 * $itemPrice[1] = "19.48";
 * $itemPrice[2] = "12.3";
 * $itemPrice[3] = "";
 */
echo '<pre>';


// TEST NUMBER ONE
// SET THE INITIAL DEFAULT FILLER
$filler = '99.99';

// SET THE TEST ARRAY
$itemPrice[0] = "12.34";
$itemPrice[1] = "1999.4678";
$itemPrice[2] = "0";
$itemPrice[3] = "";

// PROCESS THE TEST ARRAY
foreach ($itemPrice as $key => $value)
{
    if ($value)
    {
        $filler = number_format($value,2);
    }
    $itemPrice[$key] = $filler;
}

// COLLAPSE THE ARRAY INTO A COMMA-SEPARATED STRING
$str = implode(',', $itemPrice);

// SHOW THE WORK PRODUCT
var_dump($str);
echo PHP_EOL;


// TEST NUMBER TWO
// SET THE INITIAL DEFAULT FILLER
$filler = '99.99';

// SET THE TEST ARRAY
$itemPrice[0] = "";
$itemPrice[1] = "19.48";
$itemPrice[2] = "12.3";
$itemPrice[3] = "";

// PROCESS THE TEST ARRAY
foreach ($itemPrice as $key => $value)
{
    if ($value)
    {
        $filler = number_format($value,2);
    }
    $itemPrice[$key] = $filler;
}

// COLLAPSE THE ARRAY INTO A COMMA-SEPARATED STRING
$str = implode(',', $itemPrice);

// SHOW THE WORK PRODUCT
var_dump($str);
echo PHP_EOL;

Open in new window

0
 

Author Closing Comment

by:sabecs
ID: 40479708
Thanks Ray, that is perfect..
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40479772
Thanks for the points -- glad to help, ~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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

808 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