Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > PHP

Reply
 
LinkBack Thread Tools Display Modes
Old 07-11-2004, 03:35 PM   #1 (permalink)
sarah31
Code Monkey
 
sarah31's Avatar
 
Join Date: May 2002
Location: canada
Posts: 55
sarah31 is on a distinguished road
perplexing issue with some calculations

i have this code:

Code:
	$_POST['quantity'] = 0;
	for ($i=0;$i<30;$i++){
		$_POST['quantity'] += $_POST['q'.$i];
	}
	print "<div class='center-text'><b>Number of Books:</b> {$_POST['quantity']}</div>";

	$_POST['fmvtotal'] = 0;
	for ($i=0;$i<30;$i++){
		$_POST['fmvtotal'] += $_POST['fmv'.$i];
	}
	$_POST['fmvtotal'] = number_format ($_POST['fmvtotal'], 2);
	print "<div class='center-text'><b>FMV Total $:</b> {$_POST['fmvtotal']}</div>"; 

	if ($_POST['fmvtotal'] >= 1 && $_POST['fmvtotal'] <= 100) {
	$_POST['ins'] = '3.50';
	} elseif ($_POST['fmvtotal'] > 100) {
	$_POST['ins'] = (ceil(($_POST['fmvtotal'] - 100) / 100) * 0.45) + 3.50;
	} else {
	$_POST['ins'] = 0;
	}
	
	$_POST['ins'] = number_format ($_POST['ins'], 2);
	print "<div class='center-text'><b>Insurance Total $:</b> {$_POST['ins']}</div>"; 
	
	

	$_POST['servicesubtotal'] = $_POST['service']*$_POST['quantity'];
	
	$_POST['servicesubtotal'] = number_format ($_POST['servicesubtotal'], 2);
	print "<div class='center-text'><b>Service Charges $:</b> {$_POST['servicesubtotal']}</div>";

	if ($_POST['quantity'] == 1) {
	$_POST['shipping'] = '7.40';
	} elseif ($_POST['quantity'] == 2){
	$_POST['shipping'] = '10.90';
	} elseif ($_POST['quantity'] >= 3 && $_POST['quantity'] <= 5){
	$_POST['shipping'] = '14.90';
	} elseif ($_POST['quantity'] > 5) {
	$_POST['shipping'] = (($_POST['quantity']-5)*1)+14.90;
	} else {
	$_POST['shipping'] = 0;
	}

	$_POST['shipping'] = number_format ($_POST['shipping'], 2);
	print "<div class='center-text'><b>Shipping Cost:</b> {$_POST['shipping']}</div><br>";

	//grand total calculation

	$_POST['grandtotal'] = $_POST['shipping']+$_POST['servicesubtotal']+$_POST['ins'];
	$_POST['grandtotal'] = number_format ($_POST['grandtotal'], 2);
	print "<div class='center-text'><b>GRAND TOTAL$:</b> {$_POST['grandtotal']}</div>";

now if i have the following numbers entered:

Code:
Quantity 90
Publisher DC
Title Wonder Woman
Issue # 105
Month/Year June 1958
FMV 11250
i get this output:
Code:
Number of Books: 90
FMV Total $: 11,250.00
Insurance Total $: 3.50
Service Charges $: 1,260.00
Shipping Cost: 99.90

GRAND TOTAL$: 104.40
obviously the insurance (ins) calculation is not accepting the FMV Total number into the calculation and the shipping total is not being allowed into the grand total either. i have looked and look for an answer but i haven't found any explanation and i need this fixed.
sarah31 is offline   Reply With Quote
Old 07-11-2004, 06:08 PM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,706
sde is on a distinguished road
number_format returns a string. the problem is when it adds a ',' in the number, php doesn't know how to cast it back and handle it as a float.

if you converted 11,250.00 to a float, it drops everything off past the first ',', resulting in 11.

i just made those seperate variables since you are just doing that to print to the screen.
PHP Code:
    for ($i=0;$i<30;$i++){
        
$quantity += $_POST['q'.$i];
    }    
    print 
"<div class='center-text'><b>Number of Books:</b> ".$quantity."</div>";

    for (
$i=0;$i<30;$i++){
        
$fmvtotal += $_POST['fmv'.$i];
    }
    
    
$fmvtotal_string number_format($fmvtotal2);    
    print 
"<div class='center-text'><b>FMV Total $:</b> ".$fmvtotal_string."</div>"

    if (
$fmvtotal >= && $fmvtotal <= 100) {
      
$ins 3.50;
    } elseif (
$fmvtotal 100.0) {
      
$ins = ( ceil( ( $fmvtotal 100) / 100 ) * 0.45) + 3.50;
    } else {
      
$ins 0;
    }
    
    
$ins_string number_format($ins2);
    print 
"<div class='center-text'><b>Insurance Total $:</b> ".$ins_string."</div>"
    
    
$servicesubtotal $_POST['service'] * $quantity;
    
    
$servicesubtotal number_format($servicesubtotal2);
    print 
"<div class='center-text'><b>Service Charges $: </b>".$servicesubtotal."</div>";

    if (
$quantity == 1) {
      
$shipping '7.40';
    } elseif (
$quantity == 2){
      
$shipping '10.90';
    } elseif (
$quantity >= && $_POST['quantity'] <= 5){
      
$shipping '14.90';
    } elseif (
$quantity 5) {
      
$shipping = (($quantity-5)*1)+14.90;
    } else {
      
$shipping 0;
    }

    
$shipping_string number_format($shipping2);
    print 
"<div class='center-text'><b>Shipping Cost:</b> ".$shipping_string."</div><br>";

    
//grand total calculation
    
$grandtotal =  $shipping+$servicesubtotal+$ins;
    
$grandtotal number_format($grandtotal2);
    print 
"<div class='center-text'><b>GRAND TOTAL$:</b> ".$grandtotal."</div>"
also, notice you don't need the $_POST[] for every variable. you should really only need to use $_POST[] when retrieving values from a form whos method is set to POST.
__________________
Mike

Last edited by sde; 07-11-2004 at 09:52 PM.
sde is offline   Reply With Quote
Old 07-11-2004, 08:11 PM   #3 (permalink)
sarah31
Code Monkey
 
sarah31's Avatar
 
Join Date: May 2002
Location: canada
Posts: 55
sarah31 is on a distinguished road
hmmm...does it matter that there is no way of telling if someone will be submitting values over three digits in length? the 90 and 11250 were just examples for this question but in actuality the chances that those exact numbers will be entered is slim.

as for not using $_POST[] would that not require register globals to be on? my understanding is that register globals is not something you really want on for security reasons.


EDIT: oh and btw the data that the calculations are being performed on are from a form.
sarah31 is offline   Reply With Quote
Old 07-11-2004, 09:31 PM   #4 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,706
sde is on a distinguished road
i understand those were just examples. my fix will handle any value as it handles those examples.

2. register_globals just won't let you access POST form data without the $_POST[] array defined. it is perfectly fine to not use $_POST[] if you are creating new variables in your script.

3. it should work fine. just the 'dummy vars' at the top out and it should work with the form data.

have you tried it?
__________________
Mike
sde is offline   Reply With Quote
Old 07-11-2004, 09:39 PM   #5 (permalink)
sarah31
Code Monkey
 
sarah31's Avatar
 
Join Date: May 2002
Location: canada
Posts: 55
sarah31 is on a distinguished road
not yet. i just wanted to be sure of the dummay variables and such first. thanks for the help i will apply your suggested changes tomorrow ... i am too tired right now.
sarah31 is offline   Reply With Quote
Old 07-11-2004, 09:53 PM   #6 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,706
sde is on a distinguished road
there, i took them out to avoid confusion .. it is just how i was testing the script.
__________________
Mike
sde is offline   Reply With Quote
Old 07-11-2004, 10:29 PM   #7 (permalink)
sarah31
Code Monkey
 
sarah31's Avatar
 
Join Date: May 2002
Location: canada
Posts: 55
sarah31 is on a distinguished road
heh. thanks. as always i appreciate the help.
sarah31 is offline   Reply With Quote
Old 07-12-2004, 07:04 AM   #8 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,706
sde is on a distinguished road
omg! i did this before i went to sleep and i had a dream that i was wrong about using new variables without $_POST[] hahah .. it was generating errors .. but it was just a dream.
__________________
Mike
sde is offline   Reply With Quote
Old 07-12-2004, 10:10 AM   #9 (permalink)
sarah31
Code Monkey
 
sarah31's Avatar
 
Join Date: May 2002
Location: canada
Posts: 55
sarah31 is on a distinguished road
could be worse you could have dreamed you were being persued by man-eating anteaters. just when you thought you would be safe one of their long skinny tongues wraps around your ankle tripping you. you can guess the rest.
sarah31 is offline   Reply With Quote
Old 07-12-2004, 10:43 AM   #10 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,706
sde is on a distinguished road
__________________
Mike
sde is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
php/frontpage webbot issue sarah31 PHP 4 06-30-2004 10:44 AM
CCS / Style issue sharmap HTML, XML, Javascript, AJAX 6 06-18-2004 09:03 AM


All times are GMT -8. The time now is 03:59 PM.


Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0 RC8 ©2007, Crawlability, Inc.





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting