2017-12-27 197 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。 –

相關問題