php原生開發(fā)新聞?wù)局侣劻斜恚ǘ?/h1>
上節(jié)課我們把新聞的數(shù)據(jù)表創(chuàng)建完成了,那么今天我們就給大家講解下如何做新聞列表展示頁,那么在后臺模板中找到我們的內(nèi)容管理的頁面~,然后把我們不需要的以及多余的代碼給刪除掉!
然后我們就可以開始做我們的新聞列表頁了!
第一步:連接數(shù)據(jù)庫
<?php
// 顯示所有的錯誤
error_reporting(E_ALL & ~E_NOTICE );
// 連接mysql數(shù)據(jù)庫
$link = mysqli_connect('localhost','root', 'root');
if (!$link) {
echo "connect mysql error!";
exit();
}
// 選中數(shù)據(jù)庫 news為數(shù)據(jù)庫的名字
$db_selected = mysqli_select_db($link, 'news');
if (!$db_selected) {
echo "<br>selected db error!";
exit();
}
// 設(shè)置mysql字符集 為 utf8
$link->query("set names utf8");
?>
第二步:查詢數(shù)據(jù)表里的所有的內(nèi)容并執(zhí)行SQL語句
$sql = "select * from new where 1 "; // 查詢語句
$result = mysqli_query($link, $sql);
$arr_news = mysqli_fetch_all($result, MYSQL_ASSOC);
第三步:現(xiàn)在我們已經(jīng)把數(shù)據(jù)表的數(shù)據(jù)都獲取到了,現(xiàn)在就是要他數(shù)據(jù)循環(huán)輸出,在模板里找到要輸出數(shù)據(jù)的位置,然后取數(shù)據(jù)!
<table class="table table-hover text-center">
<tr>
<th width="100" style="text-align:left; padding-left:20px;">ID</th>
<th>分類名</th>
<th>標(biāo)題</th>
<th>內(nèi)容</th>
<th>關(guān)鍵字</th>
<th>圖片</th>
<th>作者</th>
<th width="10%">更新時間</th>
<th width="310">操作</th>
</tr>
<?php
if($arr_news){
foreach ($arr_news as $val){
echo "<tr>";
echo " <td style='text-align:left; padding-left:20px;'>
<input type='checkbox' name='id[]' value='' />{$val['id']}</td>";
echo "<td>{$new_category_value[$val['category_id']]}</td>";
echo "<td>". mb_substr($val['title'], 0,15,"utf-8")."</td>";
echo "<td>". mb_substr($val['content'], 0,20,"utf-8")."</td>";
echo "<td>{$val['tag']}</td>";
if($val['pic']){
echo "<td ><img src='{$val['pic']}' style='width: 50px; height: 50px'></td>";
}else{
echo "<td>暫無圖片</td>";
}
echo "<td>{$val['author']}</td>";
echo "<td>{$val['created_at']}</td>";
?>
<td>
<div class='button-group'>
<a class='button border-main' href='new_edit.php?id=<?php echo $val['id'];?>'>
<span class='icon-edit'></span> 修改</a>
<a class='button border-red' href='javascript:;' onclick='return del(<?php echo $val['id']?>)'>
<span class='icon-trash-o'></span> 刪除</a>
</div>
</td>
<?
echo "</tr>";
}
}
?>
我們這里使用的是foreach 來遍歷數(shù)據(jù)了,當(dāng)然還有很多循環(huán)的方法,大家可以根據(jù)自己的熟悉的來完成功能~
說明一下:我們這里有個分類名,這個就是我們的分類的內(nèi)容!所以我們這里不僅要對新聞表查詢,我們也要對分類表查詢!
<?php
$sql = "select * from new_category ";
$result = mysqli_query($link, $sql);
$new_category = mysqli_fetch_all($result, MYSQL_ASSOC);
$new_category_value = array();
foreach($new_category as $val ){
$new_category_value[$val['id']] = $val['name'];
}
?>
取出分類表并遍歷分類表!

如上圖所以,所有的新聞都展示出來!
上節(jié)課我們把新聞的數(shù)據(jù)表創(chuàng)建完成了,那么今天我們就給大家講解下如何做新聞列表展示頁,那么在后臺模板中找到我們的內(nèi)容管理的頁面~,然后把我們不需要的以及多余的代碼給刪除掉!
然后我們就可以開始做我們的新聞列表頁了!
第一步:連接數(shù)據(jù)庫
<?php // 顯示所有的錯誤 error_reporting(E_ALL & ~E_NOTICE ); // 連接mysql數(shù)據(jù)庫 $link = mysqli_connect('localhost','root', 'root'); if (!$link) { echo "connect mysql error!"; exit(); } // 選中數(shù)據(jù)庫 news為數(shù)據(jù)庫的名字 $db_selected = mysqli_select_db($link, 'news'); if (!$db_selected) { echo "<br>selected db error!"; exit(); } // 設(shè)置mysql字符集 為 utf8 $link->query("set names utf8"); ?>
第二步:查詢數(shù)據(jù)表里的所有的內(nèi)容并執(zhí)行SQL語句
$sql = "select * from new where 1 "; // 查詢語句 $result = mysqli_query($link, $sql); $arr_news = mysqli_fetch_all($result, MYSQL_ASSOC);
第三步:現(xiàn)在我們已經(jīng)把數(shù)據(jù)表的數(shù)據(jù)都獲取到了,現(xiàn)在就是要他數(shù)據(jù)循環(huán)輸出,在模板里找到要輸出數(shù)據(jù)的位置,然后取數(shù)據(jù)!
<table class="table table-hover text-center"> <tr> <th width="100" style="text-align:left; padding-left:20px;">ID</th> <th>分類名</th> <th>標(biāo)題</th> <th>內(nèi)容</th> <th>關(guān)鍵字</th> <th>圖片</th> <th>作者</th> <th width="10%">更新時間</th> <th width="310">操作</th> </tr> <?php if($arr_news){ foreach ($arr_news as $val){ echo "<tr>"; echo " <td style='text-align:left; padding-left:20px;'> <input type='checkbox' name='id[]' value='' />{$val['id']}</td>"; echo "<td>{$new_category_value[$val['category_id']]}</td>"; echo "<td>". mb_substr($val['title'], 0,15,"utf-8")."</td>"; echo "<td>". mb_substr($val['content'], 0,20,"utf-8")."</td>"; echo "<td>{$val['tag']}</td>"; if($val['pic']){ echo "<td ><img src='{$val['pic']}' style='width: 50px; height: 50px'></td>"; }else{ echo "<td>暫無圖片</td>"; } echo "<td>{$val['author']}</td>"; echo "<td>{$val['created_at']}</td>"; ?> <td> <div class='button-group'> <a class='button border-main' href='new_edit.php?id=<?php echo $val['id'];?>'> <span class='icon-edit'></span> 修改</a> <a class='button border-red' href='javascript:;' onclick='return del(<?php echo $val['id']?>)'> <span class='icon-trash-o'></span> 刪除</a> </div> </td> <? echo "</tr>"; } } ?>
我們這里使用的是foreach 來遍歷數(shù)據(jù)了,當(dāng)然還有很多循環(huán)的方法,大家可以根據(jù)自己的熟悉的來完成功能~
說明一下:我們這里有個分類名,這個就是我們的分類的內(nèi)容!所以我們這里不僅要對新聞表查詢,我們也要對分類表查詢!
<?php $sql = "select * from new_category "; $result = mysqli_query($link, $sql); $new_category = mysqli_fetch_all($result, MYSQL_ASSOC); $new_category_value = array(); foreach($new_category as $val ){ $new_category_value[$val['id']] = $val['name']; } ?>
取出分類表并遍歷分類表!
如上圖所以,所有的新聞都展示出來!