joesaddigh Posted August 15, 2009 Share Posted August 15, 2009 Please somebody put me out of my misery I want to do a simple calculation and it will not work //Create and intialise a int variable $totalprice = 0; //----------------------------------------------------------------------------------------------------------------------- //Do the calculation of the courseprice * the amount of weeks plus the enrolment fee - include accommodation if neccessary if ($accommodationflag) { $courseprice = ($courseprice * $durationweeks) + $enrolmentfee; echo $totalprice = $totalpriceaccommodation + $courseprice; } Assume $courseprice = 2040 $$totalpriceaccommodation 1330 $totalprice returns 2040? Link to comment https://forums.phpfreaks.com/topic/170379-solved-simple-adding-of-two-variables/ Share on other sites More sharing options...
DEVILofDARKNESS Posted August 15, 2009 Share Posted August 15, 2009 In first place I would do $courseprice = ($courseprice * $durationweeks) + $enrolmentfee; $totalprice = $totalpriceaccommodation + $courseprice; echo $totalprice; btw this should work, there is no way that this simple calculation shouldn't work Link to comment https://forums.phpfreaks.com/topic/170379-solved-simple-adding-of-two-variables/#findComment-898771 Share on other sites More sharing options...
joesaddigh Posted August 15, 2009 Author Share Posted August 15, 2009 I think that the problem is that it is being treated as a string. I have stupidly stored the course price as a varchar in the db. In this case I will need to cast it to be an integer, after retrieving it ?? Do you know how to do this? //Get the CourseID, price and enrolment fee $querycourseid="SELECT * FROM course WHERE CourseTitle = '$coursetitle' AND CourseLevel = '$courselevel'"; $resultcourseid=mysql_query($querycourseid) or die("Error getting course details"); $rowcourseid = mysql_fetch_assoc($resultcourseid); $courseid=$rowcourseid['CourseId']; $courseprice=$rowcourseid['CoursePrice']; $enrolmentfee = $rowcourseid['CourseEnrolmentFee']; Link to comment https://forums.phpfreaks.com/topic/170379-solved-simple-adding-of-two-variables/#findComment-898773 Share on other sites More sharing options...
DarkendSoul Posted August 15, 2009 Share Posted August 15, 2009 PHP should be smart enough to attempt to treat each value as a number even if they're strings. Did you try DEVILofDARKNESS' code. It should work. Or you could try $courseprice = ($courseprice * $durationweeks) + $enrolmentfee; echo $totalpriceaccommodation + $courseprice; Link to comment https://forums.phpfreaks.com/topic/170379-solved-simple-adding-of-two-variables/#findComment-898776 Share on other sites More sharing options...
joesaddigh Posted August 15, 2009 Author Share Posted August 15, 2009 Yes I did. I thought the same that PHP handled this for you... I have casted it to an integer and it seems to work fine. Thanks for your help guys Link to comment https://forums.phpfreaks.com/topic/170379-solved-simple-adding-of-two-variables/#findComment-898778 Share on other sites More sharing options...
DarkendSoul Posted August 15, 2009 Share Posted August 15, 2009 If you're dealing with prices chances are you don't want to treat them as integers, you want to use floating points. echo (float)$totalpriceaccommodation + (float)$courseprice; Link to comment https://forums.phpfreaks.com/topic/170379-solved-simple-adding-of-two-variables/#findComment-898779 Share on other sites More sharing options...
joesaddigh Posted August 15, 2009 Author Share Posted August 15, 2009 Ok Thanks I will change that!! Link to comment https://forums.phpfreaks.com/topic/170379-solved-simple-adding-of-two-variables/#findComment-898780 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.