简体   繁体   English

PHP分页-页面链接

[英]Pagination in PHP - page links

Thank you everyone who helped me yesterday with this new to me topic. 谢谢昨天在我这个新话题上为我提供帮助的每个人。 I've tried to write code myself, and it works fine for the first page. 我尝试自己编写代码,并且在第一页工作良好。 However, when I click any of the page links, I get this: 但是,当我单击任何页面链接时,都会得到以下信息:

The requested URL /headfirst_phpmysql/guitarwars/index.php&page=3 was not found on this server. 在此服务器上找不到请求的URL /headfirst_phpmysql/guitarwars/index.php&page=3。

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request. 此外,尝试使用ErrorDocument处理请求时遇到404 Not Found错误。

Here is my entire PHP: 这是我的整个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>

Where is your ? 你在? in query string? 在查询字符串? ;) ;)

And the long answer: 长答案:

The server is not requesting the index.php file but index.php&page=3 . 服务器不请求index.php文件,而是index.php&page=3 So it returns 404 as it's not finding anything;) 因此它返回404,因为它没有找到任何东西;)

The first argument suplied by GET is introduced with an ? GET提供的第一个参数以?开头? . If you have multiple arguments supplied, you use the & after that: 如果提供了多个参数,请在之后使用&

testurl.html?firstarg=123&second_arg=456

Use http_build_query to construct the query string. 使用http_build_query构造查询字符串。 See the docs @ http://www.php.net/manual/en/function.http-build-query.php 参见docs @ http://www.php.net/manual/zh/function.http-build-query.php

You need to append the result to http://example.com/? 您需要将结果附加到http://example.com/? (note the ? ) (请注意?

Try to use this: 尝试使用此:

(added ? before &) (在&之前添加?)

<!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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM