Solved

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

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
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…
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…
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.

623 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