文章标签设计方法,文章Tags数据库设计方法举例
下面我们以wordpress和sablog为例,说说wordpress和sablog在tag数据库结构设计和程序方面的差别,希望大家能从中学习文章标签设计方法。
sablog的sablog_articles表
`keywords` varchar(120) NOT NULL default ”,
keywords字段: 以 “,” 分隔所有这篇文章使用到的tag
sablog的sablog_tags表
PHP代码 1 2 3 4 5 6 7 8 CREATE TABLE IF NOT EXISTS `sablog_tags` ( `tagid` int(11) unsigned NOT NULL auto_increment, `tag` varchar(100) NOT NULL default '', `usenum` int(11) NOT NULL default '0', `aids` text NOT NULL, PRIMARY KEY (`tagid`), KEY `usenum` (`usenum`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 ;tag字段: 存放tag名称 usenum字段: 存放使用这个tag的文章总数 aids字段: 以 “,” 分隔所有使用这个tag的文章id
显示文章tag的时候,是这么写的
PHP代码 1 2 3 4 5 6 7 8 $tagdb = explode(',', $article['keywords']); $articletags = $tmark = ''; for($i=0; $i<count($tagdb); $i++) { $tagdb[$i] = trim($tagdb[$i]); $articletags .= $tmark.'<a href="./?action=tags&item='.urlencode($tagdb[$i]).'">'.htmlspecialchars($tagdb[$i]).'</a>'; $tmark = ', '; } $article['tags'] = $articletags;取出sablog_articles表keywords字段,explode后生成链接
显示相关文章的程序部份
PHP代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 $tags = $comma = ''; for($i=0; $i<count($tagdb); $i++) { $tags .= $comma."'".addslashes($tagdb[$i])."'"; $comma = ','; } $query = $DB->query("SELECT aids FROM {$db_prefix}tags WHERE tag IN ($tags)"); $relaids = 0; while ($tag = $DB->fetch_array($query)) { $relaids .= ','.$tag['aids']; } $relids = explode(',', $relaids); // 清除重复值的单元并删除当前ID $relids = array_unique($relids); $relids = array_flip($relids); unset($relids[$articleid]); $relids = array_flip($relids); $related_tatol = count($relids); $relids = implode(',',$relids); if ($related_tatol > 1 && $relids != $articleid) { $order = in_array($options['related_order'], array('dateline', 'views', 'comments')) ? $options['related_order'] : 'dateline'; $query = $DB->query("SELECT articleid,title,views,comments FROM {$db_prefix}articles WHERE visible='1' AND articleid IN ($relids) ORDER BY ".$order." DESC LIMIT ".intval($options['related_shownum'])); $titledb=array(); while ($title = $DB->fetch_array($query)) { $title['title'] = trimmed_title($title['title'], $options['related_title_limit']); $titledb[] = $title; } unset($title); $DB->free_result($query); }取出sablog_articles表keywords字段,生成取得aids的查询条件$query = $DB->query(“SELECT aids FROM {$db_prefix}tags WHERE tag IN (‘tag1′,’tag2′,’tag3′)”);
从而得到所有包含tag1,tag2,tag3的文章id, 注意aids字段的内容,上面我已经说明了aids字段: 以 “,” 分隔所有使用这个tag的文章id
wordpress则用到了4张表wp_posts, wp_terms, wp_term_relationships, wp_term_taxonomy wordpress在新版里,数据库结构有了调整,结构和名称与以前的不一样,category和tag合并了,区别在wp_term_taxonomy里taxonomy字段 category则taxonomy字段内容为category tag则taxonomy字段内容为post_tag wp_posts里不存放任何tag数据 wp_terms表用于存放tag和category,结构如下
PHP代码 1 2 3 4 5 6 7 8 CREATE TABLE IF NOT EXISTS `wp_terms` ( `term_id` bigint(20) NOT NULL auto_increment, `name` varchar(55) NOT NULL default '', `slug` varchar(200) NOT NULL default '', `term_group` bigint(10) NOT NULL default '0', PRIMARY KEY (`term_id`), UNIQUE KEY `slug` (`slug`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;`wp_terms`里的 term_id 和 `wp_term_taxonomy` 里的 term_id 关联 `wp_posts`里的 ID 和 wp_term_relationships 里的 object_id 关联 `wp_term_relationships`里的 term_taxonomy_id 和 `wp_terms` 里的 term_id 关联 先说到这吧,具体的去看数据库,熟练的程序员,由数据库结构,就能知道程序是怎么写的了
wordpress的tag数据库结构要清晰条理得多,sablog的tag数据库结构就不太利于维护,但对于2者在显示方面的性能,我还没有做测试。
自由转载,转载请注明: 转载自WEB开发笔记 www.chhua.com