Vibralux Posted July 11, 2007 Share Posted July 11, 2007 Right Guys, I have this code and for some reason its not working. Can any of you spot any mistakes or somthing missing? <h3 align="center" class="style1"> Instructions </h3> <div align="center"><span class="style1"> <div align="center" class="style1">Please fill in the cost per month with the price not including VAT.<br> The Date of Renewal must be as such - 01 01 2000 <table width="400px" style="border" bgcolor="#808080"> </span> </div> </div> <tr> <td> <P align="center"> <form method='post'> <div align="center"> <div align="center"><span class="style1">Cost per month </span> <input type=text name=cpm> <br> <span class="style1">Date of Renewal: Day</span> <input type=text maxlength=2 size=2 name=rday> <span class="style1">Month</span> <input type=text maxlength=2 size=2 name=rmonth> <span class="style1">Year</span> <input type=text maxlength=4 size=2 name=ryear> <br> <input type=submit name='calculate' value='Calculate'> <input type=reset name='reset' value='Reset'> <?php if (isset($_POST["calculate"]) && $cpm='') echo "Please enter a number into the Cost per month box"; $todaysdate = time(); if (isset($_POST["calculate"]) && $rday=''){ echo "Please enter a numberinto the Renewal Day Date.";} if (isset($_POST["calculate"]) && $rmonth=''){ echo "Please enter a numberinto the Renewal Month Date.";} if (isset($_POST["calculate"]) && $ryear=''){ echo "Please enter a numberinto the Renewal Year Date.";} $costpermonth = $_GET[cpm]; $renewalday = $_GET[rday]; $renewalmonth = $_GET[rmonth]; $renewalyear = $_GET[ryear]; $renewalseconds = mktime(0, 0, 0, $renewalmonth, $renewalday, $renewalyear, 0); $timedifference = $renewalseconds - $todaysdate; $VAT = (int)($costpermonth/100)*17.5; $price = $costpermonth / $timedifference; $totalprice = $price + $VAT; //Output final calculations echo"<br>"; echo"<br>"; echo"<br>"; print_r('$VAT, $price, $totalprice'); if (isset($_POST["calculate"])) {echo "The total price is $totalprice";} echo"<br>"; if (isset($_POST["calculate"])) {echo "The price without VAT is $price";} echo"<br>"; if (isset($_POST["calculate"])) {echo "The VAT is $VAT";} echo"<br>"; ?> </div> Im not sure if ive used time and mktime wrong. Please help! Quote Link to comment Share on other sites More sharing options...
suma237 Posted July 11, 2007 Share Posted July 11, 2007 what you want ? Quote Link to comment Share on other sites More sharing options...
mosi Posted July 11, 2007 Share Posted July 11, 2007 You set your variables using $_GET['x']; here: $costpermonth = $_GET[cpm]; $renewalday = $_GET[rday]; $renewalmonth = $_GET[rmonth]; $renewalyear = $_GET[ryear]; When your form for submitting uses POST, try changing them to: $costpermonth = $_POST[cpm]; $renewalday = $_POST[rday]; $renewalmonth = $_POST[rmonth]; $renewalyear = $_POST[ryear]; Also to tidy your code a bit, at the end use: if (isset($_POST["calculate"])) { echo "The total price is $totalprice<BR>"; echo "The price without VAT is $price<BR>"; echo "The VAT is $VAT<BR>"; } Any better now? Quote Link to comment Share on other sites More sharing options...
Vibralux Posted July 11, 2007 Author Share Posted July 11, 2007 The code is supposed to be taking what is put into the form out of the form and then play with it. I want to find out how much its gonna cost to upgrade a server or somthing in the middle of a persons contract. The script should automatically get todays date and then get the date that was inputted into the form. Then it should take those away from each other to give somthing that i can use later. Then it does some more calculations. Then it should ouput them in an echo. Quote Link to comment Share on other sites More sharing options...
suma237 Posted July 11, 2007 Share Posted July 11, 2007 use'==' sign for comparision Quote Link to comment Share on other sites More sharing options...
OLG Posted July 11, 2007 Share Posted July 11, 2007 I'll leave you with two statements and a comment echo "You don't echo $variables like this"; echo "You echo ".$variables." like this"; What isn't your script doing? Quote Link to comment Share on other sites More sharing options...
Vibralux Posted July 11, 2007 Author Share Posted July 11, 2007 OK, the calculations work now. But it comes up with the print_r bit which i dont want. It says i have to have somthing in there so what do i have in there? Also, now the calculations work when i click calculate i get the numbers but i get an E-06 at the end of the number. Does any one know what this is/means? Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 11, 2007 Share Posted July 11, 2007 Not sure what the (int) is here for: $VAT = (int)($costpermonth/100)*17.5; echo 'Or you can echo '.$variables.' like this'; btw OLG, the first method is actually correct. You can echo variables inside doubles quotes, just not single without surrounding them with {} Quote Link to comment Share on other sites More sharing options...
OLG Posted July 11, 2007 Share Posted July 11, 2007 If you don't want the print_r bit, then remove it????? Yes, i also knew this Yesideez, i regularly do it in MySQL statements. Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 11, 2007 Share Posted July 11, 2007 <h3 align="center" class="style1"> Instructions </h3> <div align="center"><span class="style1"> <div align="center" class="style1">Please fill in the cost per month with the price not including VAT.<br> The Date of Renewal must be as such - 01 01 2000 <table width="400px" style="border" bgcolor="#808080"> </span> </div> </div> <tr> <td> <P align="center"> <form action="" method="post"> <div align="center"> <div align="center"><span class="style1">Cost per month </span> <input type=text name=cpm> <br> <span class="style1">Date of Renewal: Day</span> <input type=text maxlength=2 size=2 name=rday> <span class="style1">Month</span> <input type=text maxlength=2 size=2 name=rmonth> <span class="style1">Year</span> <input type=text maxlength=4 size=2 name=ryear> <br> <input type=submit name='calculate' value='Calculate'> <input type=reset name='reset' value='Reset'> <?php if (isset($_POST["calculate"]) && $cpm=='') echo "Please enter a number into the Cost per month box"; $todaysdate = time(); if (isset($_POST["calculate"]) && $rday==''){ echo "Please enter a numberinto the Renewal Day Date.";} if (isset($_POST["calculate"]) && $rmonth==''){ echo "Please enter a numberinto the Renewal Month Date.";} if (isset($_POST["calculate"]) && $ryear==''){ echo "Please enter a numberinto the Renewal Year Date.";} $costpermonth = $_POST[cpm]; $renewalday = $_POST[rday]; $renewalmonth = $_POST[rmonth]; $renewalyear = $_POST[ryear]; $renewalseconds = mktime(0, 0, 0, $renewalmonth, $renewalday, $renewalyear, 0); $timedifference = $renewalseconds - $todaysdate; $VAT = (int)($costpermonth/100)*17.5; $price = $costpermonth / $timedifference; $totalprice = $price + $VAT; //Output final calculations echo"<br>"; echo"<br>"; echo"<br>"; print_r('$VAT, $price, $totalprice'); //COMMENT THIS OUT IF YOU DON'T WANT IT if (isset($_POST["calculate"])) {echo "The total price is £$totalprice";} echo"<br>"; if (isset($_POST["calculate"])) {echo "The price without VAT is £$price";} echo"<br>"; if (isset($_POST["calculate"])) {echo "The VAT is £$VAT";} echo"<br>"; ?> </div> Try that. OLG: Why say you don't do it? It's perfectly fine to! Quote Link to comment Share on other sites More sharing options...
OLG Posted July 11, 2007 Share Posted July 11, 2007 It isn't good practice to do it. I'm not going to get into a coding language semantics debate with you. Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 11, 2007 Share Posted July 11, 2007 Yes, i also knew this Yesideez, i regularly do it in MySQL statements. You mean like this? $query=mysql_query("SELECT data FROM table WHERE `field`='$variable'"); ? OLG: I've been programming for 26 years - properly. I think I know how to echo variables. There's nothing wrong with it. Quote Link to comment Share on other sites More sharing options...
Vibralux Posted July 11, 2007 Author Share Posted July 11, 2007 ok, this is what i have now </div> <h3 align="center" class="style1"> Instructions </h3> <div align="center"><span class="style1"> <div align="center" class="style1">Please fill in the cost per month with the price not including VAT.<br> The Date of Renewal must be as such - 01 01 2000 <table width="400px" style="border" bgcolor="#808080"> </span> </div> </div> <tr> <td> <P align="center"> <form method='post'> <div align="center"> <div align="center"><span class="style1">Cost per month </span> <input type=text name=cpm> <br> <span class="style1">Date of Renewal: Day</span> <input type=text maxlength=2 size=2 name=rday> <span class="style1">Month</span> <input type=text maxlength=2 size=2 name=rmonth> <span class="style1">Year</span> <input type=text maxlength=4 size=2 name=ryear> <br> <input type=submit name='calculate' value='Calculate'> <input type=reset name='reset' value='Reset'> <?php if (isset($_POST["calculate"]) && $cpm='') echo "Please enter a number into the Cost per month box"; $todaysdate = time(); if (isset($_POST["calculate"]) && $rday=''){ echo "Please enter a numberinto the Renewal Day Date.";} if (isset($_POST["calculate"]) && $rmonth=''){ echo "Please enter a numberinto the Renewal Month Date.";} if (isset($_POST["calculate"]) && $ryear=''){ echo "Please enter a numberinto the Renewal Year Date.";} $costpermonth = $_POST[cpm]; $renewalday = $_POST[rday]; $renewalmonth = $_POST[rmonth]; $renewalyear = $_POST[ryear]; $renewalseconds = mktime(0, 0, 0, $renewalmonth, $renewalday, $renewalyear, 0); $timedifference = $renewalseconds - $todaysdate; $VAT = ($costpermonth/100)*17.5; $price = $costpermonth / $timedifference; $totalprice = $price + $VAT; //Output final calculations echo"<br>"; echo"<br>"; echo"<br>"; if (isset($_POST["calculate"])) {echo "The total price is $totalprice <br>"; if (isset($_POST["calculate"])) echo "The price without VAT is $price <br>"; if (isset($_POST["calculate"])) echo "The VAT is $VAT <br>";} ?> </div> It works fine except i get E-06 on the end of the second number. Also how do i round it up to a whole number as its going to be money as you can proberbly work out. Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 11, 2007 Share Posted July 11, 2007 If you want to round up use ceil() and to round down use floor() Which line displays the second number? btw, if you use £ you can get the pound sign. I'm presuming you're in the UK like me as you're using VAT at 17.5% Quote Link to comment Share on other sites More sharing options...
Vibralux Posted July 11, 2007 Author Share Posted July 11, 2007 The second line is this command if (isset($_POST["calculate"])) echo "The price without VAT is $price <br>"; And with the round up and roun down. I need it to work it out its self whether it needs to round it up or round it down. Do i do and if statement for this? Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 11, 2007 Share Posted July 11, 2007 I'm not sure I follow your question regarding the number. You can shorten this lot to: if ($_POST["calculate"]) { echo "The total price is $totalprice <br>"; echo "The price without VAT is $price <br>"; echo "The VAT is $VAT <br>"; } Quote Link to comment Share on other sites More sharing options...
Vibralux Posted July 11, 2007 Author Share Posted July 11, 2007 ok here is the webpage http://www.matt.no1-host.co.uk/index.php type in 4.99 into the top box then for the date type 20/07/2007 The second line down, where is says The price without VAT is ..... at the end of the number it will have E-06 Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 11, 2007 Share Posted July 11, 2007 Have you thought of using sprintf() for displaying monetary values? echo sprintf('The price without VAT is %01.2f',$price); Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 11, 2007 Share Posted July 11, 2007 I see what you mean now - that's a exponential number. Basically there are too many numbers past the decimal point so it prints it out like that instead. Try the sprintf() method above to round the number to 2 decimal places and see how that works. Quote Link to comment Share on other sites More sharing options...
OLG Posted July 11, 2007 Share Posted July 11, 2007 You are doing this right? $VAT = int(($costpermonth/100)*17.5); Quote Link to comment Share on other sites More sharing options...
Vibralux Posted July 11, 2007 Author Share Posted July 11, 2007 No, as above i got told to get rid of the int. Shall i put it back? BTW, i cant get it to output a number with the sprintf Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 11, 2007 Share Posted July 11, 2007 I was wondering that myself. As it's a monetary equation I'd remove the int() altogether and tackle formatting the output to decimal places. What output do you get with sprintf? Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 11, 2007 Share Posted July 11, 2007 btw, it's intval() - but I'd leave it out completely. Quote Link to comment Share on other sites More sharing options...
Vibralux Posted July 11, 2007 Author Share Posted July 11, 2007 with the sprintf i get no output at all. Also is that the correct way to be doing it as im soon going to be rounding it up. Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 11, 2007 Share Posted July 11, 2007 If you're going to be rounding it up then use ceil() as mentioned earlier. Quote Link to comment 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.