1

这是我在 PHP 上制作的一个简单函数。问题是它给出了完全出乎意料的输出。

从我的数据库中,它需要 $takings = 242 和 $cost = 81 所以它应该显示 242-81=161 作为答案。但它显示的是 242。可能是什么问题?

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

伙计们。这是页面的完整代码。在浏览器中打开页面时,它会显示:在此处输入图像描述

<?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;

?>
4

4 回答 4

2

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

应该

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

注意最后一个变量。

于 2012-10-30T18:21:59.947 回答
0

正如@dev-null-dweller 指出的那样,您可能实际上并没有传入(242, 81). 编写一个单元测试,当给定有效输入时,您会发现该函数按预期工作。

<?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));
  }
}
于 2012-10-30T18:13:34.187 回答
0
$movie_cost =$row['moviecost'].'million';
$movie_takings = $row['movietakings'].'million';
$movie_health = differences($row['movietakings'],$row['movie_cost']);

为了澄清我之前的回答:请注意“movie_cost”中存在下划线,而“moviecost”中没有下划线。

于 2012-10-30T18:18:25.360 回答
-1

我认为值没有分配给成本变量。所以检查以在函数中回显成本变量。

于 2012-10-30T18:18:58.387 回答