PHP? ?? ?? ???? ?? ??? ???? ?? ??? ?? ??? ???? ?????.
?? ???? ??? ??? ??? ?? ??? ?? ????? ??? ???? ??? ???? ?????.
?? ??????? ?? ??? ???? ???
??? ??? ??? ???? ?? ?? 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>
??: ?? ? ?? ??? id? ?? ?????. ???? ?? ? ?? PHP ?? ?? ??? ?? ??? ?????.
<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(???? ??) ???? ?? ?? ??? ?????. LIKE ??? ????? ??? ??? ??? ? ????, ????? ??? "%" ?? "_" ????? ??? ???? ?? ?? ??? ?????. ??. ? ?? ?? ??? ????: like 'keyword%', like '%keyword', like'%keyword%'. <?php
$sql = "select * from new where title like '%$keyword%' or content like '%$keyword%'limit {$limitFrom}, {$limitNews}";
?>
??? ?? ??? ???? ??
<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>
<?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>文章內(nèi)容</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 ??? ??