• php,混编,数据分页


    连接数据库

    <?php
    $conn = mysql_connect('localhost', 'root', 'root') or die('ErrorCode:' . mysql_errno() . ',Error:' . mysql_error());

    mysql_select_db('company') or die('ErrorCode:' . mysql_errno() . ',Error:' . mysql_error());

    ?>

    混编数据分页

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8"/>
    <title>测试分页</title>
    </head>
    <body>
    <?php
    include 'conn.php';

    $pagesize = 10;
    mysql_query("SET NAMES utf8");
    $sql = "SELECT count(*) cnt FROM com_info";//取出总的记录条数
    $r = mysql_query($sql) or die('error code:' . mysql_errno() . ',error:' . mysql_error());

    $row = mysql_fetch_assoc($r);
    $total = $row['cnt'];//总的记录条数

    $total_pages = ceil(intval($total)/$pagesize);//总页数

    $pn = isset($_GET['page']) ? intval($_GET['page']) : 1;
    $pn < 1 && $pn = 1;
    $pn >= $total_pages && $pn = $total_pages;

    $offset = ($pn - 1)*$pagesize;
    $sql = "SELECT com,com_name FROM com_info LIMIT {$offset},{$pagesize}";
    $r = mysql_query($sql) or die('error code:' . mysql_errno() . ',error:' . mysql_error());

    ?>
    <table>
    <tr><th>编号</th><th>公司名</th></tr>
    <?php
    while ($row = mysql_fetch_assoc($r)) {
    ?>
    <tr><td><?php echo $row['com']?></td><td><?php echo $row['com_name']?></td></tr>
    <?php
    }
    ?>
    </table>
    <div class="page">
    <a href="page.php">首页</a>
    <?php
    for ($i = 1; $i <= $total_pages; $i++) {
    ?>
    <a href="page.php?page=<?php echo $i?>">第<?php echo $i?>页</a>
    <?php
    }

    ?>
    <a href="page.php?page=<?php echo $total_pages?>">末页</a>
    </div>
    </body>
    </html>

  • 相关阅读:
    《JS权威指南学习总结--6.6属性getter和setter》
    《JS权威指南学习总结--6.5枚举属性》
    django的模板系统
    django额外参数的传递和url命名
    python在图片上画矩形
    当爬虫遇到js加密
    php基础-7
    php基础-6
    php基础-5
    php基础-4
  • 原文地址:https://www.cnblogs.com/limanman/p/5314565.html
Copyright © 2020-2023  润新知