MySql+PHP实现分页

6 篇文章 0 订阅
<?php
header('Content-Type:text/html;charset=utf-8');
$link=mysql_connect('localhost','root','root');
mysql_select_db('cms2');
$sql="set names utf8";
mysql_query($sql);
//将新闻分类从数据库中取出放到页面上
$query="select * from cms_type";
$result=mysql_query($query);
//添加新闻,将新闻插入数据库中
//一般要插入什么东西的时候,都是先判断不是空,防止每次刷新都将空的东西插入数据库中
if(!empty($_POST)){
//新闻标题
$title=$_POST['title'];
//新闻类型
$type=$_POST['type'];
//新闻内容
$contents=$_POST['contents'];
$query="insert cms_article(title,contents,tid,aid,addtime)
        value
        ('{$title}','{$contents}','{$type}',1,now())
";
$result=mysql_query($query);
  if($result){
      echo '成功的插入第'.mysql_insert_id().'行';
  }else{
      echo '新闻添加失败';
  }
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>my demo</title>
<link type="text/css" rel="stylesheet" href="styles/reset.css" media="all"/>
<style>
    #wrap{
         padding:20px;
    }
     table{
          width:100%;
          border-top:1px solid #ccc;
          border-left:1px solid #ccc;
     }
     td,th{
          border-right:1px solid #ccc;
          border-bottom:1px solid #ccc;
          padding:8px;
     }
</style>
</head>
<body>
 <div id="wrap">
    <form action="addnew.php" method="post">
          <table>
               <tr>
                     <th colspan="2" class="title" style="font-size:30px">新闻发布</th>
               </tr>
               <tr>
                    <td>标题</td>
                    <td><input   type="text" name="title"/></td>
               </tr>
               <tr>
                    <td>分类</td>
                    <td>
                         <select name="type">
                             <?php while($row=mysql_fetch_assoc($result)){?>
                                 <option value="<?php echo $row['id'] ?>"><?php echo $row['tname'] ?></option>
                             <?php }?>
                         </select>
                    </td>
               </tr>
               <tr>
                    <td>正文</td>
                    <td>
                        <textarea rows="10" cols="50" name="contents"></textarea>
                    </td>
               </tr>
               <tr>
                     <th colspan="2">
                         <input type="submit" value="发布"/>
                     </th>
               </tr>
          </table>
    </form>
 </div>
</body>
</html>
-------------------------------------------------------------------------------------
<?php
header('Content-Type:text/html;charset=utf-8');
$link=mysql_connect('localhost','root','root');
//实现分页的原理解析
mysql_select_db('cms2');
$sql="set names utf8";
mysql_query($sql);
$query="select id,aname
        from cms_admin
        order by id asc
        limit 0,5
        ";
$result=mysql_query($query);

echo "<table border='1' width='500'>";
echo "<thead>
        <tr>
            <th>ID</th><th>姓名</th>
        </tr>
      </thead>
      <tbody>
";
while($row=mysql_fetch_assoc($result)){
    echo '<tr>';
    echo '<td>'.$row['id'].'</td>';
    echo '<td>'.$row['aname'].'</td>';
    echo '</tr>';
}
echo "</tbody></table>";
-------------------------------------------------------------------------------------
<?php
header('Content-Type:text/html;charset=utf-8');
//$_GET的测试程序
//http://127.0.0.1/project1/get.php?id=123&aname=tom
var_dump($_GET);
-------------------------------------------------------------------------------------
<?php
//这个是手动分页
header('Content-Type:text/html;charset=utf-8');
$link=mysql_connect('localhost','root','root');
//实现分页的原理解析
mysql_select_db('cms2');
$sql="set names utf8";
mysql_query($sql);

//每次显示的记录数
$page_size=5;
//page从浏览器的地址栏给出
//判断是否设置了变量$_GET['p'],就是是都在浏览器上面设置了p=多少
//设置了的话,就返回获得当前的页数,否则的话返回第一页
$page=isset($_GET['p'])?$_GET['p']:1;
//偏移量计算=(当前页-1)*,每页显示记录数
$offset=($page-1)*$page_size;

echo $offset,$page_size;
//每页显示的记录
$query="select id,aname
        from cms_admin
        order by id asc
        limit $offset,$page_size
        ";
$result=mysql_query($query);

echo "<table border='1' width='500'>";
echo "<thead>
        <tr>
            <th>ID</th><th>姓名</th>
        </tr>
      </thead>
      <tbody>
";
while($row=mysql_fetch_assoc($result)){
    echo '<tr>';
    echo '<td>'.$row['id'].'</td>';
    echo '<td>'.$row['aname'].'</td>';
    echo '</tr>';
}
echo "</tbody></table>";
echo '共'.mysql_num_rows($result).'行';
-------------------------------------mysql分页完整版-----------------------------
<?php
/*
 *5、应用mysql获取结果的函数,将cms_admin表中
 *的数据读取出来放到table表格中。 
 */
header("Content-Type:text/html;charset=utf-8");
//1、连接数据库
$link = mysql_connect("localhost",
                      "root",
                      "root");
//2、选择默认数据库
mysql_select_db("cms");
//3、操作-有返回结果集的sql语句 返回 资源

//每页显示记录数
$page_size = 5;
//获取当前页,当前页从地址栏获取,若没有当前页,则默认为1
$page = isset($_GET['p'])?$_GET['p']:1;

//获取总页数
$query = "select id from cms_admin";
$result = mysql_query($query);
//获取总记录数
$total = mysql_num_rows($result);
//总页数 = ceil(总记录数/每页显示记录数)
$total_page = ceil($total/$page_size); 


//对page进行限制
if($page<=0){
    //page最小为1
    $page=1;
}else if($page>=$total_page&&$total_page!=0){
    //page最大为total_page
    $page=$total_page;
}

//首页 上一页
//上一页的连接地址 = 当前页-1
$prev = $page-1; 
$flist = "";
if($prev>=1){
$flist = "<a href='?p=1'>首页</a>&nbsp;
          <a href='?p=".$prev."'>上一页</a>";
}
///中间的页数列表
// 1 2 3 4 5 6 7
// 假设当前页为4
//定义队列长度
$num = 3;
$lists = "";
//1 2 3
for($i=$num;$i>=1;$i--){
    $n = $page-$i;
    if($n>0){
        $lists.="
            <a href='?p={$n}'>{$n}</a>
        ";
    }
}
// 4 将中间的页数连接上
$lists.="<a href='?p={$page}'>{$page}</a>";
//5 6 7
for($i=1;$i<=$num;$i++){
    $n=$page+$i;
    if($n<=$total_page){
        $lists.="
            <a href='?p={$n}'>{$n}</a>
        ";
    }
}

$end="";
$next=$page+1;
if($next<=$total_page+1){
    $end.="<a href='?p={$next}'>下一页</a>
          <a href='?p={$total_page}'>尾页</a>
         ";
}

//每页显示的记录
$query="select id,aname
        from cms_admin
        order by id asc
        limit $offset,$page_size
        ";
$result=mysql_query($query);
//从mysql_query产生的资源中获取结果
//mysql_fetch_assoc
//输出table表格的外边框
echo "<table border='1' width='500'>";
//输出table表格的表头
echo "<tr><th>ID</th><th>姓名</th>
      <th>年龄</th></tr>";
// 循环从数据库中读取出数据
 while($row = mysql_fetch_assoc($result)){
    //循环输出table表格的每一行
    echo "<tr>";
    echo "<td>".$row['id']."</td>";
    echo "<td>".$row['aname']."</td>";
    echo "<td>".$row['age']."</td>";
    echo "</tr>";
 }

echo "</table>";

echo "共".$total_page."页&nbsp;当前第".$page."页".
      $flist.$lists.$llist;
//4、关闭数据库 释放资源
mysql_close();
-------------//将cms_article表中的数据读取出来显示到页面上--------------------
<?php
header("Content-Type:text/html;charset=utf-8");
//引入数据库配置文件
include '../common/db.inc.php';
//将cms_article表中的数据读取出来
$query="select a.id,title,tname,addtime
        from cms_article as a
        inner join cms_type as t
        on a.id=t.id
        ";
$result=mysql_query($query);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>my demo</title>
    <link type="text/css" rel="stylesheet" href="styles/reset.css" media="all"/>
    <style>
        #wrap{
            padding:20px;
        }
        table{
            width:100%;
            border-top:1px solid #ccc;
            border-left:1px solid #ccc;
        }
        td,th{
            border-right:1px solid #ccc;
            border-bottom:1px solid #ccc;
            padding:8px;
        }
    </style>
</head>
<body>
<div id="wrap">
    <form action="addnew.php" method="post">
        <table>
            <tr>
                <th colspan="5" class="title" style="font-size:30px">新闻列表</th>
            </tr>
            <tr>
               <th>ID</th>
                <th>新闻标题</th>
                <th>新闻分类</th>
                <th>添加时间</th>
                <th>操作</th>
            </tr>
            <?php
                while($row=mysql_fetch_assoc($result)) {?>
                   <tr>
                       <td><?php echo $row['id']?></td>
                       <td><?php echo $row['title']?></td>
                       <td><?php echo $row['tname']?></td>
                       <td><?php echo $row['addtime']?></td>
                       <td><a href="#">编辑 | 删除</a></td>
                   </tr>
            <?php
                }
            ?>
        </table>
    </form>
</div>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值