PHP develops a simple news release system to realize the overall function of the news list page

In the previous chapter, we talked about how to achieve a simple paging effect, so we won’t explain it in detail here.

First we need to display the news records in the database

Here we need to use select field name 1, field 2, ... from data table name to obtain News data

<?php
$sql = "select * from new";  //获取所有数据

$result = mysqli_query($link, $sql);

$arr = mysqli_fetch_array($result);  //获取所有数据并用数组形式展示

var_dump($arr);  //可以通过var_dump()打印出来观察
?>

Display the obtained database data in the HTML page through a while loop

<body>
<?php while($arr=mysqli_fetch_array($result)):?>

    <tr>
      <td align="center" style="border:1px solid #000;width: 8%;"><?php echo $arr['id'];?></td>
      <td align="center" style="border:1px solid #000; width: 10%;"><?php echo $arr['title'];?></td>
      <td align="center" style="border:1px solid #000; width: 10%;"><?php echo $arr['author'];?></td>
      <td align="center" style="border:1px solid #000; width: 15%;"><?php echo $arr['content'];?></td>
      <td align="center" style="border:1px solid #000; width: 10%;"><?php echo $arr['created_at'];?></td>           
      <td align="center" style="border:1px solid #000; width: 10%;">
        <a href="edit.php?id=<?php echo $arr['id']?>"><font color="red">修改</font></a>
        <a href="delete.php?id=<?php echo $arr['id']?>"><font color="red">删除</font></a>
      </td>
    </tr>
    
<?php endwhile;?>
</body>

Note: The modification function and deletion function are implemented through id, specifically modify and delete the PHP code The implementation functions will be introduced in later chapters.

<body>
<a href="edit.php?id=<?php echo $arr['id']?>"><font color="red">修改</font></a>
<a href="delete.php?id=<?php echo $arr['id']?>"><font color="red">删除</font></a>
</body>


Here we have added a search function, we give a function $keyword, pass$_GET To obtain data

<?php
$keyword = isset($_GET['keyword'])?$_GET['keyword']:"";  // 判断获取的$keyword
?>

To search for news titles and news content,need to use fuzzy search inSQL statements

Fuzzy search is mainly implemented through the LIKE (case-insensitive) keyword. LIKE conditions are generally used when specifying a field to search for, and the fuzzy search function is implemented through the use of "%" or "_" wildcard characters. The wildcard character can be in front of the field, or behind or both before and after the field.

There are three main types: like 'keyword%', like '%keyword', like'%keyword%'.

<?php
$sql = "select * from new where title like '%$keyword%' or content like '%$keyword%'limit {$limitFrom}, {$limitNews}";
?>

In order to implement the paging search function, we added keyword=<?php echo $keyword;?>

<body>
<a href="?page=<?php echo $prev;?>&keyword=<?php echo $keyword;?>">上一页|</a>
<?php for($i=1; $i<=$countPage; $i++):?>
    <a href="?page=<?php echo $i;?>&keyword=<?php echo $keyword;?>"><?php echo $i;?></a>
<?php endfor;?>
<a href="?page=<?php echo $next;?>&keyword=<?php echo $keyword;?>">|下一页</a>
</body>
in the HTML paging code

Show the complete list.php code:

<?php
$link = mysqli_connect('localhost','username','password','test');
  mysqli_set_charset($link, "utf8");
  
if (!$link) {
 die("连接失败:".mysqli_connect_error());
}

$keyword = isset($_GET['keyword'])?$_GET['keyword']:"";
$page = isset($_GET['page'])?$_GET['page']:1;//获取当前分页数
$limitNews = 3;   //每页显示新闻数量, 这里设置每页显示3条新闻
$countNews = 0;   //总共有多少条新闻
$countPage = 0;   //一共有多少页数

$limitFrom = ($page - 1) * $limitNews;//从第几条数据开始读记录
//每页显示3个
//page = l  limit 0
//page = 2  limit 3
//page = 3  limit 6

$sql = "select * from new where title like '%$keyword%' or content like '%$keyword%'limit {$limitFrom}, {$limitNews}";
$sqlCount = "select count(*) from new where title like '%$keyword%' or content like '%$keyword%'";
$retQuery = mysqli_query($link, $sqlCount); //查询数量sql语句
$retCount = mysqli_fetch_array($retQuery);  //获取数量
$count = $retCount[0]?$retCount[0]:0;  //判断获取的新闻数量
$countNews = $count;

$countPage = $countNews%$limitNews;  //求余数获取分页数量能否被除尽
if(($countPage) > 0) {  //获取的页数有余
 $countPage = ceil($countNews/$limitNews);
 // ceil()函数向上舍入为最接近的整数,除不尽则取整数+1页, 10个新闻每个页面显示3个,成3个页面,剩余1个成1个页面
} else {
 $countPage = $countNews/$limitNews;
}

$prev = ($page - 1 <= 0 )?1:$page-1;
$next = ($page + 1 > $countPage)?$countPage:$page+1;

$result = mysqli_query($link, $sql);
?>
<!DOCTYPE html>
<html>
<head>
 <meta charset="utf8">
 <title>新闻列表页</title>
</head>
<body>
       <!--搜索框-->
 <form method="get" action="list.php" style="margin:10px;">
   <input type="text" name="keyword" value="<?php echo $keyword;?>"/>
   <input type="submit" value="搜索"/>
 </form>
 <br/>
 <table cellspacing="0" cellpadding="0" align="center" bgcolor="#ccc" width=1000 >
   <tr>
     <th>编号</th>
     <th>文章标题</th>
     <th>文章作者</th>
     <th>文章内容</th>
     <th>发布时间</th>
     <th>修改时间</th>
     <th>编辑文章</th>
   </tr>
     <?php while($arr=mysqli_fetch_array($result)):?>
     <tr>
        <td align="center" style="border:1px solid #000;width: 8%;"><?php echo $arr['id'];?></td>
             <td align="center" style="border:1px solid #000; width: 10%;"><?php echo $arr['title'];?></td>
             <td align="center" style="border:1px solid #000; width: 10%;"><?php echo $arr['author'];?></td>
             <td align="center" style="border:1px solid #000; width: 15%;"><?php echo $arr['content'];?></td>
             <td align="center" style="border:1px solid #000; width: 10%;"><?php echo $arr['created_at'];?></td>
             <td align="center" style="border:1px solid #000; width: 10%;">
                <a href="edit.php?id=<?php echo $arr['id']?>"><font color="red">修改</font></a>
                <a href="delete.php?id=<?php echo $arr['id']?>"><font color="red">删除</font></a>
             </td>
         </tr>
     <?php endwhile;?>
 </table>
 <div style="margin:20px;">
   共<?php echo $countPage;?>页 |查到<?php echo $countNews;?>条记录
   当前第<?php echo $page;?>页|
   <a href="?page=<?php echo $prev;?>&keyword=<?php echo $keyword;?>">上一页|</a>
   <?php for($i=1; $i<=$countPage; $i++):?>
     <a href="?page=<?php echo $i;?>&keyword=<?php echo $keyword;?>"><?php echo $i;?></a>
   <?php endfor;?>
   <a href="?page=<?php echo $next;?>&keyword=<?php echo $keyword;?>">|下一页</a>
 </div>
</body>
</html>


Continuing Learning
||
<?php $link = mysqli_connect('localhost','username','password','test'); mysqli_set_charset($link, "utf8"); if (!$link) { die("连接失败:".mysqli_connect_error()); } $keyword = isset($_GET['keyword'])?$_GET['keyword']:""; $page = isset($_GET['page'])?$_GET['page']:1;//获取当前分页数 $limitNews = 3; //每页显示新闻数量, 这里设置每页显示3条新闻 $countNews = 0; //总共有多少条新闻 $countPage = 0; //一共有多少页数 $limitFrom = ($page - 1) * $limitNews;//从第几条数据开始读记录 //每页显示3个 //page = l limit 0 //page = 2 limit 3 //page = 3 limit 6 $sql = "select * from new where title like '%$keyword%' or content like '%$keyword%'limit {$limitFrom}, {$limitNews}"; $sqlCount = "select count(*) from new where title like '%$keyword%' or content like '%$keyword%'"; $retQuery = mysqli_query($link, $sqlCount); //查询数量sql语句 $retCount = mysqli_fetch_array($retQuery); //获取数量 $count = $retCount[0]?$retCount[0]:0; //判断获取的新闻数量 $countNews = $count; $countPage = $countNews%$limitNews; //求余数获取分页数量能否被除尽 if(($countPage) > 0) { //获取的页数有余 $countPage = ceil($countNews/$limitNews); // ceil()函数向上舍入为最接近的整数,除不尽则取整数+1页, 10个新闻每个页面显示3个,成3个页面,剩余1个成1个页面 } else { $countPage = $countNews/$limitNews; } $prev = ($page - 1 <= 0 )?1:$page-1; $next = ($page + 1 > $countPage)?$countPage:$page+1; $result = mysqli_query($link, $sql); ?> <!DOCTYPE html> <html> <head> <meta charset="utf8"> <title>新闻列表页</title> </head> <body> <!--搜索框--> <form method="get" action="list.php" style="margin:10px;"> <input type="text" name="keyword" value="<?php echo $keyword;?>"/> <input type="submit" value="搜索"/> </form> <br/> <table cellspacing="0" cellpadding="0" align="center" bgcolor="#ccc" width=1000 > <tr> <th>编号</th> <th>文章标题</th> <th>文章作者</th> <th>文章内容</th> <th>发布时间</th> <th>修改时间</th> <th>编辑文章</th> </tr> <?php while($arr=mysqli_fetch_array($result)):?> <tr> <td align="center" style="border:1px solid #000;width: 8%;"><?php echo $arr['id'];?></td> <td align="center" style="border:1px solid #000; width: 10%;"><?php echo $arr['title'];?></td> <td align="center" style="border:1px solid #000; width: 10%;"><?php echo $arr['author'];?></td> <td align="center" style="border:1px solid #000; width: 15%;"><?php echo $arr['content'];?></td> <td align="center" style="border:1px solid #000; width: 10%;"><?php echo $arr['created_at'];?></td> <td align="center" style="border:1px solid #000; width: 10%;"> <a href="edit.php?id=<?php echo $arr['id']?>"><font color="red">修改</font></a> <a href="delete.php?id=<?php echo $arr['id']?>"><font color="red">删除</font></a> </td> </tr> <?php endwhile;?> </table> <div style="margin:20px;"> 共<?php echo $countPage;?>页 |查到<?php echo $countNews;?>条记录 当前第<?php echo $page;?>页| <a href="?page=<?php echo $prev;?>&keyword=<?php echo $keyword;?>">上一页|</a> <?php for($i=1; $i<=$countPage; $i++):?> <a href="?page=<?php echo $i;?>&keyword=<?php echo $keyword;?>"><?php echo $i;?></a> <?php endfor;?> <a href="?page=<?php echo $next;?>&keyword=<?php echo $keyword;?>">|下一页</a> </div> </body> </html>
submitReset Code