Page 1 of 1

calculations

Posted: Tue Nov 04, 2008 7:37 am
by zenix
Hi, I am still learning MySQL and have been doing pretty well. I've run into a wall however and can't for the life of me figure out what I am doing wrong. If someone could point me in the right direction I would REALLY appreciate it.

I am attempting to populate a table with data. The table populates alright, but in the column for profit/loss/break even (where the result returned is supposed to have a color font appropriate to the status and a number unless even I always get "Broke even" no matter what data is fed into the equation. My equation is below;

global $profit_or_loss;
function calculate_differences($earnings,$cost)

{
$difference = $earnings - $cost;

if($difference = 0)
{
$difference = substr($difference,1);
$font_color ='green';
$profit_or_loss = "$".$difference."m";
}
elseif($difference >0)
{
$font_color ='blue';
$profit_or_loss = "$".$difference."m";
}
else
{
$font_color ='red';
$profit_or_loss = "$".$difference."m";
}
return "<font color='$font_color'>" . $profit_or_loss . "</font>";

}
...
I have a call to the function later on in the code as:
...
$movie_health = calculate_differences($movie_earnings, $movie_cost);
$page_start=<<<EOD
<HTML>
<head>
<title>Details and Reviews for: $movie_name</title>
</head>
<body>
EOD;
//global $profit_or_loss = $movie_health;
$movie_details =<<<EOD
<table width='70%' border='2' cellspacing='2' cellpadding='2' align='center'>
<tr>
<th colspan='6'><u><h2>$movie_name: Details</h2></u></th>
</tr>
$movie_table_headings
<tr>
<td width='33%' align='center'>$movie_name</td>
<td align='center'>$movie_year</td>
<td align='center'>$director</td>
<td align='center'>$leadactor</td>
<td align='center'>$movie_running_time</td>
<td align='center'>$movie_health</td>

</tr>
</table>

I have tried everything I can possibly think of, even starting over from scratch. Thanks a lot in advance!! I hope this is the correct forum, I posted it at a mysql forum and was asked why I had posted it there.

Re: calculations

Posted: Tue Nov 04, 2008 7:45 am
by papa
Try:

if($difference == 0)

Re: calculations

Posted: Tue Nov 04, 2008 8:51 am
by zenix
Thank you for the guidance! It looks as though you put me on the right track in the right direction...but it only changed which result shows. Now, all the results are green whether the movie was profitable or not, and I'm still not getting $difference to display an int in the column reflecting the difference.

I REALLY appreciate your assistance. Thanks!!

Re: calculations

Posted: Tue Nov 04, 2008 8:58 am
by Mark Baker
What values are you passing through to the calculate_differences() function?
Are $movie_earnings and $movie_cost numeric values like 12500000? or strings like '$12,500,000'?

Re: calculations

Posted: Tue Nov 04, 2008 9:15 am
by zenix
They are integers 125000. An example of where the input is coming from looks like this:

//insert new data into "movie" table for each movie
$update="UPDATE movie SET
movie_running_time=118,
movie_cost=280,
movie_earnings=137
WHERE movie_id = 1";
$results = mysql_query($update)
or die(mysql_error());

Please keep in mind that I am new enough at this that I haven't had time to make my coding very efficient. I thought I'd concentrate on learning it first.

Re: calculations

Posted: Tue Nov 04, 2008 10:12 am
by aceconcepts
Using "==0" in your conditional statement is quite a specific calculation - what if the earnings minus cost is a loss?

Re: calculations

Posted: Tue Nov 04, 2008 11:50 am
by zenix
I tried the ==0 and it changed the out come from always displaying a blue worded result to always displaying a green worded one, but still no numbers. So, I took it out and now, it's still showing green. All the results display green $0m in the column, whether the result is positive, negative or the same. Pretty frustrating.

Re: calculations

Posted: Tue Nov 04, 2008 11:55 am
by Mark Baker
Any particular reason why you're declaring $profit_or_loss as a global right at the top of your code?

Any particular reason why you're doing a $difference = substr($difference,1) if $difference is 0?


With the correction to == 0 the function seems to work perfectly correctly. Is the problem possibly somewhere in the information you're reading from the movie database?

Re: calculations

Posted: Tue Nov 04, 2008 12:37 pm
by zenix
Yeah, it was just an experiment to see if it would resolve the problem I am having, sorry. I was going to remove it before posting but forgot