【问题标题】:How to pass article id to pangination page with ajax?如何使用 ajax 将文章 id 传递给 pangination 页面?
【发布时间】:2021-01-16 01:43:26
【问题描述】:

我正在尝试为使用 ajax 列出文章的类别结果页面进行分页。 我使用 where 子句,需要将文章 id 传递给 ajax 页面。

我的代码:ajax.php

include_once '../db.php'; 
include_once '../class/Articles.php'; 
$ArticleHandler = new Articles($pdo);

if (!(isset($_GET['pageNumber']))) {
    $pageNumber = 1;
} else {
    $pageNumber = $_GET['pageNumber'];
}

$perPageCount = 5;
$catid = $_POST['article_id'];

$sql = "SELECT * FROM posts WHERE pcat = ?";
$posts = $ArticleHandler->getPosts($sql, [$catid])->fetchAll();
if ($posts !== '0') {
   $rowCount = $ArticleHandler->getPosts("SELECT count(*) FROM posts")->fetchColumn();
}

$pagesCount = ceil($rowCount/$perPageCount);

$lowerLimit = ($pageNumber - 1) * $perPageCount;

$sql = "SELECT * FROM posts WHERE pcat = :pcat ORDER BY pid DESC LIMIT :offset, :limit"; 
$results = $ArticleHandler->getPosts($sql, array(':pcat'=>$catid, ':offset'=>$lowerLimit, ':limit'=>$perPageCount))->fetchAll();

?>

<table class="table table-hover table-responsive">
<tr>
  <th align="center">Name</th>
  <th align="center">Experience<br>(in years)
  </th>
  <th align="center">Subject</th>
</tr>
<?php foreach ($results as $data) { ?>
<tr>
  <td align="left"><?php echo $data['ptitle'] ?></td>
</tr>
<?php
}
?>
</table>

<div style="height: 30px;"></div>
<table width="50%" align="center">
<tr>

  <td valign="top" align="left"><?php echo $catid; ?></td>


  <td valign="top" align="center">

<?php
for ($i = 1; $i <= $pagesCount; $i ++) {
if ($i == $pageNumber) {
?>
   <a href="javascript:void(0);" class="current"><?php echo $i ?></a>
<?php
} else {
?>
   <a href="javascript:void(0);" class="pages"
      onclick="showRecords('<?php echo $perPageCount;  ?>', '<?php echo $i; ?>');"><?php echo $i ?></a>
<?php
} 
} 
?>
</td>
  <td align="right" valign="top">
  Page <?php echo $pageNumber; ?> of <?php echo $pagesCount; ?>
</td>
</tr>
</table>

还有index.php

<div id="results"></div>
<div id="loader"></div>

Ajax 代码:

function showRecords(perPageCount, pageNumber) {
  $.ajax({
      type: "GET",
      url: "modules/ajax.php",
      data: "pageNumber=" + pageNumber,
      cache: false,
      beforeSend: function() {
          $('#loader').html('<img src="loader.png" alt="reload" width="20" height="20" style="margin-top:10px;">');
      
      },
      success: function(html) {
          $("#results").html(html);
          $('#loader').html(''); 
      }
  });
}

$(document).ready(function() {
  showRecords(10, 1);
});

我的尝试:我在 index.php 中添加了一个带有 artcile id 的 div,并使用 attr 将其传递给 ajax,但无法将其传递给页面。

【问题讨论】:

    标签: php jquery ajax pagination


    【解决方案1】:

    尝试将文章 ID 与您在 GET 请求中传递的 pageNumber 一起传递。

    data: {pageNumber : YOUR_PAGE_NUMBER, articleId: ARTICLE_ID}
    

    不要忘记将 articleId 作为第三个参数传递给函数。

    【讨论】:

    • 我试过 function showRecords(perPageCount, pageNumber, article_id) { data: "pageNumber=" + pageNumber + "&amp;article_id" + article_id, 没有意义,同样的错误 undefined id。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-08
    • 1970-01-01
    • 1970-01-01
    • 2014-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多