【openblog】第6.9讲 实现文章的按月份分类
本帖最后由 xiaoniao 于 2009-11-25 19:35 编辑/*
* phpall.cn技术交流qq群:75345798
*/
http://www.phpall.cn/images/guidang.jpg
一般博客的有个按照月份来分类文章的功能,今天主要讲下openblog是如何来实现的。
先说下思路,首先数据库设计的时候需要把日期字段里面的日期保存成这样的格式:2009-11-17.,然后查询数据库的时候使用like。Like的条件是2009-11这个年月即可。
Openblog实现的主要函数在application/modules/blog/model/blog_model.php文件中的
最重要的一句是第16行的like语句。
[*][*] //根据年月来获取文章,该函数返回文章的结果集[*]public function get_posts_by_date($year, $month)[*] {[*] //参数中的$year,$month可以从url中获得,例如http://127.0.0.1/2009-11[*] $date = $year . '-' . $month;[*] //得到当前时间[*] $current_date = date('Y-m-d');[*] $this->db->select('posts.id, posts.author, posts.date_posted, posts.title, posts.url_title, posts.excerpt, posts.content, posts.allow_comments, posts.sticky, posts.status, posts.author, users.display_name');[*] $this->db->from($this->_table['posts'] . ' posts');[*] //它使用join来得到文章的user[*] $this->db->join($this->_table['users'] . ' users', 'posts.author = users.id');[*] $this->db->where('posts.status', 'published');[*] //这句是设置限制条件,文章发布日期要小于当前日期,我想这个限制条件一般情//况下都不需要使用[*] $this->db->where('posts.date_posted <=', $current_date);[*]//获取按时间分类最重要的一句就是这个like语句了[*] $this->db->like('posts.date_posted', $date);[*] $this->db->order_by('posts.sticky', 'DESC');[*] $this->db->order_by('posts.id', 'DESC');[*] //后面就是取结果集的,这里不再赘述[*] $query = $this->db->get();[*][*] if ($query->num_rows() > 0)[*] {[*] $result = $query->result_array();[*][*][*][*] foreach (array_keys($result) as $key)[*] {[*] $result[$key]['categories'] = $this->categories->get_categories_by_ids($this->get_post_categories($result[$key]['id']));[*] $result[$key]['comment_count'] = $this->db->where('post_id', $result[$key]['id'])->from($this->_table['comments'])->count_all_results();[*] }[*][*] return $result;[*] }[*] }
第一讲:hmvc的一个支持模块
http://www.phpall.cn/forum/read.php?tid=263
第二讲:Openblog的目录结构和hmvc调用方法
http://www.phpall.cn/forum/read.php?tid=274
【openblog】第三讲 user模块 (强烈建议ci入门或者初级的朋友看)
http://www.phpall.cn/forum/read.php?tid=281
第四讲 admin模块中dashboard部分
http://www.phpall.cn/forum/read.php?tid=289
第五讲 admin模块 settings部分
http://www.phpall.cn/forum/read.php?tid=294
第六讲 【openblog】第六讲Admin模块的sidebar部分
http://www.phpall.cn/forum/read.php?tid=308
以后我们将会不定期的对openblog的源码进行一些分析。
openblog官方网站:http://www.open-blog.info/
博客演示:http://www.open-blog.info/demo/
源码下载:http://www.open-blog.info/downloads
第七讲将讲解 tag cloud 标签的实现
页:
[1]