0

我正在制作一个动态博客,它从 SQL 数据库中提取帖子,我尝试添加一些记录集导航,但是当我按“下一步”时它不会翻阅帖子。这是此页面的 SQL 部分的编码:

mysql_select_db($database_blog, $blog);
$query_getArchives = "SELECT DISTINCT DATE_FORMAT(news.updated, '%M %Y') AS archive, DATE_FORMAT(news.updated, '%Y-%m') AS link FROM news ORDER BY news.updated DESC";
$getArchives = mysql_query($query_getArchives, $blog) or die(mysql_error());
$row_getArchives = mysql_fetch_assoc($getArchives);
$totalRows_getArchives = mysql_num_rows($getArchives);

mysql_select_db($database_blog, $blog);
$query_getRecent = "SELECT news.post_id, news.title FROM news ORDER BY news.updated DESC LIMIT 10";
$getRecent = mysql_query($query_getRecent, $blog) or die(mysql_error());
$row_getRecent = mysql_fetch_assoc($getRecent);
$totalRows_getRecent = mysql_num_rows($getRecent);

$maxRows_getDisplay = 10;
$pageNum_getDisplay = 0;
if (isset($_GET['pageNum_getDisplay'])) {
  $pageNum_getDisplay = $_GET['pageNum_getDisplay'];
}
$startRow_getDisplay = $pageNum_getDisplay * $maxRows_getDisplay;

mysql_select_db($database_blog, $blog);
$query_getDisplay = "SELECT news.title, news.pre,  DATE_FORMAT(news.updated, '%M %e, %Y') AS formatted FROM news ORDER BY news.updated DESC";
$query_limit_getDisplay = sprintf("%s LIMIT %d, %d", $query_getDisplay, $startRow_getDisplay, $maxRows_getDisplay);
$getDisplay = mysql_query($query_limit_getDisplay, $blog) or die(mysql_error());
$row_getDisplay = mysql_fetch_assoc($getDisplay);

if (isset($_GET['totalRows_getDisplay'])) {
  $totalRows_getDisplay = $_GET['totalRows_getDisplay'];
} else {
  $all_getDisplay = mysql_query($query_getDisplay);
  $totalRows_getDisplay = mysql_num_rows($all_getDisplay);
}
$totalPages_getDisplay = ceil($totalRows_getDisplay/$maxRows_getDisplay)-1;

$queryString_getDisplay = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_getDisplay") == false && 
        stristr($param, "totalRows_getDisplay") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_getDisplay = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_getDisplay = sprintf("&totalRows_getDisplay=%d%s", $totalRows_getDisplay, $queryString_getDisplay);



$var1_getDisplay2 = "-1";
if (isset($_GET['archive'])) {
  $var1_getDisplay2 = $_GET['archive'];
  $query_getDisplay = sprintf("SELECT news.title, news.blog_entry, DATE_FORMAT(news.updated, '%%M %%e, %%Y') AS formatted FROM news WHERE DATE_FORMAT(news.updated, '%%Y-%%m') = %s ORDER BY news.updated DESC", GetSQLValueString($var1_getDisplay2, "text"));
} elseif (isset($_GET['post_id'])) {
  $var2_getDisplay3 = $_GET['post_id'];
  $query_getDisplay = sprintf("SELECT news.title, news.blog_entry, DATE_FORMAT(news.updated, '%%M %%e, %%Y') AS formatted FROM news WHERE news.post_id = %s", GetSQLValueString($var2_getDisplay3, "int"));
} else {
  $query_getDisplay = "SELECT news.title, news.blog_entry, DATE_FORMAT(news.updated, '%M %e, %Y') AS formatted FROM news ORDER BY news.updated DESC LIMIT 2";
}
$getDisplay = mysql_query($query_getDisplay, $blog) or die(mysql_error());
$row_getDisplay = mysql_fetch_assoc($getDisplay);
$totalRows_getDisplay = mysql_num_rows($getDisplay);

这是记录集代码:

  <div id="blog_posts_container">
  <h3><?php echo $row_getDisplay['title']; ?></h3>
  <p class="update">Posted on <?php echo $row_getDisplay['formatted']; ?></p>
  <div class="blog_entry" id="blog_posts">
    <p><?php echo nl2br($row_getDisplay['blog_entry']); ?></p>
  </div></div>
  <?php } while ($row_getDisplay = mysql_fetch_assoc($getDisplay)); ?>
  <div id="blog_posts_container">
    <table border="0">
      <tr>
        <td><?php if ($pageNum_getDisplay > 0) { // Show if not first page ?>
            <a href="<?php printf("%s?pageNum_getDisplay=%d%s", $currentPage, 0, $queryString_getDisplay); ?>">First</a>
            <?php } // Show if not first page ?></td>
        <td><?php if ($pageNum_getDisplay > 0) { // Show if not first page ?>
            <a href="<?php printf("%s?pageNum_getDisplay=%d%s", $currentPage, max(0, $pageNum_getDisplay - 1), $queryString_getDisplay); ?>">Previous</a>
            <?php } // Show if not first page ?></td>
        <td><?php if ($pageNum_getDisplay < $totalPages_getDisplay) { // Show if not last page ?>
            <a href="<?php printf("%s?pageNum_getDisplay=%d%s", $currentPage, min($totalPages_getDisplay, $pageNum_getDisplay + 1), $queryString_getDisplay); ?>">Next</a>
            <?php } // Show if not last page ?></td>
        <td><?php if ($pageNum_getDisplay < $totalPages_getDisplay) { // Show if not last page ?>
            <a href="<?php printf("%s?pageNum_getDisplay=%d%s", $currentPage, $totalPages_getDisplay, $queryString_getDisplay); ?>">Last</a>
            <?php } // Show if not last page ?></td>
      </tr>
    </table>
  </div>

它看起来与记录集导航栏工作的旧页面相同,但由于某种原因它现在无法正常工作

4

1 回答 1

1

如果每次单击链接时都获得相同的 2 篇博文,那是因为您没有M更改LIMIT [M,] N. 尝试将其更改为 -

} else {
$query_getDisplay = "SELECT news.title, news.blog_entry, DATE_FORMAT(news.updated, '%M %e, %Y') AS formatted FROM news ORDER BY news.updated DESC LIMIT ".$pageNum_getDisplay.", 2";
}
于 2012-12-27T04:55:14.273 回答