duanqianruan8448 2015-04-17 04:33
浏览 45
已采纳

任何人都可以帮助我尝试以下代码的分页......!

Well below i have attached the table structure kindly go through it.

I would like to get serial no as my 1st column followed by id and name. serial no should continue for pagination rather then starting from 1st. Thanks in advance..

Table structure is

create table departments (

 id INT(20) AUTO_INCREMENT PRIMARY KEY, 

name VARCHAR(30) NOT NULL);

<html>
<head>
<?php
    $db_connection = new mysqli("localhost","root","","emp_app");
    if($db_connection->connect_error)
        die("connection failed".$db_connection->connect_error); 
?>
</head>
<body>
    <table>
        <tr>
            <th>Serial no </th>
            <th>id</th>
            <th>name</th>
        </tr>
        <?php
        $sql_query = "select * from departments";
        $result = $db_connection->query($sql_query);
        if($result->num_rows > 0){
            while($rows = $result->fetch_assoc()){
                 echo "<tr>";
                 echo "<td>".$rows["id"]."<td>";
                 echo "<td>".$rows["name"]."<td>";
                 echo "<tr>";
            }
        }
        ?>
    </table>
</body>

  • 写回答

3条回答 默认 最新

  • douxing5199 2015-04-17 04:49
    关注

    here is a good example how to add pagination in php pagination in php Refrence link is using depricated mysql query you need to replace with mysqli query

     $num_rec_per_page=10;
    
        if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; }; 
        $start_from = ($page-1) * $num_rec_per_page; 
        $sql = "SELECT * FROM departments LIMIT $start_from, $num_rec_per_page"; 
        $rs_result = $db_connection->query ($sql); //run the query
    $serial=1;
        while ($row = $result->fetch_assoc) { 
              //code here 
    echo $serial;
    $serial++;
        }; 
    
        $sql = "SELECT * FROM departments";  //select query for total records
        $rs_result = $db_connection->query($sql); //run the query
        $total_records =$rs_result->num_rows;  //count number of records
        $total_pages = ceil($total_records / $num_rec_per_page); 
    
        echo "<a href='pagination.php?page=1'>".'|<'."</a> "; // Goto 1st page  
    
        for ($i=1; $i<=$total_pages; $i++) { 
                    echo "<a href='pagination.php?page=".$i."'>".$i."</a> "; 
        }; 
        echo "<a href='pagination.php?page=$total_pages'>".'>|'."</a> "; // Goto last page
        ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错