1

Here is a simple function which i made on PHP. The problem is that it is giving totally unexepected output.

From my database it takes $takings = 242 and $cost = 81 so it should show 242-81=161 as answer. but instead it is showing 242. What can be the problem ?

function differences($takings,$cost)
{
$difference = $takings - $cost;

if($difference < 0)
{
    $color = red;
    $difference = '$'.$difference.'million';
}

elseif($difference > 0)
{
   $color = green;
   $difference = '$'.$difference.='million';
}   

elseif($difference = 0)
{
   $color = blue;
   $difference = "broken even ";
}

return '<span style="color:'.$color.';">'.$difference.'</span>';
}

Guys. here is the complete code for the page. When open the page in the browser it shows this : enter image description here

<?php

function getdirector($director)
{
global $db;
$query = 'select name from peopledb where peopleid = '.$director;
$result = mysql_query($query) or die(mysql_error($db));
$row = mysql_fetch_assoc($result);
extract($row);
return $name;
}

function getactor($actor)
{
$query = 'select name from peopledb where peopleid = '.$actor;
$result = mysql_query($query) or die(mysql_error($db));
$row = mysql_fetch_assoc($result);
extract($row);
return $name;
}


function getmovietype($type)
{
$query = 'select movietype from movietype where movieid = '.$type;
$result = mysql_query($query) or die(mysql_error($db));
$row = mysql_fetch_assoc($result);
extract($row);
return $movietype;
}


function differences($takings,$cost)
{
$difference = $takings - $cost;

if($difference < 0)
{
    $color = red;
    $difference = '$'.$difference.'million';
}

elseif($difference > 0)
{
   $color = green;
   $difference = '$'.$difference.='million';
}   

elseif($difference = 0)
{
   $color = blue;
   $difference = "broken even ";
}

return '<span style="color:'.$color.';">'.$difference.'</span>';
}


$abc = $_GET['movieid'];


$db = mysql_connect('localhost','root','saw123') or die('Connection error');
mysql_select_db("moviesite",$db) or die(mysyql_error($db));
$query = "select moviename,releaseyear,director,actor,type,movierunningtime,moviecost,movietakings from movie where movieid = ". $abc;




$result = mysql_query($query,$db) or die(mysql_error($db));

$row = mysql_fetch_assoc($result);
$movie_name = $row['moviename'];
$movie_director = getdirector($row['director']);
$movie_releaseyear = $row['releaseyear'];
$movie_actor = getactor($row['actor']);
$movie_type = getmovietype($row['type']);
$movie_runningtime = $row['movierunningtime'].' minutes';
$movie_cost =$row['moviecost'].'million';
$movie_takings = $row['movietakings'].'million';
$movie_health = differences($row['movietakings'],$row['movie_cost']);



echo <<<ENDHTML
<html>
 <head>
   <title> Details and Reviews for the movies for $movie_name </title>
  </head>
<body>
<div style="text-align: center;">
<h2>$movie_name</h2>
<h3> details </h3>
<table cellpadding = "1" cellspacing = "4"
style = "width = 80% ;margin-left: auto;margin-right: auto;";>
<tr>
 <td><strong>Title</strong></strong></td>
 <td>$movie_name</td>
</tr>

<tr>
 <td><strong>release date</strong></strong></td>
 <td>$movie_releaseyear</td>
</tr>
<tr>

<tr>
 <td><strong>movie director</strong></strong></td>
 <td>$movie_director</td>
</tr>
<tr>

<tr>
 <td><strong>Lead Actor</strong></strong></td>
 <td>$movie_actor</td>
</tr>
<tr>

<tr>
 <td><strong>Running time</strong></strong></td>
 <td>$movie_runningtime</td>
</tr>
<tr>


<tr>
 <td><strong>Cost</strong></strong></td>
 <td>$movie_cost</td>
</tr>
<tr>


<tr>
 <td><strong>Takings</strong></strong></td>
 <td>$movie_takings</td>
</tr>
<tr>

<tr>
 <td><strong>Health</strong></strong></td>
 <td>$movie_health</td>
</tr>
<tr>


</table>
</div>

</body>
</html>
ENDHTML;



$table = <<<ENDHTML
<div style="text-align: center;">
<h2>The Ultimate movie database</h2>
<table border='1' cellpadding='1' cellspacing='2'
       style="width: 70%; margin-left: auto; margin-right: auto;">

<tr>
<th>Movie ID </th>
<th>Movie title</th>
<th>year of release </th>
<th>Movie director</th>
<th>Movie Actor</th>
<th>Movie type</th>
</tr>
ENDHTML;

?>
6
  • 1
    Have you tried outputting the $takings and $cost variables? If you have 242, and you think you're subtracting 61, but the result is still 242, you obviously aren't subtracting what you think you're subtracting. Presumably, $cost is 0, which means the error in logic lies outside this function. A little debugging would probably tell you what is wrong. Commented Oct 30, 2012 at 18:09
  • var_dump or output the parameters, just before the function call. Commented Oct 30, 2012 at 18:11
  • 1
    Show us how are you fetching data and calling this function, most probably you have typo in variable name and you are passing null instead fo real $cost Commented Oct 30, 2012 at 18:11
  • 2
    $difference = 0 should be $difference == 0 also, but I think that's unrelated to your issue. Commented Oct 30, 2012 at 18:15
  • Use some IDE (like NetBeans PHP) to avoid mistakes like @zdhickman pointed (assign instead of comparison). Commented Oct 30, 2012 at 18:22

4 Answers 4

2

$movie_health = differences($row['movietakings'],$row['movie_cost']);

should be

$movie_health = differences($row['movietakings'],$row['moviecost']);

Notice the last variable.

Sign up to request clarification or add additional context in comments.

Comments

0
$movie_cost =$row['moviecost'].'million';
$movie_takings = $row['movietakings'].'million';
$movie_health = differences($row['movietakings'],$row['movie_cost']);

To clarify my earlier answer: note the presences of an underscore in 'movie_cost', and the lack of one in 'moviecost'.

1 Comment

Also, the code needs help in a lot of other ways; for example, using the soft-deprecated "mysql_" API; using extract() blindly; plenty of opportunities for SQL injection; using nested queries intead of joins...the list could go on. Maybe try [finding a mentor]( phpmentoring.org) to help you with some of this stuff.
0

As @dev-null-dweller pointed out, you're likely not actually passing in (242, 81). Write a unit test and you'll find the function works as you expect when given valid inputs.

<?php

function differences($takings,$cost)
{
  $difference = $takings - $cost;

  if($difference < 0) {
    $color = 'red';
    $difference = '$'.$difference.'million';
  } elseif($difference > 0) {
   $color = 'green';
   $difference = '$'.$difference.='million';
  } elseif($difference == 0) {
   $color = 'blue';
   $difference = "broken even ";
  }

  return '<span style="color:'.$color.';">'.$difference.'</span>';
}

class DifferencesTest extends PHPUnit_Framework_TestCase
{
  public function testValidInput() {
    $this->assertEquals('<span style="color:green;">$161million</span>', differences(242,81));
  }

  public function testInvalidInput() {
    $this->assertEquals('<span style="color:green;">$242million</span>', differences(242,NULL));
  }
}

2 Comments

This is not an answer, this is a wild guess. Though likely as it may be, without all the information it's just an assumption and should be up there in the comments.
The OP's question was "What can be the problem ?", and this addresses the most likely problem. It's easy to repro by passing in (242, NULL).
-1

I think value are not assigned to cost variable.so check to echo cost variable in function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.