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.
calculations
Moderator: General Moderators
Re: calculations
Try:
if($difference == 0)
if($difference == 0)
Re: calculations
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!!
I REALLY appreciate your assistance. Thanks!!
-
Mark Baker
- Forum Regular
- Posts: 710
- Joined: Thu Oct 30, 2008 6:24 pm
Re: calculations
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'?
Are $movie_earnings and $movie_cost numeric values like 12500000? or strings like '$12,500,000'?
Re: calculations
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.
//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.
- aceconcepts
- DevNet Resident
- Posts: 1424
- Joined: Mon Feb 06, 2006 11:26 am
- Location: London
Re: calculations
Using "==0" in your conditional statement is quite a specific calculation - what if the earnings minus cost is a loss?
Re: calculations
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.
-
Mark Baker
- Forum Regular
- Posts: 710
- Joined: Thu Oct 30, 2008 6:24 pm
Re: calculations
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?
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
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