在最后一页上,下一个链接消失,之前的链接从未显示从头到尾

时间:2016-03-28 10:43:22

标签: php sql pagination

我无法理解我在做什么。

<?php
    $sql = "SELECT COUNT(*) from customers";
    $query = mysql_query($sql);
    $row = mysql_fetch_row($query);
    $rows = $row[0];
    $page_rows = 3;
    $last = ceil($rows/$page_rows); 
    if($last<1) {
        $last = 1;
    }
    $pagenum = 1;
    if(isset($_GET['pn'])) {        
        $pagenum = preg_replace('#[^0-9]#','',$_GET['pn']);
    } 
    if($pagenum<1) {
        $pagenum=1;
    } else 
        if($pagenum>$last) {
            $pagenum = $last;
        }

    $start = ($pagenum-1)*$page_rows;
    $end = $page_rows;  
    $textline ="page<b>$pagenum</b> of <b>$last</b>" ;
    $paginationCtrl='';
    if($last!=1) {
        if($pagenum>1) {
            // here i am giving the previous link to variable pagination.
            $previous = $pagenum-1;
            $paginationCtrl .= '<a href="'.$_SERVER['PHP_SELF'].'?     pn='.$previous.'">Previous</a>&nbsp;&nbsp;';

            //If i print this this variable then it shows it value.

            for($i=$pagenum-4; $i<$pagenum; $i++) {
                if($i>0) {
                    $paginationCtrl .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a>&nbsp;&nbsp;';
                }
            }
        }
        $paginationCtrl=''.$pagenum.'&nbsp;';
        for($i=$pagenum+1; $i<=$last; $i++) {                   
            $paginationCtrl .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a>&nbsp;&nbsp;';
            if($i>=$pagenum+4) {
                break;
            }
        }
        if($pagenum!=$last) {
            // Here i am defiinig next link which appeared untill the last page is clicked. if user is on last page it also disappeared.

            $next = $pagenum+1;
            $paginationCtrl .= '&nbsp;&nbsp;<a href="'.$_SERVER['PHP_SELF'].'?pn='.$next.'">Next</a>';
        }
    }
?>
<div class="box-body">
    <table id="example2" class="table table-bordered table-hover">
        <thead>                    
            <tr>
                <th>Customer Name</th>                               
            </tr>
        </thead>
        <tbody>
<?php               
        $sql2 = "SELECT * from customers order by customer_id DESC LIMIT $start,$end";   
        $query2 = mysql_query($sql2);
        while($row = mysql_fetch_array($query2)) { ?>
            <tr>
            </tr>
        <?php } ?>
            <tr><td></td></tr>
        </tbody>                                
    </table>

这里显示的是1 2 3 next,如果我在第2页,则显示为3 next。

<div><?php echo $paginationCtrl; ?>  </div>

如果我总共有3页。我在第2页,之前的链接没有出现。如果我在最后一页,下一个链接也消失了。和paginationctrl变量只显示第3页。没有显示以前的页面或链接

1 个答案:

答案 0 :(得分:0)

  

在最后一页上,下一个链接消失了

是的,但这很好。在最后一页,没有下一页。

如果您想显示一个不活跃的“下一个”链接,您可以更改代码:

if( $pagenum!=$last )
{
    $next = $pagenum+1;
    $paginationCtrl .= '&nbsp;&nbsp;<a href="'.$_SERVER['PHP_SELF'].'?pn='.$next.'">Next</a>';
}
else
{
    $paginationCtrl .= '&nbsp;&nbsp;<div class="inactive">Next</div>';
}
  

之前的链接从未出现过开始结束

你有一个错字。替换此行:

$paginationCtrl=''.$pagenum.'&nbsp;';

使用:

$paginationCtrl .= '' . $pagenum . '&nbsp;';