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条)

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历