0

我是 PHP 的初学者。生成分页我得到了 100 多页,它们占用了大量空间,有助于确保您可以执行以下操作:

在此处输入图像描述

带有分隔符...和按钮:第一页和最后一页。

我的代码是

<div class="span12">
<?php 
$num_pages = 0;
$per_pages = 2;
echo <<<EOF
<span class="active"><a href="index.php">1</a></span>
EOF;
do{
echo <<<EOF
<span class="pagination"><a href="index.php?num={$per_pages}">{$per_pages}</a></span>
EOF;
$per_pages = $per_pages + 1;
$num_pages++;
}
while ($num_pages <= 100);
echo "\r\n";
?>
</div>
<?php echo "\r\n"; ?>
4

1 回答 1

1

只是一个快速的近似值

<div class="container" >

<?php  
/*
    Here you should fetch your pages from your DB or whatever you're using
*/
$num_pages = 50;
$num_paginations = ceil($num_pages / 20);
$current_pagination = 0;
$current_page = 0;
$prev_page = 0;
$next_page = 0;

if(isset($_GET['page'])){

    $current_page = $_GET['page'];
    $prev_page = $current_page-1;
    $next_page = $current_page+1;
    for ($i=1; $i <= $num_paginations ; $i++) { 
        if($current_page / (20*$i) < 1){
            $current_pagination = $i;
            break;
        }
    }
}

if (isset($_GET['page']) && $_GET['page'] > 0  && $_GET['page'] <= $num_pages ) 
{

?>
<ul class="pagination">
    <li><a <?php echo 'href="pagination.php?page='.$prev_page.'"';?> >&laquo;</a></li>
    <?php 
    $initial_number = $current_pagination*20-19;
    $final_number = $current_pagination*20;
    $j = 1;
    for ($i=$initial_number; $i <=$final_number; $i++) { 

        if($j <= 5 || $j == 20 || $j == 19){
        ?>

            <li <?php 

            if($i == $current_page) echo ' class="active"'; 
            if($i > $num_pages){
                echo ' class="disabled"'; 
                ?>
                ><a> <?php echo $i; ?></a> 
                <?
                continue;
            } 

            ?>>
                <a <?php echo 'href="pagination.php?page='.$i.'"';?> > <?php echo $i; ?></a>        
            </li>
        <?
        }
        if($j == 6){
        ?>
            <li class="disabled"><a>...</a></li>
        <?
        }
        $j++;
    }

    ?>   
    <li><a <?php echo 'href="pagination.php?page='.$next_page.'"';?> >&raquo;</a></li>
</ul>
<?php  
}else{
    ?>
    <h2>Page doesn't exist!</h2>
    <?
}
?>

</div>
于 2013-08-26T12:11:33.417 回答