jetlife76 Posted October 12, 2011 Share Posted October 12, 2011 Trying to get my output to to show 2 decimal places but keep getting syntax errors please help. Here's the code i have. <?php // Input Data From Form $Cost = $_POST['fielda']; //$Cost2 = number_format(,2); //Set Current Date & Shipping Date (15 days) $Date = date('l, F d, Y'); $ShipDate = date('F d, Y',strtotime('+ 15 days')); $Tax = ($Cost * .06); $Ship = 0; //$Sub = ($Cost + $Tax); //Calculate Shipping if ($Cost <= 25.99 ){ $Ship = 3.00; } if ($Cost >= 26.00 && $Cost <= 50.99 ){ $Ship = 4.00;} if ($Cost >= 51.00 && $Cost <= 75.00) { $Ship = 5.00;} if ($Cost > 75.00) { $Ship = 6.00;} //Calculate Order Total $Total = ($Cost + $Tax + $Ship); print " Date: $Date<br><br>"; print " Cost : $$Cost<br><br> Tax: $$Tax<br><br> Shipping: $$Ship<br><br> Total: $$Total<br><br>"; print "Estimated Ship Date is $ShipDate<br> " ; ?> MOD EDIT: code tags added. Quote Link to comment https://forums.phpfreaks.com/topic/248978-having-trouble-with-number_format/ Share on other sites More sharing options...
ZulfadlyAshBurn Posted October 12, 2011 Share Posted October 12, 2011 what are the syntax errors? Quote Link to comment https://forums.phpfreaks.com/topic/248978-having-trouble-with-number_format/#findComment-1278635 Share on other sites More sharing options...
jetlife76 Posted October 12, 2011 Author Share Posted October 12, 2011 sorry, Warning: number_format() expects parameter 1 to be double, string given in Quote Link to comment https://forums.phpfreaks.com/topic/248978-having-trouble-with-number_format/#findComment-1278638 Share on other sites More sharing options...
jetlife76 Posted October 12, 2011 Author Share Posted October 12, 2011 <?php // Input Data From Form $Cost = $_POST['fielda']; //$Cost2 = number_format(,2); //Set Current Date & Shipping Date (15 days) $Date = date('l, F d, Y'); $ShipDate = date('F d, Y',strtotime('+ 15 days')); $Tax = ($Cost * .06); $Ship = 0; //$Sub = ($Cost + $Tax); //Calculate Shipping if ($Cost <= 25.99 ){ $Ship = 3.00; } if ($Cost >= 26.00 && $Cost <= 50.99 ){ $Ship = 4.00;} if ($Cost >= 51.00 && $Cost <= 75.00) { $Ship = 5.00;} if ($Cost > 75.00) { $Ship = 6.00;} //Calculate Order Total $Total = ($Cost + $Tax + $Ship); print " Date: $Date<br><br>"; print number_format (" Cost : $$Cost<br><br> Tax: $$Tax<br><br> Shipping: $$Ship<br><br> Total: $$Total<br><br>",2); print "Estimated Ship Date is $ShipDate<br> " ; ?> MOD EDIT: code tags added. Quote Link to comment https://forums.phpfreaks.com/topic/248978-having-trouble-with-number_format/#findComment-1278639 Share on other sites More sharing options...
Pikachu2000 Posted October 12, 2011 Share Posted October 12, 2011 The posted code isn't generating that error. Post the actual code that's causing the error, and please post it within . . . BBCode tags. Quote Link to comment https://forums.phpfreaks.com/topic/248978-having-trouble-with-number_format/#findComment-1278641 Share on other sites More sharing options...
jetlife76 Posted October 12, 2011 Author Share Posted October 12, 2011 sorry the second code i posted is the one with the error Quote Link to comment https://forums.phpfreaks.com/topic/248978-having-trouble-with-number_format/#findComment-1278643 Share on other sites More sharing options...
jnvnsn Posted October 12, 2011 Share Posted October 12, 2011 print number_format (" Cost : $$Cost<br><br> Tax: $$Tax<br><br> Shipping: $$Ship<br><br> Total: $$Total<br><br>",2); you passed invalid variables to the number_format function. remove those. Quote Link to comment https://forums.phpfreaks.com/topic/248978-having-trouble-with-number_format/#findComment-1278646 Share on other sites More sharing options...
Pikachu2000 Posted October 12, 2011 Share Posted October 12, 2011 You can't feed an entire string to the number_format function like that. It would have no idea how to operate on it, as it expects a float/double value. You need to format each number individually, either prior to echoing the string, or or by concatenating the output into the string as it's echoed. Doing it prior would result in more readable code. $number1 = 123.45678; $number2 = 456.78901; $number1 = number_format($number1, 2); $number2 = number_format($number2, 2); echo "Number1 is: $number1, and Number2 is: $number2"; I also see no reason for you to be using variable variables here. Cost : $$Cost<br><br> Tax: $$Tax<br><br> Shipping: $$Ship<br><br> Total: $$Total<br><br> Quote Link to comment https://forums.phpfreaks.com/topic/248978-having-trouble-with-number_format/#findComment-1278649 Share on other sites More sharing options...
jetlife76 Posted October 12, 2011 Author Share Posted October 12, 2011 ok i've done that but now it says lines 16 & 18 have undefined variables, Ship and Tax, i thought they were defined aleady <?php // Input Data From Form $Cost = $_POST['fielda']; //Set Current Date & Shipping Date (15 days) $Date = date('l, F d, Y'); $ShipDate = date('F d, Y',strtotime('+ 15 days')); $Cost2 = number_format($Cost,2); $Tax2 = number_format($Tax,2); $Tax = ($Cost * .06); $Ship2 = number_format($Ship,2); $Ship = 0; //$Sub = ($Cost + $Tax); //Calculate Shipping if ($Cost <= 25.99 ){ $Ship = 3.00; } if ($Cost >= 26.00 && $Cost <= 50.99 ){ $Ship = 4.00;} if ($Cost >= 51.00 && $Cost <= 75.00) { $Ship = 5.00;} if ($Cost > 75.00) { $Ship = 6.00;} //Calculate Order Total $Total = ($Cost + $Tax + $Ship); $Total2 = number_format($Total,2); print "Date: $Date<br><br>"; print "Cost: $$Cost2<br><br>"; print "Tax: $$Tax2<br><br>"; print "Shipping: $$Ship2<br><br> $$Total<br><br>"; print "Total: $$Total2<br><br>"; print "Estimated Ship Date is $ShipDate<br> " ; ?> Quote Link to comment https://forums.phpfreaks.com/topic/248978-having-trouble-with-number_format/#findComment-1278668 Share on other sites More sharing options...
ManiacDan Posted October 12, 2011 Share Posted October 12, 2011 Works just fine for me, copied and pasted and made $cost a random number. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/248978-having-trouble-with-number_format/#findComment-1278674 Share on other sites More sharing options...
Pikachu2000 Posted October 12, 2011 Share Posted October 12, 2011 They need to be defined before you attempt to use or access their values . . . Quote Link to comment https://forums.phpfreaks.com/topic/248978-having-trouble-with-number_format/#findComment-1278675 Share on other sites More sharing options...
jetlife76 Posted October 12, 2011 Author Share Posted October 12, 2011 thanks i found wut was wrong with it, one more question please? Trying to get the date function to handle a leap year, any help? Quote Link to comment https://forums.phpfreaks.com/topic/248978-having-trouble-with-number_format/#findComment-1278691 Share on other sites More sharing options...
Pikachu2000 Posted October 12, 2011 Share Posted October 12, 2011 What do you mean? Quote Link to comment https://forums.phpfreaks.com/topic/248978-having-trouble-with-number_format/#findComment-1278696 Share on other sites More sharing options...
jetlife76 Posted October 12, 2011 Author Share Posted October 12, 2011 Since i have my shipping date to automatically configure 15 days after the order date, i need it to adjust to a leap year, in case the order is made the last day in feb.(29) on a leap Quote Link to comment https://forums.phpfreaks.com/topic/248978-having-trouble-with-number_format/#findComment-1278698 Share on other sites More sharing options...
jetlife76 Posted October 12, 2011 Author Share Posted October 12, 2011 nevermind i figured it out thanks for all your help guys Quote Link to comment https://forums.phpfreaks.com/topic/248978-having-trouble-with-number_format/#findComment-1278707 Share on other sites More sharing options...
Psycho Posted October 12, 2011 Share Posted October 12, 2011 Since i have my shipping date to automatically configure 15 days after the order date, i need it to adjust to a leap year, in case the order is made the last day in feb.(29) on a leap And, how are you determining 15 days? You shouldn't need any special handling for leap years if you did it right. It would just "work". //Create timestamp 15 days in future $ship_date_ts = str_to_time('+15 days'); //Format the ship date $formatted_ship_date = date('m-d-Y', $ship_date_ts); Quote Link to comment https://forums.phpfreaks.com/topic/248978-having-trouble-with-number_format/#findComment-1278709 Share on other sites More sharing options...
jetlife76 Posted October 18, 2011 Author Share Posted October 18, 2011 Ok if anyone can help, i need to put my shipping calculations in a programmer defined function, any help, i kno it's something simple just can't get my brain started this morning. Quote Link to comment https://forums.phpfreaks.com/topic/248978-having-trouble-with-number_format/#findComment-1280223 Share on other sites More sharing options...
ManiacDan Posted October 18, 2011 Share Posted October 18, 2011 Ok if anyone can help, i need to put my shipping calculations in a programmer defined function, any help, i kno it's something simple just can't get my brain started this morning. You seem to be making less sense the more you explain things. Copy and paste? Call a function? make a web service? We have no idea what you're talking about. Did you read what was said about leap years? PHP handles them for you. Quote Link to comment https://forums.phpfreaks.com/topic/248978-having-trouble-with-number_format/#findComment-1280229 Share on other sites More sharing options...
jetlife76 Posted October 18, 2011 Author Share Posted October 18, 2011 Well i was told that i need to use a function to calculate the shipping cost, if that makes more sense. Quote Link to comment https://forums.phpfreaks.com/topic/248978-having-trouble-with-number_format/#findComment-1280238 Share on other sites More sharing options...
ManiacDan Posted October 18, 2011 Share Posted October 18, 2011 Well i was told that i need to use a function to calculate the shipping cost, if that makes more sense. Ok...you have a bunch of code. put that code into a function. The manual will be helpful here. Quote Link to comment https://forums.phpfreaks.com/topic/248978-having-trouble-with-number_format/#findComment-1280247 Share on other sites More sharing options...
jetlife76 Posted October 18, 2011 Author Share Posted October 18, 2011 ok here's what i got, still don't truly understand the "function" though. <?php // Input Data From Form $Cost = $_POST['fielda']; //Calculate tax $Tax = ($Cost * .06); $Ship = 0; //Set Current Date & Shipping Date (15 days) $Date = date('l, F d, Y'); $ShipDate = date('F d, Y',strtotime('+ 15 days')); //Calculate Shipping function shipping_cost($Cost) { if ($Cost > 0 && $Cost <= 25.00 ){ $Ship = 3.00; } if ($Cost >= 25.01 && $Cost <= 50.00 ){ $Ship = 4.00;} if ($Cost >= 50.01 && $Cost <= 75.00) { $Ship = 5.00;} if ($Cost > 75.00) { $Ship = 6.00;} return $Ship; } //Calculate Order Total $Total = ($Cost + $Tax + $Ship); $Cost2 = number_format($Cost,2); $Tax2 = number_format($Tax,2); $Ship2 = number_format($Ship,2); $Total2 = number_format($Total,2); print "Date: $Date<br><br>"; print "Cost: $$Cost2<br><br>"; print "Tax: $$Tax2<br><br>"; print "Ship: $$Ship2<br><br>"; print "Total: $$Total2<br><br>"; print "Estimated Ship Date is $ShipDate<br> " ; ?> Quote Link to comment https://forums.phpfreaks.com/topic/248978-having-trouble-with-number_format/#findComment-1280289 Share on other sites More sharing options...
ManiacDan Posted October 18, 2011 Share Posted October 18, 2011 As much as it pains me to say it: read the manual. Functions are one of the most basic concepts of computer science. You have to call a function in order for it to do anything and (in your case) store the return value somewhere. Ask your professor if you need help with this, functions are very simple to understand. Quote Link to comment https://forums.phpfreaks.com/topic/248978-having-trouble-with-number_format/#findComment-1280319 Share on other sites More sharing options...
jetlife76 Posted October 18, 2011 Author Share Posted October 18, 2011 Thanks for your help i forgot to call it, got it working, sorry for being such a NEWB, lol Quote Link to comment https://forums.phpfreaks.com/topic/248978-having-trouble-with-number_format/#findComment-1280330 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.