목록 표시로그인

목록 표시

데이터베이스에 데이터를 추가한 후 첫 페이지에 데이터베이스의 데이터를 표시하려고 합니다.

먼저 데이터베이스에 있는 데이터를 표시하려면 데이터베이스에 연결해야 합니다. 앞서 언급했듯이 데이터베이스 연결 코드가 사용되는 곳이 많을 것입니다. 매번 파일을 다시 만들고 연결 데이터베이스를 작성한 다음 호출해 보겠습니다.

<?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);//执行查询
?>

목록 데이터 테이블에서 데이터를 찾아 프런트엔드 페이지에 표시해 보겠습니다.

<?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>
코스웨어