Jump to content

[SOLVED] Simple adding of two variables


joesaddigh

Recommended Posts

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

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

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'];

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;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.