亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

PHP開(kāi)發(fā)簡(jiǎn)單新聞發(fā)布系統(tǒng)之新聞列表頁(yè)整體功能實(shí)現(xiàn)

前面的章節(jié)我們說(shuō)到了怎麼實(shí)現(xiàn)簡(jiǎn)單的分頁(yè)效果,這裡我們就不再詳細(xì)講解了。

首先我們要把資料庫(kù)中的新聞紀(jì)錄展示出來(lái)

這裡我們需要用到?select 欄位名稱1 , 欄位2 , … from 資料表名 來(lái)取得新聞資料

<?php
$sql = "select * from new";  //獲取所有數(shù)據(jù)

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

$arr = mysqli_fetch_array($result);  //獲取所有數(shù)據(jù)并用數(shù)組形式展示

var_dump($arr);  //可以通過(guò)var_dump()打印出來(lái)觀察
?>

透過(guò)while循環(huán)把獲取的資料庫(kù)資料展示在HTML頁(yè)面中

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

註解:修改功能和刪除功能透過(guò)id 來(lái)實(shí)現(xiàn),具體修改和刪除PHP程式碼實(shí)作功能會(huì)在後面的章節(jié)介紹。

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


這裡我們?cè)黾恿艘粋€(gè)搜尋功能,我們給定一個(gè)函數(shù)$keyword,透過(guò)$_GET來(lái)獲取資料

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

對(duì)新聞標(biāo)題和新聞內(nèi)容搜索,就要使用SQL語(yǔ)句中的模糊查找

主要透過(guò)LIKE(不區(qū)分大小寫)關(guān)鍵字實(shí)現(xiàn)模糊查找。 LIKE條件一般用在指定搜尋某欄位的時(shí)候, 透過(guò)"%"或" _" 通配符的作用實(shí)現(xiàn)模糊查找功能,通配符可以在欄位前面也可以在後面或前後都有。

主要有:?like ?'關(guān)鍵字%' ,??like ?'%關(guān)鍵字', like'%關(guān)鍵字%' 三種。

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

為了實(shí)現(xiàn)分頁(yè)搜尋功能,我們?cè)贖TML分頁(yè)程式碼中加入了keyword=<?php echo $keyword;?>

#
<body>
<a href="?page=<?php echo $prev;?>&keyword=<?php echo $keyword;?>">上一頁(yè)|</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;?>">|下一頁(yè)</a>
</body>

展示完整list.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;//獲取當(dāng)前分頁(yè)數(shù)
$limitNews = 3;   //每頁(yè)顯示新聞數(shù)量, 這里設(shè)置每頁(yè)顯示3條新聞
$countNews = 0;   //總共有多少條新聞
$countPage = 0;   //一共有多少頁(yè)數(shù)

$limitFrom = ($page - 1) * $limitNews;//從第幾條數(shù)據(jù)開(kāi)始讀記錄
//每頁(yè)顯示3個(gè)
//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); //查詢數(shù)量sql語(yǔ)句
$retCount = mysqli_fetch_array($retQuery);  //獲取數(shù)量
$count = $retCount[0]?$retCount[0]:0;  //判斷獲取的新聞數(shù)量
$countNews = $count;

$countPage = $countNews%$limitNews;  //求余數(shù)獲取分頁(yè)數(shù)量能否被除盡
if(($countPage) > 0) {  //獲取的頁(yè)數(shù)有余
 $countPage = ceil($countNews/$limitNews);
 // ceil()函數(shù)向上舍入為最接近的整數(shù),除不盡則取整數(shù)+1頁(yè), 10個(gè)新聞每個(gè)頁(yè)面顯示3個(gè),成3個(gè)頁(yè)面,剩余1個(gè)成1個(gè)頁(yè)面
} 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>新聞列表頁(yè)</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>編號(hào)</th>
     <th>文章標(biāo)題</th>
     <th>文章作者</th>
     <th>文章內(nèi)容</th>
     <th>發(fā)布時(shí)間</th>
     <th>修改時(shí)間</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;?>頁(yè) |查到<?php echo $countNews;?>條記錄
   當(dāng)前第<?php echo $page;?>頁(yè)|
   <a href="?page=<?php echo $prev;?>&keyword=<?php echo $keyword;?>">上一頁(yè)|</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;?>">|下一頁(yè)</a>
 </div>
</body>
</html>


#
繼續(xù)學(xué)習(xí)
||
<?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;//獲取當(dāng)前分頁(yè)數(shù) $limitNews = 3; //每頁(yè)顯示新聞數(shù)量, 這里設(shè)置每頁(yè)顯示3條新聞 $countNews = 0; //總共有多少條新聞 $countPage = 0; //一共有多少頁(yè)數(shù) $limitFrom = ($page - 1) * $limitNews;//從第幾條數(shù)據(jù)開(kāi)始讀記錄 //每頁(yè)顯示3個(gè) //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); //查詢數(shù)量sql語(yǔ)句 $retCount = mysqli_fetch_array($retQuery); //獲取數(shù)量 $count = $retCount[0]?$retCount[0]:0; //判斷獲取的新聞數(shù)量 $countNews = $count; $countPage = $countNews%$limitNews; //求余數(shù)獲取分頁(yè)數(shù)量能否被除盡 if(($countPage) > 0) { //獲取的頁(yè)數(shù)有余 $countPage = ceil($countNews/$limitNews); // ceil()函數(shù)向上舍入為最接近的整數(shù),除不盡則取整數(shù)+1頁(yè), 10個(gè)新聞每個(gè)頁(yè)面顯示3個(gè),成3個(gè)頁(yè)面,剩余1個(gè)成1個(gè)頁(yè)面 } 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>新聞列表頁(yè)</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>編號(hào)</th> <th>文章標(biāo)題</th> <th>文章作者</th> <th>文章內(nèi)容</th> <th>發(fā)布時(shí)間</th> <th>修改時(shí)間</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;?>頁(yè) |查到<?php echo $countNews;?>條記錄 當(dāng)前第<?php echo $page;?>頁(yè)| <a href="?page=<?php echo $prev;?>&keyword=<?php echo $keyword;?>">上一頁(yè)|</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;?>">|下一頁(yè)</a> </div> </body> </html>
提交重置程式碼