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

标点符 2010-05-04 10:16:19 累计浏览 5,710 次

第一步:数据库的创建

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. NGINX 1.31.7: Native GeoIP2 support, HTTP/2 priorities, and QUIC improvements (2026-11-13 23:40:02)
  2. 说说广场 API 0.31.0 版的调用代码说明 (2026-11-13 19:10:02)
  3. NGINX Routing on HTTP Request Body (2026-10-27 14:10:42)

查看更多 后端 文章 →

建议继续学习

  1. 用Hyer来进行网站的抓取 (累计阅读 158,836)
  2. 使用gettext来支持PHP的多语言 (累计阅读 39,702)
  3. MySQL数据库在实际应用一些方面的介绍 (累计阅读 36,809)
  4. WordPress插件开发 -- 在插件使用数据库存储数据 (累计阅读 29,568)
  5. Mysql监控指南 (累计阅读 21,830)
  6. Paypal接口详细代码(PHP版,非API接口) (累计阅读 19,821)
  7. 由浅入深探究mysql索引结构原理、性能分析与优化 (累计阅读 17,051)
  8. 我的PHP,Python和Ruby之路 (累计阅读 13,719)
  9. 在Apache2.2.XX下安装Mod-myvhost模块 (累计阅读 13,481)
  10. include(“./file.php”)和include(“file.php”)区别 (累计阅读 13,273)