Solved

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

Posted on 2014-12-02
3
138 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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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.
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.

770 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