Solved

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

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

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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
Comment Utility
Thanks Ray, that is perfect..
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Thanks for the points -- glad to help, ~Ray
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

762 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

8 Experts available now in Live!

Get 1:1 Help Now