簡體   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