[原创]帝国CMS实现猜你喜欢功能 多表、跨表模型的相关链接
原文地址: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 点击 系统 -> 数据表与系统模型 -> 管理数据表 帝国CMS猜你喜欢功能实现图1.jpg 右侧找到你想要添加字段的表,点管理字段,出来窗体后,点添加字段,按下图的方式增加,记得是副表就行
帝国CMS猜你喜欢功能实现图2.jpg
加完字段后,点击管理系统模型,按下图设置,记得最后钩选上 自动生成表单模板 最后确定即可
帝国CMS猜你喜欢功能实现图3.jpg
成功后增加信息时,将出现如下图所示的输入框
帝国CMS猜你喜欢功能实现图4.jpg 以上就是手工实现的猜你喜欢必须的工作,现在开始说自动的,也就是跨表实现相关链接
找到 \e\class 目录下的userfun.php文件,加入以下代码 复制代码
// 计算中文字符串长度 function utf8_strlen($string = null) { // 将字符串分解为单元 preg_match_all("/./us", $string, $match); // 返回单元个数 return count($match[0]); } //同样的URL在数组中返回true function GetOtherLink_in_outarr_list(&$outarr_list, $strurl) { foreach ($outarr_list as $val) { if( stristr($val[1], $strurl)) { return true; } // if (in_array($strurl, $val)) //{ // 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; } } } } //再处理tags与关键字相同的记录 $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'];//当前记录ID $cur_tbname = $class_r[$navinfor['classid']]['tbname'];//当前文章表名 $next_num = 0; $sph = new SphinxClient(); //实例化 sphinx 对象 $sph->SetServer('localhost', 9312); //连接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 = ""; //不需要查出来的ID if( strcasecmp($arr_type["dbname"], $cur_tbname) == 0 ) { $str_not_id = $curdata_id; } //为tags分配的记录数如果有没处理完的传给key $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);//去掉字符串前的 or 刚好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;//tags充许的记录数减少 --$num;//c 允许获取的总记录数减少 } } } } $next_num = $keygetcount; } return $num; } //返回未处理的$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'];//当前记录ID $cur_tbname = $class_r[$navinfor['classid']]['tbname'];//当前文章表名 $sql_tags_str = str_replace(",", "','", $tag_str); //echo "num值:".$num; $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);//去掉字符串前的 or 刚好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;//保存分配时没有获取的数量, //比如A,B表各分配了2个,但A表没得到任何数据将2传给B 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 ; } //先读取tags相关链的记录,再读键值相关的记录 //为当前表的不同类型分配允许获取的记录数,比如A表要获取tag,key这样A表的总记录数就得平分给tag,key $taggetcount = (int)($getcount/2); if(($getcount%2) > 0 ) { ++$taggetcount;} if( $taggetcount < 1 ) { continue ; } //第一种获取都为0,接下来肯定不可能分配 $keygetcount = $getcount - $taggetcount; //平均分配给tags,key的数量完成 //echo "总数:". $getcount. " t数". $taggetcount." k数".$keygetcount." "; //获取数据表的ID $r=$empire->fetch1("select * from {$dbtbpre}enewstable where tbname='" .$arr_type["dbname"]."'" ); $mid = $r['mid']; $str_not_id = ""; //不需要查出来的ID if( strcasecmp($arr_type["dbname"], $cur_tbname) == 0 ) { $str_not_id = $curdata_id; } $arr_item = array(); if(!emptyempty($mid)) { //获取tagid 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); //$int_i = 0; 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; //$int_i ++; } } // echo "tags获取记录数:".$int_i." "; } } } //为tags分配的记录数如果有没处理完的传给key $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(); // $int_i = 0; 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;//tags充许的记录数减少 --$num;//c 允许获取的总记录数减少 // $int_i ++; } } // echo "key获取记录数:".$int_i." "; } } //key也没有处理完就传到下一个表 $next_num = $keygetcount; // echo "向下传的值:". $next_num; } return $num; }
到此准备工作完成,接下来说说我实现的猜你喜欢的原理
未完成的,在下一楼,请向下看
|
[该贴被修改 10 次,最后修改时间 2014-04-15 17:17:53 ]
|