谢谢大家昨天帮我解决这个新的话题。 我自己尝试编写代码,它适用于第一页。 但是,当我点击任何页面链接时,我得到了这个:
在此服务器上找不到请求的URL /headfirst_phpmysql/guitarwars/index.php&page=3。
此外,尝试使用ErrorDocument处理请求时遇到404 Not Found错误。
这是我的整个PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Guitar Wars - High Scores</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<h2>Guitar Wars - High Scores</h2>
<p>Welcome, Guitar Warrior, do you have what it takes to crack the high score list? If
so, just <a href="addscore.php">add your own score</a>.</p>
<hr />
<?php
// This function builds navigational page links based on the current page and the number of pages
function generate_page_links($cur_page, $num_pages) {
$page_links = '';
// If this page is not the first page, generate the "Previous" link
if ($cur_page > 1) {
$page_links .= '<a href = "' . $_SERVER['PHP_SELF'] .
'&page=' . ($cur_page - 1) . '"><-</a>';
}
else {
$page_links .= '<- ';
}
// Loop through the pages generating the page number links
for ($i = 1; $i <= $num_pages; $i++) {
if ($cur_page == $i) {
$page_links .= '' . $i;
}
else {
$page_links .= '<a href = "' . $_SERVER['PHP_SELF'] .
'&page=' . $i . '"> ' . $i . '</a>';
}
}
// If this page is not the last page, generate the "Next" link
if ($cur_page < $num_pages) {
$page_links .= '<a href = "' . $_SERVER['PHP_SELF'] .
'&page=' . ($cur_page + 1) . '">-></a>';
}
else {
$page_links .= '->';
}
return $page_links;
}
// Calculate pagination information
$cur_page = isset($_GET['page']) ? $_GET['page'] : 1;
// Number of results per page
$results_per_page = 5;
// Compute the number of the first row on the page
$skip = (($cur_page - 1) * $results_per_page);
require_once('appvars.php');
require_once('connectvars.php');
// Connect to the database
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
// Retrieve the score data from MySQL
$query = "SELECT * FROM guitarwars WHERE approved = 1 ORDER BY score DESC, date ASC";
$data = mysqli_query($dbc, $query);
$total = mysqli_num_rows($data);
$num_pages = ceil($total / $results_per_page);
// Query again to get just the subset of results
$query = $query . " LIMIT $skip, $results_per_page";
$data = mysqli_query($dbc, $query);
// Loop through the array of score data, formatting it as HTML
echo '<table>';
$i = 0;
while ($row = mysqli_fetch_array($data)) {
// Display the score data
if ($i == 0) {
echo '<tr><td colspan="2" class="topscoreheader">Top Score: ' . $row['score'] . '</td></tr>';
}
echo '<tr><td class="scoreinfo">';
echo '<span class="score">' . $row['score'] . '</span><br />';
echo '<strong>Name:</strong> ' . $row['name'] . '<br />';
echo '<strong>Date:</strong> ' . $row['date'] . '</td>';
if (is_file(GW_UPLOADPATH . $row['screenshot']) && filesize(GW_UPLOADPATH . $row['screenshot']) > 0) {
echo '<td><img src="' . GW_UPLOADPATH . $row['screenshot'] . '" alt="Score image" /></td></tr>';
}
else {
echo '<td><img src="' . GW_UPLOADPATH . 'unverified.gif' . '" alt="Unverified score" /></td></tr>';
}
$i++;
}
echo '</table>';
// Generate navigational page links if we have more than one page
if ($num_pages > 1) {
echo generate_page_links($cur_page, $num_pages);
}
mysqli_close($dbc);
?>
</body>
</html>
答案 0 :(得分:1)
查询字符串中的?
在哪里? ;)
答案很长:
服务器未请求index.php
文件,而是index.php&page=3
。所以它返回404,因为它没有找到任何东西;)
答案 1 :(得分:1)
GET提供的第一个参数是?
引入的。如果您提供了多个参数,则在之后使用&
:
testurl.html?firstarg=123&second_arg=456
答案 2 :(得分:1)
使用http_build_query
构造查询字符串。请参阅文档@ http://www.php.net/manual/en/function.http-build-query.php
您需要将结果附加到http://example.com/?
(请注意?
)
答案 3 :(得分:0)
尝试使用它:
(添加?之前和之后)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Guitar Wars - High Scores</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<h2>Guitar Wars - High Scores</h2>
<p>Welcome, Guitar Warrior, do you have what it takes to crack the high score list? If
so, just <a href="addscore.php">add your own score</a>.</p>
<hr />
<?php
// This function builds navigational page links based on the current page and the number of pages
function generate_page_links($cur_page, $num_pages) {
$page_links = '';
// If this page is not the first page, generate the "Previous" link
if ($cur_page > 1) {
$page_links .= '<a href = "' . $_SERVER['PHP_SELF'] .
'?&page=' . ($cur_page - 1) . '"><-</a>';
}
else {
$page_links .= '<- ';
}
// Loop through the pages generating the page number links
for ($i = 1; $i <= $num_pages; $i++) {
if ($cur_page == $i) {
$page_links .= '' . $i;
}
else {
$page_links .= '<a href = "' . $_SERVER['PHP_SELF'] .
'?&page=' . $i . '"> ' . $i . '</a>';
}
}
// If this page is not the last page, generate the "Next" link
if ($cur_page < $num_pages) {
$page_links .= '<a href = "' . $_SERVER['PHP_SELF'] .
'?&page=' . ($cur_page + 1) . '">-></a>';
}
else {
$page_links .= '->';
}
return $page_links;
}
// Calculate pagination information
$cur_page = isset($_GET['page']) ? $_GET['page'] : 1;
// Number of results per page
$results_per_page = 5;
// Compute the number of the first row on the page
$skip = (($cur_page - 1) * $results_per_page);
require_once('appvars.php');
require_once('connectvars.php');
// Connect to the database
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
// Retrieve the score data from MySQL
$query = "SELECT * FROM guitarwars WHERE approved = 1 ORDER BY score DESC, date ASC";
$data = mysqli_query($dbc, $query);
$total = mysqli_num_rows($data);
$num_pages = ceil($total / $results_per_page);
// Query again to get just the subset of results
$query = $query . " LIMIT $skip, $results_per_page";
$data = mysqli_query($dbc, $query);
// Loop through the array of score data, formatting it as HTML
echo '<table>';
$i = 0;
while ($row = mysqli_fetch_array($data)) {
// Display the score data
if ($i == 0) {
echo '<tr><td colspan="2" class="topscoreheader">Top Score: ' . $row['score'] . '</td></tr>';
}
echo '<tr><td class="scoreinfo">';
echo '<span class="score">' . $row['score'] . '</span><br />';
echo '<strong>Name:</strong> ' . $row['name'] . '<br />';
echo '<strong>Date:</strong> ' . $row['date'] . '</td>';
if (is_file(GW_UPLOADPATH . $row['screenshot']) && filesize(GW_UPLOADPATH . $row['screenshot']) > 0) {
echo '<td><img src="' . GW_UPLOADPATH . $row['screenshot'] . '" alt="Score image" /></td></tr>';
}
else {
echo '<td><img src="' . GW_UPLOADPATH . 'unverified.gif' . '" alt="Unverified score" /></td></tr>';
}
$i++;
}
echo '</table>';
// Generate navigational page links if we have more than one page
if ($num_pages > 1) {
echo generate_page_links($cur_page, $num_pages);
}
mysqli_close($dbc);
?>
</body>
</html>