zoukankan      html  css  js  c++  java
  • 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>

  • 相关阅读:
    输出重定向
    echo带颜色输出
    shell学习视频目录
    css盒模型
    jQuery表格模糊搜索
    mysql基础语法3
    mysql基础语法2
    mysql基础语法1
    pyspider框架的使用
    quill富文本框图片上传重写
  • 原文地址:https://www.cnblogs.com/limanman/p/5314565.html
Copyright © 2011-2022 走看看