Jump to content
Larry Ullman's Book Forums

A Number Over 1,000 Is Being Added As 1


Recommended Posts

Hi all,
I'm querying a DB for basket contents. I'm then using a foreach to display and then add the totals togethor. The grand total is then being displayed below the list of items in the basket. All pretty standard stuff. However if the amount of one of the items in the basket is greater that 999 it just counts the number of thousands, i.e.  £1,245.60 will be counted as 1. £2,842.70 is counted as 2. £22,168.50 is counted as 22.
 
Relevant code

foreach ($_SESSION['curtain_basket'] as $curtain_title => $curtain_value)
	{	
		echo'<tr>';
		foreach ($curtain_value as $cur_t => $cur_v)
		{
			echo'
			<td><p>'. $cur_v . '</p></td>
			';			
			
		}
		echo'
		<td><input name="curtain_remove" type="checkbox" value="' . $curtain_title . '" /></td>
		<td><p class="tiny_text">Remove?</p></td>
		</tr>';
		echo'</br>';
			
		//Display the delivery total, the artwork total and grand total.		
		$curtain_price_total += $cur_v;		
	}

echo'<h3>Total for curtains: &pound '. number_format((float)$curtain_price_total, 2, '.', '').'</h3>';

Relevant debugging info

[curtain_basket] => Array                (                    [0] => Array                        (                            [Curtain Type] => Handmade triple pleat buckram heading                            [Track or pole width] => 500                            [Finished length of the curtain (drop)] => 500                            [Type of lining] => Blackout                            [Fullness] => Normal (double fullness)                            [Interlined] => No                            [Fabric width] => 139                            [The final quote] => 662.00                        )                    [1] => Array                        (                            [Curtain Type] => Handmade triple pleat buckram heading                            [Track or pole width] => 1000                            [Finished length of the curtain (drop)] => 1000                            [Type of lining] => Blackout                            [Fullness] => Normal (double fullness)                            [Interlined] => Yes                            [Fabric width] => 139                            [The final quote] => 2,615.70                        )                    [2] => Array                        (                            [Curtain Type] => Handmade triple pleat buckram heading                            [Track or pole width] => 5000                            [Finished length of the curtain (drop)] => 5000                            [Type of lining] => Blackout                            [Fullness] => Normal (double fullness)                            [Interlined] => No                            [Fabric width] => 139                            [The final quote] => 22,158.00                        )                )
[curtain_price_total] => 686    [curtain_value] => Array        (            [Curtain Type] => Handmade triple pleat buckram heading            [Track or pole width] => 5000            [Finished length of the curtain (drop)] => 5000            [Type of lining] => Blackout            [Fullness] => Normal (double fullness)            [Interlined] => No            [Fabric width] => 139            [The final quote] => 22,158.00        )    [curtain_title] => 2    [cur_v] => 22,158.00    [cur_t] => The final quote

It seemed to me at first glance to be a number formatting issue however the value of 'curtain_price_total' variable is already incorrect so the formatting seems to me to be irrelevant.

 

It's a mystery to me and any help would be greatly appreciated.

Cheers

Paul

 

Link to comment
Share on other sites

I think your issue is that you're trying to type hint float on a number that isn't a float: $curtain_price_total.

 

(float)1,234.45

 

Floats don't have commas.

 

Personally, I would store price as integers in the DB and then format as and when required in your scripts.

Link to comment
Share on other sites

Hi rob,

Thanks for the answer. I tried removing the 'float' part of the number_format code and it made no difference. I take your point though, I can see why it wouldn't be needed. I'll bear that in mind and change it where I've used it in other areas.

 

However, if it were a formatting issue then would that make the 'curtain_price_total' variable incorrect?

 

Cheers

Paul

Link to comment
Share on other sites

Hi HartleySan,

Thanks for the answer.

1. The $curtain_price_total was initialised, it was at the top of the page and I didn't include it.

$curtain_price_total = 0;

2. I changed the code to

$curtain_price_total += (int)$cur_v;

It didn't work unfortunately. I did think about adding the '(int)' within the foreach but some of the values are strings.

 

Thanks anyway.

Paul

Link to comment
Share on other sites

I think Rob meant ditch the comma but keep the float cast. If you want to store the data with the comma then strip it out before you use it; simplest method would be to use:

 

 $cur_v = str_replace(',', '', $cur_v)

 

or to strip out more characters use preg_replace()
 

 

Link to comment
Share on other sites

 Share

×
×
  • Create New...