PHP membangunkan sistem siaran berita mudah untuk merealisasikan fungsi keseluruhan halaman senarai berita

Dalam bab sebelumnya, kami bercakap tentang cara mencapai kesan paging yang mudah, jadi kami tidak akan menerangkannya secara terperinci di sini.

Mula-mula kita perlu memaparkan rekod berita dalam pangkalan data

Di sini kita perlu menggunakan pilih nama medan 1, medan 2, ... daripada nama jadual data hingga mendapatkan data Berita

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

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

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

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

memaparkan data pangkalan data yang diperoleh dalam halaman HTML melalui gelung sementara

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

Nota: Fungsi pengubahsuaian dan fungsi pemadaman dilaksanakan melalui id, dan pengubahsuaian khusus dan pemadaman kod PHP Fungsi pelaksanaan akan diperkenalkan dalam bab kemudian.

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


Di sini kami menambah fungsi carian, kami memberikan fungsi $kata kunci, pas $_GET Untuk mendapatkan data

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

Untuk mencari tajuk berita dan kandungan berita, mesti menggunakan carian kabur dalam pernyataan SQL

Carian kabur terutamanya dilaksanakan melalui kata kunci LIKE (tidak sensitif huruf besar-besaran). Keadaan LIKE biasanya digunakan apabila menentukan medan untuk dicari, dan fungsi carian kabur dilaksanakan melalui penggunaan aksara "%" atau "_" Aksara kad bebas boleh berada di hadapan medan, atau di belakang atau kedua belah pihak.

Terdapat tiga jenis utama: seperti 'kata kunci%', seperti '%keyword', seperti'%keyword%'.

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

Untuk melaksanakan fungsi carian halaman, kami menambah kata kunci=<?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>
dalam HTML kod paging

Tunjukkan senarai lengkap kod.php:

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


Meneruskan pembelajaran
||
<?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>
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!