-1

我有一个数据库。当我获取数据时,它有很多行。我想打破这个结果。我阅读了许多有关分页的教程,但不了解如何进行分页。以下是我的代码-

<?php
                if(isset($_GET['search_profile'])){
                $gender = $_GET['Gender'];
                $religion = $_GET['selected_religion'];
                $caste = $_GET['selected_caste'];
                $marital_status = $_GET['marital_status'];
                $lage = $_GET['lage'];
                $hage = $_GET['hage'];

                $current_date = date('Y');
                            $hage1 = date('Y') - $hage;
                //echo $hage1;
            //  echo "<br>";
                $lage1 = date('Y') - $lage;
                //echo $lage1;



                $qq = "SELECT * FROM members WHERE yyyy BETWEEN '$hage1' AND '$lage1' AND gender='$gender' 
                AND religion LIKE '%$religion%'
                AND caste LIKE '%$caste%'
                AND marital_status LIKE '%$marital_status%'";

$profile_query_result = mysql_query($qq, $dbc)
or die('Error1'.mysql_error());


                }
                ?>
4

3 回答 3

0

I just finished my first pagination script the other day. It wasn't that hard. Honestly I would be happy to help you understand the logic of it but I am not going to code it for you - I also believe this topic has been covered on these boards and this is a possible duplicate.

How much PHP do you know? The logic behind your pagination script (assuming the data is coming from a database) is something like this:

You need at least 2 database queries -Your first query will tell you how many results there are and your second query will actually provide the results.

There are a few variables you will need - You will need to know the number of items. You will also need to know the limit - that is, how many items can appear on each page. You find the number of pages by dividing the limit from the number of items (# of items / limit per page )

The Display HTML - To create the HTML of the pagination I used a series of if statements. Kind of like the following pseudo code:

if ( isset( $_GET['page'] ) ) {
       $page = preg_replace( '/[^0-9]/', '', $_GET['page'] );
}
else {
       $page = 1;
}


/* the following code will not work
   with the identity (===) operator 
   because when coming from get $page
is a string not an int */

if ( $current_page == 1 ) {

     $page2 = $page + 1;
     $page3 = $page + 2;
     $page4 = $page + 3;


     $pagination .= "<div>Pg $page</div>";

    // if theres a second page, display page 2
    if ( $num_of_posts > ( $limit * 1 ) ) {
         $pagination .= "<a>$page2 &gt;</a>";
    }

    // if there is a 3rd page
    if ( $num_of_posts > ( $limit * 2 ) ) {
         $pagination .= "<a>$page3 &gt;</a>";
    }

    // if there is a 4th page
    if ( $num_of_posts > ( $limit * 3 ) ) {
         $pagination .= "<a>$page4 &gt;</a>";
    }

    // if there is more than 4 pages
    if ( ($page4 != $last_page && $page3 != $last_page ) {
         $pagination .= "<a href='" . $last_page . "'>&gt; &gt;</a>";
    }
}

This process will need to be repeated. How much you repeat it is entirely up to you. You can rewrite the if / elseif statements as a switch statement, I personally do not use the switch statement in web programming.

You can take the same approach to ( $page == 2 ), ( $page == $last_page ), ( $page == ( $last_page - 1 ). You can go overboard. This simply ensures that the HTML output will be appropriate if there is only one or two pages of content.

The Second Query - The second query will fetch the results from the database. The limit will be ( ( $page - 1 ) * $limit_per_page ), $limit_per_page

ie:

 $limit = ( $page - 1) * $limit_per_page
   $sql= "SELECT post, user, id, etc, whatever
         FROM table
         WHERE this = that
         ORDER BY foobarz ASC[/DESC]
         LIMIT ".$limit.",".$limit_per_page;

I hope this explains it enough for you to build it! Good luck! Let me know if you need any more explaining!

于 2013-02-07T23:41:35.567 回答
0

您可以使用 LIMIT 关键字告诉 MySQL 要获取多少条记录,并指定起始偏移量

 SELECT * 
   FROM members 
   WHERE yyyy BETWEEN '$hage1' AND '$lage1' 
     AND gender='$gender' 
     AND religion LIKE '%$religion%'
     AND caste LIKE '%$caste%'     
     AND marital_status LIKE '%$marital_status%'
  LIMIT 0,100

上面的 SQL 查询会返回前 100 条记录,从第一个开始

如果您想要接下来的 100 条(即从记录 #101 开始的 100 条记录),那么您可以使用LIMIT 100, 100; 然后为接下来的 100LIMIT 200,100等等..

当然,如果您希望通过 PHP 控制页码,您可以使用参数而不是硬编码值,如下所示:

 SELECT * 
   FROM members 
   WHERE yyyy BETWEEN '$hage1' AND '$lage1' 
     AND gender='$gender' 
     AND religion LIKE '%$religion%'
     AND caste LIKE '%$caste%'     
     AND marital_status LIKE '%$marital_status%'
  LIMIT '$offset', '$recordsPerPage'

然后 $offset 应该以 $recordsPerPage 为增量(即 0、100、200、...n*100)和 $recordsPerPage 可以是您希望在每页上显示的任意数量的记录(通常为 10、20 或 100)

于 2013-02-07T19:16:40.583 回答
0

如果客户端分页不成问题,一个简单的解决方案是使用 Jquery 分页。您可以在以下位置查看示例http://www.datatables.net/release-datatables/examples/basic_init/alt_pagination.html

在您的 JavaScript 代码中添加:

$(document).ready(function() {
    $('#example').dataTable( {
        "sPaginationType": "full_numbers"
    } );
} );

如果您的元素,'example' 是 ID。

于 2013-02-07T19:21:44.727 回答