Solved

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

Posted on 2014-12-02
3
153 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
[X]
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
  • 2
3 Comments
 
LVL 110

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 110

Expert Comment

by:Ray Paseur
ID: 40479772
Thanks for the points -- glad to help, ~Ray
0

Featured Post

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.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

738 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