Jump to content

Val addition


timmah1

Recommended Posts

Can you do a print_r on $_POST['package_'] so we can see exactly what is being processed.

 

I just tried:

 

<?php
$package=array("bread"=>"89.99,wed","cheese"=>"30.00,fri");
$total=0;

while (list ($name,$val) = @each ($package)) {     
      $raw = "$val";
      $price = explode(",", $raw);
      $total += $price[0];
  }
echo $total;
?>

 

which from your earlier comments is what i think the structure of $_POST['package_'] would give and I get the correct total.  Thats why i think your while loop is actually ok and there is another problem...

Link to comment
https://forums.phpfreaks.com/topic/136969-val-addition/page/2/#findComment-715418
Share on other sites

Yesideez,

You are my hero!

 

This is the final code

while (list ($name,$val) = @each ($_POST['package_'])) {     
		$val=substr($val,1);
	  $price = explode(",", $val);
	  $tmp = $price[0];
	  $total += $tmp;

 

And it gives me a total now

 

Thank you so much for your patience

 

Mad Mick, thank you for your help as well, but the $_POST['package_'] has always worked ok

Link to comment
https://forums.phpfreaks.com/topic/136969-val-addition/page/2/#findComment-715427
Share on other sites

This might be safer...

while (list ($name,$val) = @each ($_POST['package_'])) {     
        $price = explode(",", $val);
        $total += substr($price[0],1);

 

Give it a whirl and see...

 

Where you had it before it was stripping the first character off each element in the array and the display looks a bit strange with the first character missing.

Link to comment
https://forums.phpfreaks.com/topic/136969-val-addition/page/2/#findComment-715431
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.