这是我项目中的 PHP 文件。我使用 href 来获取“获取电影详细信息”,因为它会转到另一个页面并显示结果。
<a href = "movie_details.php?movie_id=$movie_id" ...
我需要使用 ajax 请求获取这些详细信息并在同一页面中呈现响应(如果它在 div 内部更好)。
表1.php
<?php
$link = mysql_connect("localhost","root","") or die(mysql_error());
mysql_selectdb("MovieSite") or die(mysql_error());
$query = "SELECT movie_id, movie_name, movie_director, movie_leadactor FROM Movie ";
//"WHERE movie_year > 1990 ORDER BY movie_type";
$result = mysql_query($query, $link) or die(mysql_error());
$num_movies = mysql_num_rows($result);
$movie_header = <<<EOD
<h2><center>Movie Review Database</center></h2>
<table width = "70%" border = "1" cellpadding = "2" cellspacing = "2" align = "center">
<tr>
<th>Movie Title</th>
<th>Movie Director </th>
<th>Movie Lead Actor</th>
</tr>
EOD;
function get_director(){
global $movie_director;
global $director;
$query_d = "SELECT people_fullname FROM people WHERE people_id = '$movie_director'";
$results_d = mysql_query($query_d) or die(mysql_error());
$row_d = mysql_fetch_array($results_d);
extract($row_d);
$director = $people_fullname;
}
function get_leaderactor(){
global $movie_leadactor;
global $leadactor;
$query_l = "SELECT people_fullname FROM people WHERE people_id = '$movie_leadactor'";
$result_l = mysql_query($query_l);
$row_l = mysql_fetch_array($result_l);
extract($row_l);
$leadactor = $people_fullname;
}
$movie_details = '';
while ($row = mysql_fetch_array($result)){
$movie_id = $row['movie_id'];
$movie_name = $row['movie_name'];
$movie_director = $row['movie_director'];
$movie_leadactor = $row['movie_leadactor'];
get_director();
get_leaderactor();
$movie_details .= <<<EOD
<tr>
<td><a href = "movie_details.php?movie_id=$movie_id" title = "Find out more about $movie_name">$movie_name</a></td>
<td>$director</td>
<td>$leadactor</td>
</tr>
EOD;
}
$movie_details .= <<<EOD
<tr>
<td>Total : $num_movies Movies </td>
</tr>
EOD;
$movie_footer = "</table>";
$movie = <<< MOVIE
$movie_header;
$movie_details;
$movie_footer;
MOVIE;
echo $movie;
?>
movie_details.php
<?php
$link = mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db('MovieSite') or die("DB Select" . mysql_error());
function calculate_differences($takings, $cost){
$difference = $takings - $cost;
if ($difference < 0){
$difference = substr($difference, 1);
$font_color = 'red';
$profit_or_loss = "Rs. " . $difference . "M";
}elseif ($difference > 0){
$font_color = 'green';
$profit_or_loss = "Rs. " . $difference . "M";
}else {
$font_color = 'blue';
$profit_or_loss = "Broke even";
}
return "<font color = \"$font_color\"> ". $profit_or_loss . "</font>";
}
function get_director(){
global $movie_director;
global $director;
$query_d = "SELECT people_fullname FROM people WHERE people_id = '$movie_director'";
$results_d = mysql_query($query_d) or die(mysql_error());
$row_d = mysql_fetch_array($results_d);
extract($row_d);
$director = $people_fullname;
}
function get_leaderactor(){
global $movie_leadactor;
global $leadactor;
$query_l = "SELECT people_fullname FROM people WHERE people_id = '$movie_leadactor'";
$result_l = mysql_query($query_l);
$row_l = mysql_fetch_array($result_l);
extract($row_l);
$leadactor = $people_fullname;
}
$movie_query = "SELECT * FROM Movie WHERE movie_id = '" . $_GET['movie_id'] . "'";
$result = mysql_query($movie_query, $link) or die(mysql_error());
$movie_header = <<<EOD
<h2><center>Movie Review Database</center></h2>
EOD;
while ($row = mysql_fetch_array($result)){
//$movie_id = $row['movie_id'];
$movie_name = $row['movie_name'];
$movie_director = $row['movie_director'];
$movie_leadactor = $row['movie_leadactor'];
$movie_year = $row['movie_year'];
$movie_running_time = $row['movie_running_time'] . "Mins";
$movie_takings = $row['movie_taking'];
$movie_cost = $row['movie_cost'];
get_director();
get_leaderactor();
}
$movie_health = calculate_differences($movie_takings, $movie_cost);
$page_start = <<<EOD
<html>
<head>
<title>Details and Review for: $movie_name </title>
</head>
<body>
EOD;
$movie_table_headings =<<<EOD
<tr>
<th>Movie Title</th>
<th>Year of Release</th>
<th>Movie Director </th>
<th>Movie Lead Actor</th>
<th>Movie running Time</th>
<th>Movie Health</th>
</tr>
EOD;
$movie_details = <<<EOD
<table width = "70%" border = "1" cellpadding = "2" cellspacing = "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>
<br /><br />
EOD;
$page_end = <<<EOD
<a href = "table1.php">Home</a>
</body>
</html>
EOD;
$movie = <<< MOVIE
$page_start;
$movie_details;
$page_end;
MOVIE;
echo $movie;
mysql_close();