PHP仿博客园 个人博客(2) 数据库增添改删(2)

我们定义了一个数组,$results = array( ); 这个数组的作用明显,它将保存我们从 model 中获取的任何数据,也可以保存从url上 GET 的特殊参数。然后将在我们下面require_once(*****) 包含的模版中显示出来, 路径定义在了 path 变量中。

同时我们会接收2个提示参数,

error , 表示操作出现错误,任何人都在所难免,包括电脑,谁都会犯错,关键是去承认,电脑做的很好,他们勇于承认错误。

status; 表示状态,就是成功的操作。

$pagination = new Pagination; 这个类是我们的分页类,我们传入一个 总的数量给它,然后它自己会算出总页数,每跳转一个页面,相当于刷新了一次,所以大家的做法就是,在构造器里 GET(获取)url上的page 的值,让我们知道是当前那一页了。同时我们重新生成了查询的语句,后面加上一条限制的语句,类似 limit $start(起始的id), $offset(长度); 原理就是从这个id起,往后给我10 条记录;我的设定就是 10 条,你也可以更灵活。
$cat = new Category;这个类后面会详细说,也是非常重要的分类model。这里我们就是简单获取 这个类型下的所有分类,显示在侧边栏,我已经完成了。有图有真相!


PHP仿博客园 个人博客(2) 数据库增添改删

这样 我们的 $results 数组中就储存了我们页面所需的所有数据。 好的,来看看我们的模版,是怎么输出的。

复制代码 代码如下:


 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 <html>
     <head>
         <title>
             博客后台管理</title>
             <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
             <link type="text/css" href="https://www.jb51.net/assert/css/blog.css" />          
     </head>   
     <body>
             <table cellpadding="0" cellspacing="0">
                 <tr>
                     <td colspan="2"><div></div>           
                         <div>
                             Arist's Blog
                         </div>
                         <div>
                             <b><blockquote>Hinging there, everything will be fine.</blockquote></b>
                         </div>
                     </td>
                 </tr>
                 <tr>
                     <td>
                         <div>操作</div>
                     </td>
                     <td>
                         <ul>
                             <li><a href="https://www.jb51.net/post.php?action=IsPost">随笔</a></li>
                             <li><a href="https://www.jb51.net/article.php?action=IsArticle">文章</a></li>
                             <li><a href="https://www.jb51.net/diary.php?action=IsDiary">日记</a></li>
                             <li><a href="https://www.jb51.net/comment.php?action=IsComment">评论</a></li>
                             <li><a href="https://www.jb51.net/photo.php?action=IsPhoto">相片</a></li>                           
                         </ul>
                         <div>
                             当前位置:&nbsp;<?php if( isset( $results['path'] )) echo $results['path']; ?>
                         </div>
                     </td>
                 </tr>
                 <tr>
                     <td>                       
                         <div>
                             <ul>
                                 <li><a href="https://www.jb51.net/article.php?action=newArticle">&raquo; 添加新文章</a></li>
                             </ul>
                         </div>
                         <div>
                             分类
                         </div>
                         <div>
                             <ul>
                             <li><a href="https://www.jb51.net/category.php?action=ListCat&type=article">[编辑分类]</a></li>
                             <li><a href="https://www.jb51.net/article.php?action=IsArticle">[所有分类]</a></li>
                             <li><a href="https://www.jb51.net/article.php?action=unCategory">[未分类]</a></li>
 <?php
 if( isset( $results['categories'] )  && ! empty( $results['categories'] ) ){
     foreach( $results['categories'] as $category ){
     echo <<<EOB
                     <li><a href="article.php?action=diffentCategoryArticle&catID={$category['category_id']}">{$category['name']}({$category['count_child_number']})</a></li>
 EOB;
     }
 }
 ?>
                             </ul>
                         </div>
                     </td>
                     <td>
                         <div>
                 <div>
                 <!-- 显示提示信息 -->
                 <?php
                     if( isset( $results['statusMessage'] )){echo  $results['statusMessage'];}
                     if( isset( $results['errorMessage'] )){echo  $results['errorMessage'];}
                 ?>
                 </div>
 <div>
     <div>
         <span>文章(主要用于转载,发布原创博文要通过“随笔”)</span>
     </div>
     <div>    
 <?php 
     if( isset( $results['posts'] )){
     echo <<<EOB
             <table cellspacing="0" cellpadding="0">
                 <tr>
                     <th valign="bottom">
                         标题
                     </th>
                     <th>
                         发布<br />
                         状态
                     </th>
                     <th valign="bottom">
                         评论
                     </th>
                     <th>
                         页面<br />
                         浏览
                     </th>             
                     <th valign="bottom">
                         操作
                     </th>
                     <th valign="bottom">
                         操作
                     </th>
                 </tr>

 EOB;
         foreach( $results['posts'] as $post ){
             $time = date("Y-m-d H:i:s", $post['create_time']);
             if( $post['status'] == "1" ){
                 $post['status']  = "发布";
             }    else {
                 $post['status']  = "<b>未发布</b>";
             }
             echo <<<EOB
             <tr>
                 <td>{$post['title']} ({$time})</td>
                 <td>{$post['status']}</td>
                 <td>{$post['view_count']}</td>
                 <td>{$post['comment_count']}</td>
                 <td><a href="article.php?action=editArticle&postID={$post['post_id']}">编辑</a></td>
                 <td><a href="JavaScript:if(confirm('从数据库中删除这篇文档?')==true){window.location='article.php?action=delete&postID={$post['post_id']}';}">删除</a></td>   
             </tr>
 EOB;
         }
             echo "</table>";               
             if( isset( $pagination) ){$pagination->createLinks( ) ;}
     } else {
         echo "当前无内容!";
     }

 ?>  

     </div>
 </div>

 <span></span>

                         </div>
                     </td>
                 </tr>
             </table>
             <div>
                 <div>
                 </div>
                 <div>         
                  <span><b><?php  echo $_SESSION['username']; ?> </b>&nbsp; <a href="https://www.jb51.net/?action=logout">logout</a></span>
                 </div>
                 <div></div>
             </div>
             <table cellpadding="0" cellspacing="0">
                 <tr>
                     <td colspan="2">
                         <div>
                             &copy; <?php echo date("Y", time( ) ); ?> Arist
                         </div>
                     </td>
                 </tr>
             </table>
     </body>
 </html>

以上只是显示数据,人人都会啊。

我们怎么操作这些数据呢?

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/308829fe83db7cb514ea672552e928f8.html