php – 限制分页页码

前端之家收集整理的这篇文章主要介绍了php – 限制分页页码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
$objConnect = MysqL_connect("localhost","root","") or die(MysqL_error());
$objDB = MysqL_select_db("Test");

$strsql = "SELECT * FROM UserAddedRecord WHERE (Name LIKE '%".$getname."%' and State LIKE '%".$getstate."%' and Cell LIKE '%".$getcell."%' and Custgroup LIKE '%".$getgroup."%') AND user_id=$id";

$objQuery = MysqL_query($strsql) or die ("Error Query [".$strsql."]");
$Num_Rows = MysqL_num_rows($objQuery);

$Per_Page = 5;   

if (!isset($_GET['Page'])) {
    $Page = 1;
} else {
    $Page = $_GET['Page'];
}

$Prev_Page = $Page - 1;
$Next_Page = $Page + 1;

$Page_Start = (($Per_Page * $Page) - $Per_Page);
if ($Num_Rows <= $Per_Page) {
    $Num_Pages = 1;
} elseif (($Num_Rows % $Per_Page) == 0) {
    $Num_Pages = ($Num_Rows / $Per_Page) ;
} else {
    $Num_Pages = ($Num_Rows / $Per_Page) + 1;
    $Num_Pages = (int) $Num_Pages;
}


$strsql .=" order  by addedrec_ID DESC LIMIT $Page_Start,$Per_Page";
$objQuery  = MysqL_query($strsql) or trigger_error(MysqL_error());;

if ($Prev_Page) {
    echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$Prev_Page&txtName=$getname&txtState=$getstate&txtCell=$getcell&txtGroup=$getgroup'><< Back</a> ";
}

for ($i=1; $i <= $Num_Pages; $i++) {
    if ($i != $Page) {
        echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$i&txtName=$getname&txtState=$getstate&txtCell=$getcell&txtGroup=$getgroup'>$i</a> ";
    } else {
        echo "<b> $i </b>"; 
    }
}

if ($Page!=$Num_Pages) {
    echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$Next_Page&txtName=$getname&txtState=$getstate&txtCell=$getcell&txtGroup=$getgroup'>Next>></a> ";        
}

MysqL_close($objConnect);

这是我用来创建数据搜索分页代码.但是我只是注意到我有太多的记录,那么它会有太多的页码.我试图限制显示页码,并寻找许多例子.有很多方法可以做到,但是我仍然没有想法如何限制我的方法

可以得到一点点提示吗?
谢谢.

好的,如果你的意思是显示类似的东西
Prev 1 2 3 4 5 6 .. 40 41 Next 
Prev 1 2 .. 6 7 8 9 10 .. 40 41 Next

首先我们需要创建一个可以处理我们输出分页功能.继承我使用的功能,它的功能很好.

function get_paging_info($tot_rows,$pp,$curr_page)
{
    $pages = ceil($tot_rows / $pp); // calc pages

    $data = array(); // start out array
    $data['si']        = ($curr_page * $pp) - $pp; // what row to start at
    $data['pages']     = $pages;                   // add the pages
    $data['curr_page'] = $curr_page;               // Whats the current page

    return $data; //return the paging data

}

现在这个功能非常稳固,对我来说效果很好.

所以你传递这个功能

> $tot_rows =查询的计数行
> $pp =每页的项目
> $curr_page =当前页码

好的,现在有了你需要的数据,你需要显示它.

在我想你之前,请问我使用什么,请阅读,“啊,太久了”.其实很简单

回想一下它将会返回的快照

<!-- Create the query -->
    <?PHP $count = MysqL_fetch_assoc( MysqL_query ( "SELECT COUNT( rows ) as count FROM table" ) ) ;

    <?PHP $count = $count[0]['count']; ?>

<!-- Call our function from above -->
<?PHP $paging_info = get_paging_info($count,5,34); ?>


<p>
    <!-- If the current page is more than 1,show the First and PrevIoUs links -->
    <?PHP if($paging_info['curr_page'] > 1) : ?>
        <a href='' title='Page 1'>First</a>
        <a href='' title='Page <?PHP echo ($paging_info['curr_page'] - 1); ?>'>Prev</a>
    <?PHP endif; ?>



    <?PHP
        //setup starting point

        //$max is equal to number of links shown
        $max = 7;
        if($paging_info['curr_page'] < $max)
            $sp = 1;
        elseif($paging_info['curr_page'] >= ($paging_info['pages'] - floor($max / 2)) )
            $sp = $paging_info['pages'] - $max + 1;
        elseif($paging_info['curr_page'] >= $max)
            $sp = $paging_info['curr_page']  - floor($max/2);
    ?>

    <!-- If the current page >= $max then show link to 1st page -->
    <?PHP if($paging_info['curr_page'] >= $max) : ?>

        <a href='' title='Page 1'>1</a>
        ..

    <?PHP endif; ?>

    <!-- Loop though max number of pages shown and show links either side equal to $max / 2 -->
    <?PHP for($i = $sp; $i <= ($sp + $max -1);$i++) : ?>

        <?PHP
            if($i > $paging_info['pages'])
                continue;
        ?>

        <?PHP if($paging_info['curr_page'] == $i) : ?>

            <span class='bold'><?PHP echo $i; ?></span>

        <?PHP else : ?>

            <a href='' title='Page <?PHP echo $i; ?>'><?PHP echo $i; ?></a>

        <?PHP endif; ?>

    <?PHP endfor; ?>


    <!-- If the current page is less than say the last page minus $max pages divided by 2-->
    <?PHP if($paging_info['curr_page'] < ($paging_info['pages'] - floor($max / 2))) : ?>

        ..
        <a href='' title='Page <?PHP echo $paging_info['pages']; ?>'><?PHP echo $paging_info['pages']; ?></a>

    <?PHP endif; ?>

    <!-- Show last two pages if we're not near them -->
    <?PHP if($paging_info['curr_page'] < $paging_info['pages']) : ?>

        <a href='<?PHP echo str_replace('/page'.$paging_info['curr_page'],'',$paging_info['curr_url']) . '/page'.($paging_info['curr_page'] + 1); ?>' title='Page <?PHP echo ($paging_info['curr_page'] + 1); ?>'>Next</a>

        <a href='<?PHP echo str_replace('/page'.$paging_info['curr_page'],$paging_info['curr_url']) . '/page'.$paging_info['pages']; ?>' title='Page <?PHP echo $paging_info['pages']; ?>'>Last</a>

    <?PHP endif; ?>
</p>
原文链接:https://www.f2er.com/php/132135.html

猜你在找的PHP相关文章