技术头条 - 一个快速在微博传播文章的方式     搜索本站
您现在的位置首页 --> PHP --> 使用PHP创建一个面向对象的博客

使用PHP创建一个面向对象的博客

浏览:3939次  出处信息

第一步:数据库的创建

1.使用phpmyadmian创建一个数据库,名字可以自己选,因为是博客有可能需要输入中文,所以MySQL连接校对选择:uft8_unicode_ci(或utf8_general_ci)

2.创建数据表

需要建立的数据表如图:

以创建blog_posts为例:

需要注意的是,blog_post_tags这张表无需设置主键。

第二步:使用PHP创建对象

创建blogpost.php文件,文件内容为:

以下是代码片段:
<?php
class BlogPost
{
 public $id;
 public $title;
 public $post;
 public $author;
 public $tags;
 public $datePosted;
 
 //构造函数
 function __construct($inId=NULL,$inTitle=NULL,$inPost=NULL,$inPostFull=NULL,$inAuthorId=NULL,$inDatePosted=NULL)
 {
  if (!empty($inId))
  {
   $this->id = $inId;
  }
  if (!empty($inTitle))
  {
   $this->title = $inTitle;
  }
  if (!empty($inPost))
  {
   $this->post =$inPost;
  }
  
  if (!empty($inDatePosted))
  {
   //将2005-05-03格式化为2005/05/03
   $splitDate =explode("-",$inDatePosted);
   $this->datePosted = $splitDate[0]."/".$splitDate[1]."/".$splitDate[2];
  }
  
  if (!empty($inAuthorId))
  {
   //获取全名
   $query = mysql_query("SELECT first_name, last_name FROM people WHERE id = ".$inAuthorId);
   $row = mysql_fetch_assoc($query);
   $this->author = $row["first_name"] ." " . $row["last_name"];
  }
  
  $postTags = "No Tags";
  if (!empty($inId))
  {
   //获取文章标签
   $query = mysql_query("SELECT tags.* From blog_post_tags LEFT JOIN(tags) ON (blog_post_tags.tag_id =tags.id) WHERE blog_post_tags.blog_post_id= ".$inId);
   $tagArray = array();
   $tagIDArray = array();
   while ($row = mysql_fetch_assoc($query))
   {
    array_push($tagArray,$row["name"]);
    array_push($tagIDArray,$row["id"]);
   }
   if (sizeof($tagArray)>0)
   {
    foreach ($tagArray as $tag)
    {
     if ($postTags == "No Tags")
     {
      $postTags = $tag;
     }
     else
     {
      $postTags = $postTags.",".$tag;
     }
    }
   }
  }
  $this->tags = $postTags;
 }
}
?>

第三步:从MySQL获取数据,并显示内容

1.创建includes.php

以下是代码片段:
<?php
include ’blogpost.php’;
$connection = mysql_connect("localhost","root","") or die("<p class=’error’>Sorry, we ware unable to connect to the datebase server.</p>");
$query = mysql_query("SET NAMES ’utf8’"); //如果需要显示中文,则必须要在数据库连接后面加上此句
 
$database = "myblog";
mysql_select_db($database,$connection) or die("<p class=’error’>Sorry, We were unable to connect the datebase.</p>");
 
function GetBlogPosts($inId=NULL,$inTagId=NULL)
{
 if (!empty($inId))
 {
  $query = mysql_query("SELECT * FROM blog_posts WHERE id=".$inId."ORDER BY id DESC");
 }
 elseif (!empty($inTagId))
 {
  $query = mysql_query("SELECT blog_posts.* FROM blog_post_tags LEFT JOIN (blog_posts) ON (blog_post_tags.postID = blog_posts.id) WHERE blog_post_tags.tagID.".$tagID."ORDER BY blog_posts.id DESC"); 
 }
 else
 {
  $query = mysql_query("SELECT * FROM blog_posts ORDER BY id DESC"); 
 }
 
 $postArray = array();
 while ($row = mysql_fetch_assoc($query))
 {
  $myPost = new BlogPost($row["id"],$row["title"],$row["post"],$row["postfull"],$row["author_id"],$row["date_posted"]);
  array_push($postArray,$myPost);
 }
 return $postArray;
}
?>

2.创建实例index.php

以下是代码片段:
<div id="main">
 <h1>My Simple Blog</h1>
 <div id="blogPosts">
 
 <?php 
 include ’includes.php’;
 
 $blogPosts = GetBlogPosts();
 
 foreach ($blogPosts as $post)
 {
  echo "<div class=’post’>";
  echo "<h2>".$post->title."</h2>";
  echo "<p>".$post->post."</p>";
  echo "<span class=’footer’>Posted By: " . $post->author . " Posted On: " . $post->datePosted . " Tags: " . $post->tags . "</span>";
  echo "</div>";
 }
 ?>
 
 </div>
</div>

原文链接:http://net.tutsplus.com/news/how-to-create-an-object-oriented-blog-using-php/

建议继续学习:

  1. 一个典型支付系统的设计与实现    (阅读:7868)
  2. 面向对象的Shell脚本    (阅读:5028)
  3. 用星际争霸讲解面向对象的概念    (阅读:4004)
  4. 关于架构的一句话,还有一个实例    (阅读:3474)
  5. 个人订阅的10佳博客与相关介绍    (阅读:3333)
  6. PHP面向对象编程的三大特性    (阅读:3129)
  7. 如何保证一个程序在单台服务器上只有唯一实例(linux)    (阅读:3105)
  8. 面向对象设计模式的核心法则    (阅读:2995)
  9. 博客系统的结构简述    (阅读:2944)
  10. PHP内核介绍及扩展开发指南―类和对象    (阅读:2758)
QQ技术交流群:445447336,欢迎加入!
扫一扫订阅我的微信号:IT技术博客大学习
© 2009 - 2024 by blogread.cn 微博:@IT技术博客大学习

京ICP备15002552号-1