2017-12-27 1006 views
0

我想添加分页,即使它的代码似乎是正确的,该页面只显示前10个结果(10个结果每页我想)。正如您在下面看到的,该问题似乎是在$_GET方法中创建的。

这里是AJAX:
PHP和Ajax分页。为什么GET方法总是返回空值?

function displayAllTopics() { 
     $.ajax({ 
      url: "requests.php", 
      type: "GET", 
      data: { 
      keyPagination: "displayAllTopics" 
      }, 
      dataType: "json", 
      success: function(response) { 
      $("#topicArea").append(response.response_message); 
      $("#pagination").append(response.pagination); 
      } 
     }); 
     } 

这里是requests.php文件:

if(isset($_GET['keyPagination'])) { 
if($_GET['keyPagination'] == "displayAllTopics") { 
    $sql = "SELECT * FROM travel"; 
    $result = mysqli_query($conn, $sql); 
    $count_topics = mysqli_num_rows($result); 
    if($count_topics < 1) { 
    exit('There are no topics yet.'); 
    } 

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

    //results per page 
    $results_per_page = 10; 
    //number of pages 
    $offset = ($page-1) * $results_per_page; 
    $number_of_pages = ceil($count_topics/$results_per_page); 

    $sql_pagination = "SELECT * FROM travel LIMIT $offset, $results_per_page"; 
    $results_pagination = mysqli_query($conn, $sql_pagination); 
    $response_message = ""; 
    $pagination = ""; 
    while($row = mysqli_fetch_assoc($results_pagination)) { 
    $user_id = $row['user_id']; 
    $travel_subject_id = $row['travel_subject_id']; 
    $travel_subject_date = $row['travel_subject_date']; 
    $travel_title = $row['travel_subject_title']; 
    $travel_body = $row['travel_subject_body']; 
    $travel_comments_count = $row['travel_comments_count']; 
    $travel_views = $row['travel_subject_views']; 
    $travel_subject_date_format = date('j M y H:i', strtotime($travel_subject_date)); 
    $sql_user = "SELECT * FROM forum_users WHERE user_id='$user_id'"; 
    $result_user = mysqli_query($conn, $sql_user); 
    if($row_user = mysqli_fetch_assoc($result_user)) { 
    $username = $row_user['username']; 
    $response_message .= "<tr> 
    <td><a href='travel_subject.php?id=".$travel_subject_id."' onclick='countview(".$travel_subject_id.");'><img src='topic.png' class='img_first'>$travel_title</a></td> 
    <td><img src='comment.png' class='img_second'><p>$travel_comments_count</p></td> 
    <td><a href='profile_user.php?id=".$user_id."'><img src='avatar.png' class='img_third'>$username</a></td> 
    <td><p><img src='views.png' class='img_four'>$travel_views</p></td> 
    <td><p>$travel_subject_date_format</p></td> 
    </tr>"; 
    } 
} 
for ($i = 1; $i <= $number_of_pages; $i++) { 
    $pagination .= "<a href='forum_index.php?page=".$i."&limit=".$offset."'>$i</a>"; 
} 

$data = array('response_message' => $response_message, 'pagination' => $pagination); 
echo json_encode($data); 

exit(); 
} 
} 

注意:变量$page始终为1,所以似乎不能设置$_GET['page']和结果是,下面的页面总是显示前10个结果。这是为什么?

,这里是PHP文件,我想显示结果:

<div class='display-subjects'> 
    <table class='display-subjects-table' cols='5'> 
<tbody id="topicArea"> 
    </tbody> 
</table> 
</div> 
<div id='pagination'> 
    <?php 
    if(isset($_GET['page'])) { 
    $page = $_GET['page']; 
    } 
    else { 
    $page = 1; 
    } 
?> 
</div> 

哪里错了一部分?提前致谢。

+0

'data:'选项中没有'page:'参数。你期望'$ _GET ['page']'来自哪里? – Barmar

回答

1

displayAllTopics()需要采取页码作为参数,然后通过它在data选项。

function displayAllTopics(pageNum) { 
    $.ajax({ 
     url: "requests.php", 
     type: "GET", 
     data: { 
     keyPagination: "displayAllTopics", 
     page: pageNum 
     }, 
     dataType: "json", 
     success: function(response) { 
     $("#topicArea").append(response.response_message); 
     $("#pagination").append(response.pagination); 
     } 
    }); 
    } 
+0

尽管我尽量不工作.. –

+0

你是对的。问题解决了。之前没有工作,因为我没有声明变量pageNum。 –