在转到下一页时,count会在PHP中自动重置分页

时间:2017-10-01 08:14:00

标签: php mysql

在我的页面中,我将ITEM PER PAGE值设为5。

在第一页中,它显示5条记录为Slno 1,2,3,4,5,但是一旦我通过分页进入下一页,它显示接下来的5条记录,但是Slno再次从1,2,3,4开始, 5。

我的代码:

<style>
/* Pagination style */
.pagination{margin:0;padding:0;}
.pagination div{
display: inline;
padding: 6px 10px 6px 10px;
border: 1px solid #ddd;
margin-right: -1px;
font: 15px/20px Arial, Helvetica, sans-serif;
background: #FFFFFF;
box-shadow: inset 1px 1px 5px #F4F4F4;
}
.pagination div a{
color: rgb(89, 141, 235);
}
.pagination div.first {
border-radius: 5px 0px 0px 5px;
}
.pagination div.last {
border-radius: 0px 5px 5px 0px;
}
.pagination div:hover{
background: #CFF;
}
.pagination div.active{
background: red;
color: #FFF;
}
</style>

<?php
//continue only if $_POST is set and it is a Ajax request
if(isset($_POST) && isset($_SERVER['HTTP_X_REQUESTED_WITH']) && 
strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){

include("config.php");  //include config file
//Get page number from Ajax POST
if(isset($_POST["page"])){
    $page_number = filter_var($_POST["page"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH); //filter number
    if(!is_numeric($page_number)){die('Invalid page number!');} //incase of invalid page number
}else{
    $page_number = 1; //if there's no page number, set it to 1
}

//get total number of records from database for pagination
$results = $linkID1->query("SELECT COUNT(*) FROM cae_users");
$get_total_rows = $results->fetch_row(); //hold total records in variable
//break records into pages
$total_pages = ceil($get_total_rows[0]/$item_per_page);

//get starting position to fetch the records
$page_position = (($page_number-1) * $item_per_page);

//SQL query that will fetch group of records depending on starting position and item per page. See SQL LIMIT clause
$result = $linkID1->query("select fullname,email,mobile,city,date_registered,user_type,rcode from cae_users LIMIT $page_position, $item_per_page")
or
die("error");
?>
    <div class="contact-form">
            <div style="overflow-x:auto;">
              <table>
                <tr style="color:#FFF; background-color:#F00;">
                  <th>Sl. No.</th>
                  <th>Fullname</th>
                  <th>Email ID</th>
                  <th>Mobile</th>
                  <th>City</th>
                  <th>Date Registered</th>
                  <th>User Type</th>
                  <th>Referred By</th>
                </tr>
                <?php
                    $num=1;
                    while($row = mysqli_fetch_array($result))
                    {
                ?>
                    <tr>
                      <td><?php echo $num; ?></td>
                      <td><?php echo $row['fullname']; ?></td>
                      <td><?php echo $row['email']; ?></td>
                      <td><?php echo $row['mobile']; ?></td>
                      <td><?php echo $row['city']; ?></td>
                      <td><?php echo $row['date_registered']; ?></td>
                      <td><?php echo $row['user_type']; ?></td>
                      <td><?php echo $row['rcode']; ?></td>
                    </tr>
                <?php
                        $num++;
                    }
                ?>
              </table>
            </div>
        </div>
    <br>
<?php
echo '<div align="center">';
/* We call the pagination function here to generate Pagination link for us. 
As you can see I have passed several parameters to the function. */
echo paginate_function($item_per_page, $page_number, $get_total_rows[0], $total_pages);
echo '</div>';
}
################ pagination function #########################################
function paginate_function($item_per_page, $current_page, $total_records, 
$total_pages)
{
$pagination = '';
if($total_pages > 0 && $total_pages != 1 && $current_page <= $total_pages){ //verify total pages and current page number
    $pagination .= '<div class="pagination">';

    $right_links    = $current_page + 3; 
    $previous       = $current_page - 1; //previous link 
    $next           = $current_page + 1; //next link
    $first_link     = true; //boolean var to decide our first link

    if($current_page > 1){
        $previous_link = ($previous==0)?1:$previous;
        $pagination .= '<div class="first"><a href="#" data-page="1" title="First">&laquo;</a></div>'; //first link
        $pagination .= '<div><a href="#" data-page="'.$previous_link.'" title="Previous">&lt;</a></div>'; //previous link
            for($i = ($current_page-2); $i < $current_page; $i++){ //Create left-hand side links
                if($i > 0){
                    $pagination .= '<div><a href="#" data-page="'.$i.'" title="Page'.$i.'">'.$i.'</a></div>';
                }
            }   
        $first_link = false; //set first link to false
    }

    if($first_link){ //if current active page is first link
        $pagination .= '<div class="first active">'.$current_page.'</div>';
    }elseif($current_page == $total_pages){ //if it's the last active link
        $pagination .= '<div class="last active">'.$current_page.'</div>';
    }else{ //regular current link
        $pagination .= '<div class="active">'.$current_page.'</div>';
    }

    for($i = $current_page+1; $i < $right_links ; $i++){ //create right-hand side links
        if($i<=$total_pages){
            $pagination .= '<div><a href="#" data-page="'.$i.'" title="Page '.$i.'">'.$i.'</a></div>';
        }
    }
    if($current_page < $total_pages){ 
            $next_link = ($i > $total_pages)? $total_pages : $i;
            $pagination .= '<div><a href="#" data-page="'.$next_link.'" title="Next">&gt;</a></div>'; //next link
            $pagination .= '<div class="last"><a href="#" data-page="'.$total_pages.'" title="Last">&raquo;</a></div>'; //last link
    }

    $pagination .= '</div>'; 
}
return $pagination; //return pagination links
}
?>

1 个答案:

答案 0 :(得分:0)

解决了这个问题。我在users表中添加了一个新列slno [autoincrement]并显示了该列值。现在它的工作正常。日Thnx。