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

PHP は、ニュース一覧ページの全體的な機能を実裝するためのシンプルなニュース リリース システムを開発します

前の章では、単純なページング効果を実現(xiàn)する方法について説明したため、ここでは詳しく説明しません。

まず、データベース內のニュースレコードを表示する必要があります

ここでは、データテーブル名からフィールド名1、フィールド2、...を選択してニュースデータを取得する必要があります

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

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

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

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

取得したデータベースを実行しますwhile ループ データは 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>

注: 変更および削除関數(shù)は id を通じて実裝されます。具體的な変更および削除の PHP コード実裝関數(shù)については、後の章で紹介します。

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


ここでは関數(shù)

$keywordを與え、$_GETを通じてデータを取得します

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

ニュースタイトルとニュースコンテンツを検索するには、

を使用する必要があります SQL ステートメントのあいまい検索

は、主に LIKE (大文字と小文字を區(qū)別しない) キーワードによるあいまい検索を実裝します。 LIKE 條件は通常、検索するフィールドを指定するときに使用され、あいまい検索機能は「%」または「_」ワイルドカード文字を使用して実裝されます。ワイルドカード文字はフィールドの前、後ろ、またはフィールドに置くことができます。両側。

主に 3 つのタイプがあります: like 'keyword%'、like '%keyword'、like'%keyword%'。

<?php
$sql = "select * from new where title like '%$keyword%' or content like '%$keyword%'limit {$limitFrom}, {$limitNews}";
?>
ページング検索機能を実裝するために、

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>

Show the complete 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;//獲取當前分頁數(shù)
$limitNews = 3;   //每頁顯示新聞數(shù)量, 這里設置每頁顯示3條新聞
$countNews = 0;   //總共有多少條新聞
$countPage = 0;   //一共有多少頁數(shù)

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

$countPage = $countNews%$limitNews;  //求余數(shù)獲取分頁數(shù)量能否被除盡
if(($countPage) > 0) {  //獲取的頁數(shù)有余
 $countPage = ceil($countNews/$limitNews);
 // ceil()函數(shù)向上舍入為最接近的整數(shù),除不盡則取整數(shù)+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>

を追加しました。 HTML ページング コード

學び続ける
||
<?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;//獲取當前分頁數(shù) $limitNews = 3; //每頁顯示新聞數(shù)量, 這里設置每頁顯示3條新聞 $countNews = 0; //總共有多少條新聞 $countPage = 0; //一共有多少頁數(shù) $limitFrom = ($page - 1) * $limitNews;//從第幾條數(shù)據(jù)開始讀記錄 //每頁顯示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); //查詢數(shù)量sql語句 $retCount = mysqli_fetch_array($retQuery); //獲取數(shù)量 $count = $retCount[0]?$retCount[0]:0; //判斷獲取的新聞數(shù)量 $countNews = $count; $countPage = $countNews%$limitNews; //求余數(shù)獲取分頁數(shù)量能否被除盡 if(($countPage) > 0) { //獲取的頁數(shù)有余 $countPage = ceil($countNews/$limitNews); // ceil()函數(shù)向上舍入為最接近的整數(shù),除不盡則取整數(shù)+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>