我官方网站原文:http://www.panshy.com/article/Sort_anothar/another/2014-04-14/2485.php
为什么要使用猜你喜欢功能,原因是:现在各大网站都有猜你喜欢功能,而且使用上猜你喜欢功能后还能给网站带来不少的流量,所以我就想为自己的网站加上猜你喜欢功能。
帝国CMS是没有现成的猜你喜欢功能的,但是有相关链接这个功能,然而帝国的相关链接这个功能不能跨表进行相关链接,更不能将标题进行中文分词,造成的相关链接并不是很好用。
根据观察各大网站的猜你喜欢功能,他们大部份也是使用相关链接来做的,为了能更好的体现出猜你喜欢功能,我的做法是使用手动添加和自动方式相结合的方式进行
手动添加就是新增一个字段,添加上这个文章相关的其它文章链接,这样做有什么好处呢,因为自动的有时不准,而且不能使用站外链接我的网站就有三个,所以加上这个功能是最合适的,三个网站可以相互增加外链。
同时带来流量。并且手工添加的认为是最靠前的,权重最高。自动获取的不一定靠前这就是为什么这个字段的原因。自动获取是改造了帝国的相关链接功能,但实现了跨多个模型数据表,同时引用了中文分词,将标题进行分词后再查找。
下面跟着我来实现帝国CMS猜你喜欢功能吧:
首先添加手动功能的猜你喜欢
首先增加一个字段,将你认为要实现此功能的数据表加上这个字段,我的网站是两个数据表
www_panshy_com_ecms_download
www_panshy_com_ecms_pansharticle
点击 系统 -> 数据表与系统模型 -> 管理数据表
右侧找到你想要添加字段的表,点管理字段,出来窗体后,点添加字段,按下图的方式增加,记得是副表就行
加完字段后,点击管理系统模型,按下图设置,记得最后钩选上 自动生成表单模板 最后确定即可
成功后增加信息时,将出现如下图所示的输入框
以上就是手工实现的猜你喜欢必须的工作,现在开始说自动的,也就是跨表实现相关链接
找到 \e\class 目录下的userfun.php文件,加入以下代码
复制代码
-
- function utf8_strlen($string = null)
- {
-
- preg_match_all("/./us", $string, $match);
-
- return count($match[0]);
- }
-
-
- function GetOtherLink_in_outarr_list(&$outarr_list, $strurl)
- {
- foreach ($outarr_list as $val)
- {
- if( stristr($val[1], $strurl))
- {
- return true;
- }
-
-
-
-
-
- }
-
- return false;
- }
复制代码
- function user_combo_otherlike_html(&$outarr_list, $strrows , $beginhtml, $endhtml)
- {
- $str = "";
- $str_list = "";
-
- $arr_count = count($outarr_list);
-
- if( $arr_count < 1 )
- {
- $str_list = $beginhtml."暂无内容".$endhtml;
- return $str_list;
- }
-
- $rows_list = explode(",", $strrows);
- $rows_count = count($rows_list);
- if( $rows_count != 2)
- {
- $str_list = $beginhtml."暂无内容".$endhtml;
- return $str_list;
- }
-
- $index = $rows_list[0];
- $num = $rows_list[1];
-
- if( $index < 0 || $index >= $arr_count)
- {
- $str_list = $beginhtml."暂无内容".$endhtml;
- return $str_list;
- }
-
-
- for($i=$index; $i<$arr_count; $i++)
- {
- $arr_temp = $outarr_list[$i];
- if( count($arr_temp) == 2 )
- {
- $str_link = '<a target=_blank href="';
- $str_link .= $arr_temp[1];
- $str_link .= '" title="';
- $str_link .= $arr_temp[0];
- $str_link .= '">';
- $str_link .= esub($arr_temp[0], 40, '...');
- $str_link .= '</a>';
-
- $str = $beginhtml.$str_link.$endhtml;
- $str_list .= $str;
- --$num;
- if( $num <= 0 )
- {
- break;
- }
- }
- }
-
- if( strlen($str_list) < 1 )
- {
- $str_list = $beginhtml."暂无内容".$endhtml;
- }
-
- return $str_list;
- }
-
-
- include ( "sphinxapi.php" );
- function user_GetOtherLinkInfo(&$outarr_list, $num, $tbname, $str_not_tagcid)
- {
- global $navinfor,$empire,$dbtbpre,$class_r ;
-
- $outarr_list = array();
- $arr_temp = array();
-
- $strcai_like = $navinfor['cai_like'];
-
- $cai_like_list = explode('[####]', $strcai_like);
- $cai_like_count = count($cai_like_list);
-
- if( $cai_like_count > 0 )
- {
- foreach ($cai_like_list as $val)
- {
- $arr_temp = explode('[##]', $val);
- if( count($arr_temp) == 2 )
- {
- if( !GetOtherLink_in_outarr_list($outarr_list, $arr_temp[1]))
- {
- array_push($outarr_list, $arr_temp);
- --$num;
- }
- }
- }
- }
-
-
- $num = read_tags_key_OtherLink($tbname, $outarr_list, $num, $str_not_tagcid);
-
- }
-
-
- function read_Sphinx_cai_like($tbname, &$outarr_list, $num)
- {
- global $navinfor,$empire,$dbtbpre,$class_r ;
-
- if( strlen($tbname) < 1 || $num < 1) return $num;
-
- $arr_tbname = explode(',', $tbname);
- $tbname_count = count($arr_tbname);
-
-
-
- $lvalue = (int)($num / $tbname_count);
- $lmod = (int)($num % $tbname_count);
-
- $arr_type_list = array();
- $arr_type = array();
-
-
- $curdata_id = $navinfor['id'];
- $cur_tbname = $class_r[$navinfor['classid']]['tbname'];
-
- $next_num = 0;
-
- $sph = new SphinxClient();
- $sph->SetServer('localhost', 9312);
- $keywords = $sph->BuildKeywords( "aaaa", "www_panshy_com", false );
- return ;
-
- foreach ($arr_tbname as $val)
- {
- $arr_type["dbname"] = $val;
- $arr_type["num"] = $lvalue;
- if( $lmod > 0 )
- {
- ++$arr_type["num"];
- --$lmod;
- }
- $getcount = $arr_type["num"];
- $getcount += $next_num;
- $next_num = 0;
- if( $getcount < 1 ) { continue ; }
- $index_name = "";
- if( strcasecmp($arr_type["dbname"], "pansharticle") == 0 )
- {
- $index_name = "www_panshy_com";
- }
- else if( strcasecmp($arr_type["dbname"], "download") == 0 )
- {
- $index_name = "www_panshy_com_down";
- }
- else
- {
- $next_num = $getcount;
- continue ;
- }
-
-
- $keywords = $sph->BuildKeywords( $navinfor['title'], $index_name, false );
-
- $str_not_id = "";
- if( strcasecmp($arr_type["dbname"], $cur_tbname) == 0 )
- {
- $str_not_id = $curdata_id;
- }
-
-
- $keygetcount = $getcount;
- if( $keygetcount > 0 )
- {
- $str_key_sql_keyboard = "";
- $str_key_sql_title = "";
- $str_key_sql = "";
-
- foreach ($keywords as $keyval)
- {
- if( utf8_strlen($keyval['normalized']) > 1 )
- {
- $str_key_sql_keyboard .= " or keyboard like '%";
- $str_key_sql_keyboard .= $keyval['normalized'];
- $str_key_sql_keyboard .= "%'";
-
- $str_key_sql_title .= " or title like '%";
- $str_key_sql_title .= $keyval['normalized'];
- $str_key_sql_title .= "%'";
-
- }
- }
- $str_key_sql_keyboard = substr($str_key_sql_keyboard, 4);
- $str_key_sql_title = substr($str_key_sql_title, 4);
- $str_key_sql = $str_key_sql_keyboard." or ".$str_key_sql_title;
-
- if( strlen($str_key_sql) > 0 )
- {
- $str_sql = "select title,titleurl from {$dbtbpre}ecms_";
- $str_sql .= $arr_type['dbname'];
- $str_sql .= " where (";
- $str_sql .= $str_key_sql;
- $str_sql .= ")";
- if( strlen($str_not_id) > 0 )
- {
- $str_sql .= " and id not in(";
- $str_sql .= $str_not_id;
- $str_sql .= ")";
- }
-
- $str_sql .= " order by newstime desc limit ";
- $limit_int = $keygetcount + count($outarr_list);
- $str_sql .= $limit_int;
-
- $sql = $empire->query($str_sql);
- $arr_item = array();
-
- while($r=$empire->fetch($sql))
- {
- if( $keygetcount < 1) break;
-
- $arr_item[0] =$r['title'];
- $arr_item[1] = $r['titleurl'];
-
- if( !GetOtherLink_in_outarr_list($outarr_list, $arr_item[1]))
- {
- array_push($outarr_list, $arr_item);
- --$keygetcount;
- --$num;
- }
- }
-
- }
- }
-
- $next_num = $keygetcount;
-
- }
-
- return $num;
- }
-
-
- function read_tags_key_OtherLink($tbname, &$outarr_list, $num, $str_not_tagcid)
- {
- global $navinfor,$empire,$dbtbpre,$class_r ;
-
- if( strlen($tbname) < 1 || $num < 1) return $num;
-
- $arr_tbname = explode(',', $tbname);
- $tbname_count = count($arr_tbname);
-
-
- $lvalue = (int)($num / $tbname_count);
- $lmod = (int)($num % $tbname_count);
-
- $arr_type_list = array();
- $arr_type = array();
-
- $tag_str = $navinfor['infotags'];
- $curdata_id = $navinfor['id'];
- $cur_tbname = $class_r[$navinfor['classid']]['tbname'];
-
- $sql_tags_str = str_replace(",", "','", $tag_str);
-
-
- $str_key_sql_keyboard = "";
- $str_key_sql_title = "";
- $str_key_sql = "";
- $arr_key = array();
- if( strlen($navinfor['keyboard']) > 0 )
- {
- $arr_key = explode(',', $navinfor['keyboard']);
-
- if( count($arr_key) > 0 )
- {
- foreach ($arr_key as $val)
- {
- $str_key_sql_keyboard .= " or keyboard like '%";
- $str_key_sql_keyboard .= $val;
- $str_key_sql_keyboard .= "%'";
-
- $str_key_sql_title .= " or title like '%";
- $str_key_sql_title .= $val;
- $str_key_sql_title .= "%'";
- }
-
- $str_key_sql_keyboard = substr($str_key_sql_keyboard, 4);
- $str_key_sql_title = substr($str_key_sql_title, 4);
- $str_key_sql = $str_key_sql_keyboard." or ".$str_key_sql_title;
- }
- }
-
- $next_num = 0;
-
-
- foreach ($arr_tbname as $val)
- {
- $arr_type["dbname"] = $val;
- $arr_type["num"] = $lvalue;
- if( $lmod > 0 )
- {
- ++$arr_type["num"];
- --$lmod;
- }
- $getcount = $arr_type["num"];
- $getcount += $next_num;
- $next_num = 0;
- if( $getcount < 1 ) { continue ; }
-
-
-
-
- $taggetcount = (int)($getcount/2);
-
- if(($getcount%2) > 0 ) { ++$taggetcount;}
-
- if( $taggetcount < 1 ) { continue ; }
-
- $keygetcount = $getcount - $taggetcount;
-
-
-
-
-
- $r=$empire->fetch1("select * from {$dbtbpre}enewstable where tbname='" .$arr_type["dbname"]."'" );
- $mid = $r['mid'];
- $str_not_id = "";
- if( strcasecmp($arr_type["dbname"], $cur_tbname) == 0 )
- {
- $str_not_id = $curdata_id;
- }
- $arr_item = array();
- if(!emptyempty($mid))
- {
-
- if( strlen($sql_tags_str) > 0 )
- {
- $str_sql = "select tagid from {$dbtbpre}enewstags where tagname in('".$sql_tags_str. "')";
- if( strlen($str_not_tagcid) > 0)
- {
- $str_sql .= " and cid not in(";
- $str_sql .= $str_not_tagcid;
- $str_sql .= ") ";
- }
- $sql = $empire->query($str_sql);
-
- $str_tag_id = "";
- while($r=$empire->fetch($sql))
- {
- $temp_tagid = $r['tagid'];
- if(!emptyempty($temp_tagid))
- {
- if( strlen($str_tag_id) > 0 )
- {
- $str_tag_id .= ",";
- $str_tag_id .= $temp_tagid;
- }
- else
- {
- $str_tag_id = $temp_tagid;
- }
- }
- }
-
- $str_id_list = "";
- if( strlen($str_tag_id) > 0 )
- {
- $str_sql = "select {$dbtbpre}enewstagsdata.id from {$dbtbpre}enewstagsdata where ";
- $str_sql .= "{$dbtbpre}enewstagsdata.mid=";
- $str_sql .= $mid;
- $str_sql .= " and tagid in(";
- $str_sql .= $str_tag_id;
- $str_sql .= ") order by newstime DESC limit ";
- $str_sql .= $taggetcount;
-
- $sql = $empire->query($str_sql);
- while($r=$empire->fetch($sql))
- {
- $temp_id = $r['id'];
- if(!emptyempty($temp_id))
- {
- if( strlen($str_id_list) > 0 )
- {
- $str_id_list .= ",";
- $str_id_list .= $temp_id;
- }
- else
- {
- $str_id_list = $temp_id;
- }
- }
- }
- }
-
- if( strlen($str_id_list) > 0 )
- {
- $str_sql = "select title,titleurl from {$dbtbpre}ecms_";
- $str_sql .= $arr_type['dbname'];
- $str_sql .= " where id in (";
- $str_sql .= $str_id_list;
- $str_sql .= ")";
-
- if( strlen($str_not_id) > 0 )
- {
- $str_sql .= " and id != ";
- $str_sql .= $str_not_id;
-
- $str_not_id .= ",";
- $str_not_id .= $str_id_list;
- }
- else
- {
- $str_not_id = $str_id_list;
- }
-
- $str_sql .= " order by newstime DESC";
-
- $sql = $empire->query($str_sql);
-
- while($r=$empire->fetch($sql))
- {
- $arr_item[0] =$r['title'];
- $arr_item[1] = $r['titleurl'];
-
- if( !GetOtherLink_in_outarr_list($outarr_list, $arr_item[1]))
- {
- array_push($outarr_list, $arr_item);
- --$taggetcount;
- --$num;
-
-
- }
- }
-
- }
- }
- }
-
-
-
- $keygetcount += $taggetcount;
- if( $keygetcount > 0 )
- {
- if( strlen($str_key_sql) > 0 )
- {
- $str_sql = "select title,titleurl from {$dbtbpre}ecms_";
- $str_sql .= $arr_type['dbname'];
- $str_sql .= " where (";
- $str_sql .= $str_key_sql;
- $str_sql .= ")";
- if( strlen($str_not_id) > 0 )
- {
- $str_sql .= " and id not in(";
- $str_sql .= $str_not_id;
- $str_sql .= ")";
- }
-
- $str_sql .= " order by newstime desc limit ";
- $str_sql .= $keygetcount;
-
-
- $sql = $empire->query($str_sql);
- $arr_item = array();
-
- while($r=$empire->fetch($sql))
- {
-
- $arr_item[0] =$r['title'];
- $arr_item[1] = $r['titleurl'];
-
- if( !GetOtherLink_in_outarr_list($outarr_list, $arr_item[1]))
- {
- array_push($outarr_list, $arr_item);
- --$keygetcount;
- --$num;
-
- }
- }
-
- }
- }
-
- $next_num = $keygetcount;
-
-
- }
-
- return $num;
- }
猜你喜欢的原理
到此准备工作完成,接下来说说我实现的猜你喜欢的原理
现在假设,你需要在你的内容页面获取14条猜你喜欢记录,其中关联到2个表模型,这里叫A,B,C三个表,当僧多粥少时,有些表是不能得到记录的。
现在有一个内容叫 ZZ 的,首先获取手工添加的,从上面添加的那个字段中获取,现在假设得到3条记录,保存起来
然后程序平均分配余下的记录数11条,A=3,B=3 ,C=3 发现还多出2条记录,那么A=4,B=4,C=3这是分配方式
现在开始查找记录,首先取A表的,上面最终分配是4条,这4条记录,要从tags和keys中获取,所以4除2 = 2 tags=2 keys=2 (注 C表=3 这种情况是 tags=2 keys=1)
程序是先获取tags相关记录,再获取keys记录的,假设在A表中 tags 只取出一条记录,余 1条记录,程序会将这一条传到keys中,这时keys = 3 然后,再次获取keys记录,最多3条
如果keys也只获取到1条,余2条,这2条会交给下一个表,B,也就是说 B = 4 + 2 = 6条记录,依此类推,处理完A,B,C三个表
程序在每次得到的记录时都会检查是不是有重的,没有的话才会将记录数减少,并将记录保存起来。
如果发现还有没处理完的记录数,就可以调用中文分词处理程序获取相关记录。即 read_Sphinx_cai_like 函数,当然也可以把这个顺序调过来,修改相关函数就可以
注:由于使用 Sphinx 中文分词后,帝国CMS生成内容特别慢,所以这个功能暂时保留,可以自行删除上面的相关函数。大家可以关注我的网站,随时会进行这个功以的补充。
调用方法
内容页代码添加,在body开头处添加
复制代码
- <?
- $arr_outarr_list;
- user_GetOtherLinkInfo($arr_outarr_list, 20, "pansharticle,download", "1,2,3,4,5,6,7,8,9,10");
- ?>
其中 20 为要获取的记录数,"pansharticle,download 是要获取的表名,用逗号隔开, "1,2,3,4,5,6,7,8,9,10“ 表示不需要关联的tags类别ID
上图中,管理数据表中显示粗体黑色字的就是表名,管理TAG分类中的ID就是上面的ID
好啦,在你要显示的地方加入
<?php echo user_combo_otherlike_html($arr_outarr_list, "0,5", "<li>", "</li>")?>
记录是从0开始记算的,上面的20条就是0 - 19 所以 0, 5表示,从0条记录开始,取5条记录,其中,取得的每条记录使用 <li> 与 </li>包围起来
好了,到此就剩下手工添加记录的方式呢
有关手工添加猜你喜欢的方法和相关文件请关注
http://bbs.panshy.com/thread-395-1-1.html
到此帝国CMS猜你喜欢就算完成了,同时也实现了,跨表的相关链接,
虽不算完美,但也算是用最简单的办法,达到效果吧,希望有高手来完善,谢谢大家支持。