繁体   English   中英

PHP分页-页面链接

[英]Pagination in PHP - page links

谢谢昨天在我这个新话题上为我提供帮助的每个人。 我尝试自己编写代码,并且在第一页工作良好。 但是,当我单击任何页面链接时,都会得到以下信息:

在此服务器上找不到请求的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>

你在? 在查询字符串? ;)

长答案:

服务器不请求index.php文件,而是index.php&page=3 因此它返回404,因为它没有找到任何东西;)

GET提供的第一个参数以?开头? 如果提供了多个参数,请在之后使用&

testurl.html?firstarg=123&second_arg=456

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

您需要将结果附加到http://example.com/? (请注意?

尝试使用此:

(在&之前添加?)

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