dongreng9864 2015-07-29 09:46
浏览 58
已采纳

分页上一个 - 下一个增量

i create a php news system, but i have a problem:

<?php
include('config.php');
if( isset( $_GET["page"]) ) $PAGE=$_GET["page"]; else $PAGE=1;
$query1=mysql_query("select id, name, email , age from addd LIMIT ". (($PAGE * 5) - 5) .",5");

echo "<table><tr><td>Testo</td><td>Nome</td><td>Anni</td></tr>";

function truncate_string($str, $length) {
if (!(strlen($query2['name']) <= $length)) {
    $query2['name'] = substr($query2['name'], 0, strpos($query2['name'], ' ', $length)) . '...';
}

return $query2['name'];
}

while($query2=mysql_fetch_array($query1))
{
$number= $query2['name'];
echo "<tr><td>".substr($query2['name'], 0, 500)."...</td>";
echo "<td>".$query2['email']."</td>";
echo "<td>".$query2['age']."</td>";
echo "<td>".str_word_count($number)."</td>";
echo "<td><a href='edit.php?id=".$query2['id']."'>Mod</a></td>";
echo "<td><a href='delete.php?id=".$query2['id']."' onclick=\"return confirm('Sei sicuro di volerlo eliminare?');\");'>Canc</a></td><tr>";
echo "<td><a href='singletwo.php?id=".$query2['id']."');'>vedi</a></td<tr>";
}
?>

The pages follow this numbering: ?page=1, ?page=2 ecc.

Each page contains 5 news.

How do I create an automatic pagination system?

With Prev-Next, automatically detect possible next or previous pages?

I don't know how to do it.

  • 写回答

2条回答 默认 最新

  • dsxsou8465 2015-07-29 09:51
    关注

    Start by having the max length and total number of rows in variables:

    <?php
    include('config.php');
    
    $max = 5;
    $total = mysql_query("select count(*) from addd");
    $PAGE = isset($_GET["page"]) ? $_GET["page"] : 1;
    
    $query1 = mysql_query("select id, name, email , age from addd LIMIT " . (($PAGE * $max) - $max) . "," . $max);
    

    That way, you can calculate how many pages you'll need.

    The following code will give you a page list (Page 1, Page 2, Page 3 etc.):

    for($i = 0; $i < ceil($total / $max); $i ++)
    {
        $p = $i + 1;
        echo '<a href="?page=' . $p . '">Page ' . $p . '</a>';
    }
    

    If you'd rather have Previous and Next links, try this:

    if($PAGE > 1)
        echo '<a href="?page=' . ($PAGE - 1) . '>Previous</a>';
    
    if(ceil($total / $max) > $PAGE)
        echo '<a href="?page=' . ($PAGE + 1) . '>Next</a>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度