刚才给一个插件的文章加上TAG,反复思考认为此方法可以灵活运用到其它的插件上,故写下此文,以备查阅,供大家参考。
$tags = empty($result['tag'])?array():unserialize($result['tag']); $info['tag'] = implode(' ', $tags);
在动手之前,我们有必要先搞清楚TAG数据库部分:
-- -- 表的结构 `uchome_resource_tag` -- CREATE TABLE IF NOT EXISTS `uchome_resource_tag` ( `tagid` mediumint(8) unsigned NOT NULL auto_increment, `tagname` char(30) NOT NULL default '', `uid` mediumint(8) unsigned NOT NULL default '0', `dateline` int(10) unsigned NOT NULL default '0', `blognum` smallint(6) unsigned NOT NULL default '0', `close` tinyint(1) NOT NULL default '0', PRIMARY KEY (`tagid`), KEY `tagname` (`tagname`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ; -- -- 表的结构 `uchome_resource_tagblog` -- CREATE TABLE IF NOT EXISTS `uchome_resource_tagblog` ( `tagid` mediumint(8) unsigned NOT NULL default '0', `blogid` mediumint(8) unsigned NOT NULL default '0', PRIMARY KEY (`tagid`,`blogid`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; -- -- 表的结构 `uchome_resource_detail` 注意这是文章信息一起的 -- ALTER TABLE `uchome_resource_detail` ADD `tag` VARCHAR( 255 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL AFTER `posttime`;
接下来,我们看数据提交部分:
//处理tag function tag_batch($blogid, $tags) { global $_SGLOBAL; $tagarr = array(); $tagnames = empty($tags)?array():array_unique(explode(' ', $tags)); if(empty($tagnames)) return $tagarr; $vtags = array(); $query = $_SGLOBAL['db']->query("SELECT tagid, tagname, close FROM ".tname('resource_tag')." WHERE tagname IN (".simplode($tagnames).")"); while ($value = $_SGLOBAL['db']->fetch_array($query)) { $value['tagname'] = addslashes($value['tagname']); $vkey = md5($value['tagname']); $vtags[$vkey] = $value; } $updatetagids = array(); foreach ($tagnames as $tagname) { if(!preg_match('/^([\x7f-\xff_-]|\w){3,20}$/', $tagname)) continue; $vkey = md5($tagname); if(empty($vtags[$vkey])) { $setarr = array( 'tagname' => $tagname, 'uid' => $_SGLOBAL['supe_uid'], 'dateline' => $_SGLOBAL['timestamp'], 'blognum' => 1 ); $tagid = inserttable('resource_tag', $setarr, 1); $tagarr[$tagid] = $tagname; } else { if(empty($vtags[$vkey]['close'])) { $tagid = $vtags[$vkey]['tagid']; $updatetagids[] = $tagid; $tagarr[$tagid] = $tagname; } } } if($updatetagids) $_SGLOBAL['db']->query("UPDATE ".tname('resource_tag')." SET blognum=blognum+1 WHERE tagid IN (".simplode($updatetagids).")"); $tagids = array_keys($tagarr); $inserts = array(); foreach ($tagids as $tagid) { $inserts[] = "('$tagid','$blogid')"; } if($inserts) $_SGLOBAL['db']->query("REPLACE INTO ".tname('resource_tagblog')." (tagid,blogid) VALUES ".implode(',', $inserts)); return $tagarr; }
//TAG $POST['tag'] = shtmlspecialchars(trim($_POST['tag'])); $POST['tag'] = getstr($POST['tag'], 500, 1, 1, 1); //语词屏蔽 $blogid=$_POST['infoid']; $sql = 'SELECT * FROM '.$_SC['tablepre'].'resource_detail where uid= '. $uid . ' and ' . $_SC['tablepre'].'resource_detail.id='.intval($_POST['infoid']); $query = $_SGLOBAL['db']->query($sql); $olds = mysql_fetch_array($query,1); $oldtagstr = addslashes(empty($olds['tag'])?'':implode(' ', unserialize($olds['tag']))); $tagarr = array(); if($POST['tag'] != $oldtagstr) { if(!empty($olds['tag'])) { //先把以前的给清理掉 $oldtags = array(); $query = $_SGLOBAL['db']->query("SELECT tagid, blogid FROM ".tname('resource_tagblog')." WHERE blogid='$blogid'"); while ($value = $_SGLOBAL['db']->fetch_array($query)) { $oldtags[] = $value['tagid']; } if($oldtags) { $_SGLOBAL['db']->query("UPDATE ".tname('resource_tag')." SET blognum=blognum-1 WHERE tagid IN (".simplode($oldtags).")"); $_SGLOBAL['db']->query("DELETE FROM ".tname('resource_tagblog')." WHERE blogid='$blogid'"); } } $tagarr = tag_batch($blogid, $_POST['tag']); //更新附表中的tag $POST['tag'] = empty($tagarr)?'':addslashes(serialize($tagarr)); } $tag=$POST['tag'];
其次,看TAG在文章内显示部分:
标签: $tagname
最后,TAG获取与列表显示:
//获取TAG $query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('resource_tag')." WHERE ".($id?"tagid='$id'":"tagname='$name'")." LIMIT 1"); $tag = $_SGLOBAL['db']->fetch_array($query); if(empty($tag)) { showmessage('tag_does_not_exist'); } elseif ($tag['close']) { showmessage('tag_locked'); } //获取tag关联 $prinum = 0; $query = $_SGLOBAL['db']->query("SELECT blog.* FROM ".tname('resource_tagblog')." tb , ".tname('resource_detail')." blog WHERE tb.tagid='$tag[tagid]' AND blog.id=tb.blogid LIMIT $start,$perpage"); while ($value = $_SGLOBAL['db']->fetch_array($query)) { if(empty($value['friend'])) { realname_set($value['uid'], $value['username']); $list[] = $value; } else { $prinum++; } $count++; }
相关文章:
转载请注明出处:https://www.onexin.net/discuz/ucenter_home/uch-plug-ins-to-your-article-with-tag-general-method/