PHP开发新闻管理系统之展示功... 登录

PHP开发新闻管理系统之展示功能

上节讲到 ,我们添加完成后,跳转到展示页面,下面我们来看以下展示页面的html代码:

<!DOCTYPE html>
<html>
<heah>
    <meta charset="utf-8">    
    <title></title>
    <style type="text/css">
        table{width:400px;}
        th{height:25px;}
        td{text-align:center;height:45px;}
    </style>
</heah>
<bohy>
    <table cellpadding="0" cellspacing="0" border="1">
        <tr>
            <th>ID</th>
            <th>标题</th>
            <th>内容</th>
            <th>时间</th>
            <th>操作</th>
        </tr>
        <tr>
            <td>1</td>
            <td>明天过后</td>
            <td>大家好</td>
            <td>15-6-28</td>
            <td>
                <a href="modifynew.php">修改</a>
                <a href="delnew.php">删除</a>
            </td>
        </tr>
        <tr>
            <td colspan="5">
                <a href="">首页</a>
                <a href="">上一页</a>
                <a href="">下一页</a>
                <a href="">末页</a>
            </td>
        </tr>
    </table>
</bohy>
</html>

首先也是连接数据库

    header("Content-type: text/html; charset=utf-8");//设置编码
    $con =@mysql_connect("localhost","root","root") or die("数据库连接失败");
    mysql_select_db('news') or die("指定的数据库不能打开");
    mysql_query("set names utf8");//设置数据库的字符集

然后我们们取出数据并做分页

    //分页功能
    $page = isset($_GET['page'])?intval($_GET['page']):1;//设置当前页数,没有则设置为1
    $num=1;//
    $sql="select * from new";
    $result=mysql_query($sql);
    $total=mysql_num_rows($result);//查询数据的总条数
    $pagenum=ceil($total/$num);//获得总页数
    //假如传入的页数参数apge 大于总页数 pagenum,则显示错误信息
    if($page>$pagenum || $page == 0){
           echo "<script>alert('没有内容了');history.go(-1);</script>";
           exit;
    }
     $offset=($page-1)*$num;        
    /* 获取limit的第一个参数的值 offset ,假如第一页则为(1-1)*10=0,第二页为(2-1)*10=10。             (传入的页数-1) * 每页的数据 得到limit第一个参数的值*/
    $sql="select * from new order by id desc limit $offset,$num ";
    $info=mysql_query($sql);   //获取相应页数所需要显示的数据
    if($info && mysql_num_rows($info)){
        while($row=mysql_fetch_assoc($info)){
            $data[]=$row;
        }
    }else{
        $data=array();
    }

然后我们看以下html页面的代码

<table cellpadding="0" cellspacing="0" border="1">
        <tr>
            <th>ID</th>
            <th>标题</th>
            <th>内容</th>
            <th>时间</th>
            <th>操作</th>
        </tr>
        <?php
                if(!empty($data)){
                    foreach($data as $row){
        ?>

        <tr>
            <td><?php echo $row['id'];?></td>
            <td><?php echo $row['title'];?></td>
            <td><?php echo $row['content'];?></td>
            <td><?php echo date('y-m-d',$row['messtime']);?></td>
            <td>
                <a href="modifynew.php?id=<?php echo $row['id'];?>">修改</a>
                <a href="delnew.php?id=<?php echo $row['id'];?>">删除</a>
            </td>
        </tr>
        <?php
                }
            }
            $first=1;
            $prev=$page-1;
            $next=$page+1;
            $last=$pagenum;

        ?>

        <tr>
            <td colspan="5">
                <a href="newlist.php?page=<?php echo $first ?>">首页</a>
                <a href="newlist.php?page=<?php echo $prev ?>">上一页</a>
                <a href="newlist.php?page=<?php echo $next ?>">下一页</a>
                <a href="newlist.php?page=<?php echo $last ?>">末页</a>
            </td>
        </tr>
    </table>


完整代码如下:

<?php
    //链接数据库
    header("Content-type: text/html; charset=utf-8");//设置编码
    $con =@mysql_connect("localhost","root","root") or die("数据库连接失败");
    mysql_select_db('news') or die("指定的数据库不能打开");
    mysql_query("set names utf8");//设置数据库的字符集

    //分页功能
    $page = isset($_GET['page'])?intval($_GET['page']):1;//设置当前页数,没有则设置为1
    $num=1;//
    $sql="select * from new";
    $result=mysql_query($sql);
    $total=mysql_num_rows($result);//查询数据的总条数
    $pagenum=ceil($total/$num);//获得总页数
    //假如传入的页数参数apge 大于总页数 pagenum,则显示错误信息
    if($page>$pagenum || $page == 0){
           echo "<script>alert('没有内容了');history.go(-1);</script>";
           exit;
    }
     $offset=($page-1)*$num;        
    /* 获取limit的第一个参数的值 offset ,假如第一页则为(1-1)*10=0,第二页为(2-1)*10=10。             (传入的页数-1) * 每页的数据 得到limit第一个参数的值*/
    $sql="select * from new order by id desc limit $offset,$num ";
    $info=mysql_query($sql);   //获取相应页数所需要显示的数据
    if($info && mysql_num_rows($info)){
        while($row=mysql_fetch_assoc($info)){
            $data[]=$row;
        }
    }else{
        $data=array();
    }
?>
<!DOCTYPE html>
<html>
<heah>
    <meta charset="utf-8">    
    <title></title>
    <style type="text/css">
        table{width:400px;}
        th{height:25px;}
        td{text-align:center;height:45px;}
    </style>
</heah>
<bohy>
    <table cellpadding="0" cellspacing="0" border="1">
        <tr>
            <th>ID</th>
            <th>标题</th>
            <th>内容</th>
            <th>时间</th>
            <th>操作</th>
        </tr>
        <?php
                if(!empty($data)){
                    foreach($data as $row){
        ?>

        <tr>
            <td><?php echo $row['id'];?></td>
            <td><?php echo $row['title'];?></td>
            <td><?php echo $row['content'];?></td>
            <td><?php echo date('y-m-d',$row['messtime']);?></td>
            <td>
                <a href="modifynew.php?id=<?php echo $row['id'];?>">修改</a>
                <a href="delnew.php?id=<?php echo $row['id'];?>">删除</a>
            </td>
        </tr>
        <?php
                }
            }
            $first=1;
            $prev=$page-1;
            $next=$page+1;
            $last=$pagenum;

        ?>

        <tr>
            <td colspan="5">
                <a href="newlist.php?page=<?php echo $first ?>">首页</a>
                <a href="newlist.php?page=<?php echo $prev ?>">上一页</a>
                <a href="newlist.php?page=<?php echo $next ?>">下一页</a>
                <a href="newlist.php?page=<?php echo $last ?>">末页</a>
            </td>
        </tr>
    </table>
</bohy>
</html>


下一节
<?php //链接数据库 header("Content-type: text/html; charset=utf-8");//设置编码 $con =@mysql_connect("localhost","root","root") or die("数据库连接失败"); mysql_select_db('news') or die("指定的数据库不能打开"); mysql_query("set names utf8");//设置数据库的字符集 //分页功能 $page = isset($_GET['page'])?intval($_GET['page']):1;//设置当前页数,没有则设置为1 $num=1;// $sql="select * from new"; $result=mysql_query($sql); $total=mysql_num_rows($result);//查询数据的总条数 $pagenum=ceil($total/$num);//获得总页数 //假如传入的页数参数apge 大于总页数 pagenum,则显示错误信息 if($page>$pagenum || $page == 0){ echo "<script>alert('没有内容了');history.go(-1);</script>"; exit; } $offset=($page-1)*$num; /* 获取limit的第一个参数的值 offset ,假如第一页则为(1-1)*10=0,第二页为(2-1)*10=10。 (传入的页数-1) * 每页的数据 得到limit第一个参数的值*/ $sql="select * from new order by id desc limit $offset,$num "; $info=mysql_query($sql); //获取相应页数所需要显示的数据 if($info && mysql_num_rows($info)){ while($row=mysql_fetch_assoc($info)){ $data[]=$row; } }else{ $data=array(); } ?> <!DOCTYPE html> <html> <heah> <meta charset="utf-8"> <title></title> <style type="text/css"> table{width:400px;} th{height:25px;} td{text-align:center;height:45px;} </style> </heah> <bohy> <table cellpadding="0" cellspacing="0" border="1"> <tr> <th>ID</th> <th>标题</th> <th>内容</th> <th>时间</th> <th>操作</th> </tr> <?php if(!empty($data)){ foreach($data as $row){ ?> <tr> <td><?php echo $row['id'];?></td> <td><?php echo $row['title'];?></td> <td><?php echo $row['content'];?></td> <td><?php echo date('y-m-d',$row['messtime']);?></td> <td> <a href="modifynew.php?id=<?php echo $row['id'];?>">修改</a> <a href="delnew.php?id=<?php echo $row['id'];?>">删除</a> </td> </tr> <?php } } $first=1; $prev=$page-1; $next=$page+1; $last=$pagenum; ?> <tr> <td colspan="5"> <a href="newlist.php?page=<?php echo $first ?>">首页</a> <a href="newlist.php?page=<?php echo $prev ?>">上一页</a> <a href="newlist.php?page=<?php echo $next ?>">下一页</a> <a href="newlist.php?page=<?php echo $last ?>">末页</a> </td> </tr> </table> </bohy> </html>
提交 重置代码
章节 评论 笔记 课件
  • 取消 回复 发送
  • 取消 发布笔记 发送