将PHP分页转换为jQuery?

时间:2010-04-07 20:51:52

标签: php jquery class pagination

嘿,我一直试图让我this pagination class使用更多的ajaxy - 这意味着当我点击页面编号如页[2]时数据加载,但我想加载数据无需转到其他页面(后台的HTTP请求,没有页面重新加载)。

对于php和jquery都是新手,我对如何实现这个结果有点不确定,特别是在使用php类时。

这就是主页面的样子:

<?php
$categoryId=$_GET['category'];
echo $categoryId;
?>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
<script type="text/javascript" src="jquery_page.js"></script>

<?php
    //Include the PS_Pagination class
    include('ps_pagination.php');
    //Connect to mysql db
    $conn = mysql_connect('localhost', 'root', 'root');
    mysql_select_db('ajax_demo',$conn);
    $sql = "select * from explore where category='$categoryId'";
    //Create a PS_Pagination object
    $pager = new PS_Pagination($conn, $sql, 3, 11, 'param1=value1&param2=value2');
    //The paginate() function returns a mysql
    //result set for the current page
    $rs = $pager->paginate();
    //Loop through the result set

echo "<table width='800px'>";

    while($row = mysql_fetch_assoc($rs)) {            

                echo "<tr>";
                    echo"<td>";
                        echo $row['id'];
                    echo"</td>";

                    echo"<td>";
                        echo $row['site_description'];
                    echo"</td>";

                    echo"<td>";
                        echo $row['site_price'];
                    echo"</td>";
                echo "</tr>";

    }
echo "</table>";

        echo "<ul id='pagination'>";

            echo "<li>";
            //Display the navigation
            echo $pager->renderFullNav();
            echo "</li>";

        echo "</ul>";

?>

<div id="loading" ></div>
<div id="content" ></div>

我是否需要对课程的这一部分做些什么?,如上所示:

$pager = new PS_Pagination($conn, $sql, 3, 11, 'param1=value1&param2=value2');

或者这个?:

echo $pager->renderFullNav();

我对jquery的了解不多,但我想我会像以下那样开始:

$("#pagination li").click(function() {

然后加载一些东西......

我不是。对此的任何帮助都会很棒。感谢。

1 个答案:

答案 0 :(得分:1)

我不确定如何使用该类,它似乎有点棘手,因为您调用ajax来检索数据的脚本需要访问当前的PS_pagination实例。

虽然没有上课,但这不会太棘手。

您需要一个php脚本来实际返回数据,该数据包含每页的记录数和当前页码。在这个脚本中,我返回html而不是返回数据。所以我从数据库中获取数据,然后生成表。这意味着我在ajax成功时必须做的就是用我从这个脚本中获得的新html替换当前的功能。这是一个例子..

//Current Page Number 
$page_num = isset($_GET['page_number']) ?  mysql_real_escape_string($_GET['page_number']) : 1;

//Number of records to show on each page 
$num_records = isset($_GET['num_records_pp']) ?  mysql_real_escape_string($_GET['num_records_pp']) : 10;

//Row to start collecting data from 
$start_row = $num_records * ($page_num - 1);

//String to store html to return 
$return_html = '';

//SQL Query 
$sql = mysql_query("SELECT * FROM my_table LIMIT $start_row, $num_records");

//Query success 
if($sql) {      
    //Construct html for table  
    $return_html = "<table width='800px'>";         

    while($row = mysql_fetch_array($sql)    {       

      $return_html .= "<tr>";       
      $return_html .= "<td>" . $row['id'] . "</td>";        
      $return_html .= "<td>" . $row['site_description'] . "</td>";
      $return_html .= "<td>" . $row['site_price'] . "</td>"; 
      $return_html .= "</tr>";

    }       
    $return_html .= "</table>";

//Query Failed 
} else {

    $return_html = "<p class='error'>Error Fetching Data</p>";

}

return $return_html;

然后你只需通过ajax发出一个get请求并传递页码和你想要的行数。

$.get("get_data.php", { page_number: 1, num_records_pp: 20 },
   function(data){
     $('div#my_table').html(data);
   });

因此,这个查询假设您有一个id为“my_table”的div,其中包含您的表格,然后将其替换为您所请求数据的新表格。

这段代码只是为了给你一个jist,所以我可能会有一些错误,但希望它有所帮助。