List display

After adding data to the database, we then want to display the data in the database on our front page. This is our display function.

First of all, if we want to display the data in the database, we need to connect to our database. As mentioned before, there will be many places where the database connection code is used. It will be very troublesome to write it every time. , we re-create a file, write the connection database into it, and then call it.

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

Name the new file config.php, just quote it directly

The next step is to query the data from the database and display the found data on the front-end page .

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

Find the data from the list data table, and then display it on the front-end page. Let's take a look at the front-end file.

<?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;?>

We only need to display the queried data in a loop on the front-end page.

We assign the queried value to $row in the form of an array and loop it out through the while loop. Loop out in the corresponding place on the front-end page in the form of <?php echo $row['']?>. In this way, our display page is completed.

We will talk about paging and keyword search in the next section.

Continuing Learning
||
<?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>
submitReset Code