列表展示 登录

列表展示

向数据库中添加完数据之后,接下来我们就希望将数据库中的数据展示在我们的前台页面中,这就是我们的展示功能。

首先,想要将数据库中的数据展示出来,我们就是要连接我们的数据库,之前提到过,用到连接数据库代码的地方会很多,每用一次就写一遍会很麻烦,我们重新创建一个文件,将连接数据库写进去,接着调用就可以了。

<?php
session_start();
header("content-type:text/html;charset=utf-8");
require 'config.php';
?>

将新文件名称取名为config.php,直接引用就行了

接下来就是从数据库中查询数据,将查到的数据展示在前端页面上。

<?php
$SQL = "select * from list";//设置查询指令
$result=mysqli_query($link,$SQL);//执行查询
?>

将数据从list数据表中查出,接下来就是显示在前端页面了,我们来看下前端文件。

<?php
while($row=mysqli_fetch_assoc($result))
  ?>
  <tr>
    <td style="text-align:left; padding-left:20px;"><input type="checkbox" name="id" value="" /><?php echo $row['id'];?></td>
    <td><span><?php echo $row['author'];?></span></td>
    <td width="10%"><img src="<?php echo $row['image'];?>" alt="" width="70" height="50" /></td>
    <td width="30%"><span><?php echo $row['content'];?></span></td>
    <td><span>首页</span></td>
    <td><span><?php echo $cat_array[$row['cid']];?></span></td>
    <td><span>2016-07-01</span></td>
    <td><div class="button-group">
        <a class="button border-main" href="edit.php?id=<?php echo $row['id']?>">
            <span class="icon-edit"></span> 修改</a>
        <a class="button border-red" href="del.php?idD=<?php echo $row['id']?>" onclick="return del(1,1,1)">
            <span class="icon-trash-o"></span>删除</a>
    </div>
    </td>
  </tr>
<?php endwhile;?>

我们只需要将查询的数据在前端页面循环展示出来就行。

我们将查询出来的值以数组的形式赋值给$row,通过while循环出来。在前端页面对应的地方以<?php echo $row['']?>的形式循环出来。这样我们的展示页面就算是做完了。

下面一节我们会说一下分页和关键词搜索。

下一节
<?php session_start(); header("content-type:text/html;charset=utf-8"); require 'config.php'; $SQL = "select * from list";//设置查询指令 $result=mysqli_query($link,$SQL);//执行查询 ?> <!DOCTYPE html> <html lang="zh-cn"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" /> <meta name="renderer" content="webkit"> <title></title> <link rel="stylesheet" href="css/pintuer.css"> <link rel="stylesheet" href="css/admin.css"> <script src="js/jquery.js"></script> <script src="js/pintuer.js"></script> </head> <body> <form method="get" action="" id="listform"> <div class="panel admin-panel"> <div class="panel-head"><strong class="icon-reorder"> 内容列表</strong> <a href="" style="float:right; display:none;">添加字段</a></div> <div class="padding border-bottom"> <ul class="search" style="padding-left:10px;"> <li> <a class="button border-main icon-plus-square-o" href="add.php"> 添加内容</a> </li> <li> <select name="cid" class="input" style="width:200px; line-height:17px;" onchange="changesearch()"> <option value="">请选择分类</option> <?php foreach($cat_array as $k=>$vo){ echo "<option value='{$k}'".($k==$_GET['cid']?' selected':'').">{$vo}</option>"; } ?> </select> <li> <input type="text" placeholder="请输入搜索关键字" name="key" class="input" style="width:250px; line-height:17px;display:inline-block" value="<?php echo $_GET[key];?>"/> <input type="submit" name="sub" class="button border-main icon-search" value="搜索" /> </li> </ul> </div> <table class="table table-hover text-center"> <tr> <th width="100" style="text-align:left; padding-left:20px;">ID</th> <th width="10%">作者</th> <th>图片</th> <th>内容</th> <th>评论</th> <th>分类名称</th> <th width="10%">发布时间</th> <th width="310">操作</th> </tr> <?php while($row=mysqli_fetch_assoc($result)): ?> <tr> <td style="text-align:left; padding-left:20px;"><input type="checkbox" name="id" value="" /><?php echo $row['id'];?></td> <td><span><?php echo $row['author'];?></span></td> <td width="10%"><img src="<?php echo $row['image'];?>" alt="" width="70" height="50" /></td> <td width="30%"><span><?php echo $row['content'];?></span></td> <td><span>首页</span></td> <td><span><?php echo $cat_array[$row['cid']];?></span></td> <td><span>2016-07-01</span></td> <td><div class="button-group"> <a class="button border-main" href="edit.php?id=<?php echo $row['id']?>"> <span class="icon-edit"></span> 修改</a> <a class="button border-red" href="del.php?idD=<?php echo $row['id']?>" onclick="return del(1,1,1)"> <span class="icon-trash-o"></span>删除</a> </div> </td> </tr> <?php endwhile;?> </html>
提交 重置代码
章节 评论 笔记 课件
  • 取消 回复 发送
  • 取消 发布笔记 发送