IT技术博客大学习 共学习 共进步
全部 移动开发 后端 数据库 AI 算法 安全 DevOps 前端 设计 开发者

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

标点符 2010-05-04 10:16:19 累计浏览 5,463 次
本机暂存

第一步:数据库的创建

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. 等了十年的 Go 链式管道,终于来了:seq 让你像写 Scala 一样写 Go (2026-06-25 18:38:18)
  2. Go 实验特性详解 (2026-06-21 10:05:27)
  3. amd64 微架构级别对 Go 程序性能提升多少? (2026-06-21 09:38:49)

查看更多 后端 文章 →

建议继续学习

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