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

PHP開發(fā)簡單新聞發(fā)布系統(tǒng)之新聞列表頁整體功能實現(xiàn)

前面的章節(jié)我們說到了怎么實現(xiàn)簡單的分頁效果,這里我們就不再詳細講解了。

首先我們要把數據庫中的新聞紀錄展示出來

這里我們需要用到?select 字段名1 , 字段2 , …… from 數據表名 來獲取新聞數據

<?php
$sql = "select * from new";  //獲取所有數據

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

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

var_dump($arr);  //可以通過var_dump()打印出來觀察
?>

通過while循環(huán)把獲取的數據庫數據展示在HTML頁面中

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

注釋: 修改功能和刪除功能通過 id 來實現(xiàn),具體修改和刪除PHP代碼實現(xiàn)功能會在后面的章節(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>


這里我們增加了一個搜索功能,我們給定一個函數$keyword,通過$_GET來獲取數據

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

對新聞標題和新聞內容搜索,就要使用SQL語句中的模糊查找

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

主要有:?like ?'關鍵字%' ,??like ?'%關鍵字', like'%關鍵字%' 三種。

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

為了實現(xiàn)分頁搜索功能,我們在HTML分頁代碼中加入了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>

展示完整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;//獲取當前分頁數
$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>發(fā)布時間</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>


繼續(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;//獲取當前分頁數 $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>發(fā)布時間</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>
提交重置代碼