公司网站正在新做的一个功能
考虑到最近blog里没写什么新的东西就把这段代码贴一下
test.html
XML/HTML代码
- <html>
- <head>
- <title>自动从标题中提取关键词及ajax输入提示</title>
- <script src="js/jquery.js" type="text/javascript" charset="utf-8"></script>
- <link href="css/global.css" rel="stylesheet" type="text/css" />
- </head>
- <body style="padding:20px;">
- <form id="form1" name="form1" method="post" action="javascript:;">
- 标题:<input type="text" name="title" id="title" />
- <label>
- <input type="submit" name="button" id="button" value="提交" onclick="sendTitle()"/>
- </label>
- <input name="add" type="hidden" id="add" value="add" />
- </form>
- <br>
- 企业:<input type="text" name="key" id="key" onkeyup="showKey()"/>
- <input type="hidden" name="keys" id="keys" value="">
- <style>
- #showBox{overflow:auto;width:250px;border:1px solid #eee;height:100px;margin-top:10px;}
- div{padding:3px;}
- </style>
- <div id="showBox"></div>
- <script type="text/javascript">
- function sendTitle(){
- if($("#title").val()==”){
- alert("标题不能为空");
- }else{
- $.post("?index/test/sendtitle",{title:$("#title").val()},function(data){
- var html="";
- for(var i = 0; i < data.length; i++){
- html+="<div>"+data[i].companyid+" | "+data[i].c_name+"</div>"
- }
- $("#showBox").html(html);
- $("#key").val($("#title").val());
- $("#keys").val($("#title").val());
- },"json");
- }
- }
- function showKey(){
- var key=$("#key").val();
- var keys=$("#keys").val();
- if(key!=keys){
- $.post("?index/test/sendtitle",{title:key},function(data){
- var html="";
- for(var i = 0; i < data.length; i++){
- var ii2=i+1;
- html+="<div>"+i2+" | "+data[i].c_name+"</div>"
- }
- $("#showBox").html(html);
- $("#keys").val(key);
- },"json");
- }
- }
- </script>
- </body>
- </html>
php处理部分:
PHP代码
- <?php
- class box
- {
- public function __construct($db)
- {
- $this->db=$db;
- }
- //读出全部的企业关键词,用于处理标题,提取标题中对应有关的企业列表
- public function readCompanyKey($cachetime=3600*24) //关键词库的缓存时间默认为一天
- {
- global $ROOT_PATH;
- $file=$ROOT_PATH.‘/temp/CompanyKey.inc’;
- if(!file_exists($file)){
- $this->writeCompanyKey();
- }else{
- if(time()-filemtime($file)>$cachetime){
- $this->writeCompanyKey();
- }
- }
- $row=unserialize(file_get_contents($file));
- return $row;
- }
- //缓存全部的企业关键词库
- public function writeCompanyKey()
- {
- global $ROOT_PATH;
- $file=$ROOT_PATH.‘/temp/CompanyKey.inc’;
- $rs=$this->db->query("select * from `companykey`");
- $row = $rs->fetchall(PDO::FETCH_ASSOC);
- $content=serialize($row);
- $fs=fopen($file,‘w’);
- if(fwrite($fs, $content) === FALSE) {
- echo "不能写入到文件!请注意temp文件夹的读写权限";
- exit;
- }else{
- return true;
- }
- fclose($fs);
- }
- //从标题中通过关键词库获取相关的企业列表
- public function getComapnyList($title)
- {
- $title=strtolower($title);
- $key=$this->readCompanyKey();
- //if(count($key)<=0){echo ‘出错,关键词不存在!’;exit;}
- foreach($key as $v){
- if(strpos($title,$v[‘key’])!==false){
- $companyid[]=$v[‘companyid’];
- //echo ‘1’;
- }
- }
- //print_r($companyid);
- if(count($companyid)>0){
- $companyidArr=$this->listOrder($companyid); //对获取的企业按权重进行排序,去除重复的企业
- foreach($companyidArr as $v){
- $rs=$this->db->query("select `companyid`,`c_name` from `company` where `companyid`=’".$v."’ limit 1");
- $row = $rs->fetchall(PDO::FETCH_ASSOC);
- $list[]=$row[0];
- }
- return $list;
- }else{
- return false;
- }
- }
- //排序算法,按标题中同一个关键词的出现个数对其所对应的企业进行排序,并去除重复企业
- public function listOrder($arr){
- if(!is_array($arr)){
- return ”;
- }
- $count=count($arr);
- $arr2=array_unique($arr);
- foreach($arr2 as $k=>$v){
- $i=0;
- $i2=0;
- for($l=$i2;$l<$count;$l++){
- if($v==$arr[$l]){
- $i++;
- $s[$k]=$i;
- //$s[$k][‘key’]=$i;
- //$s[$k][‘var’]=$v;
- }
- $i2++;
- }
- }
- arsort($s);
- foreach($s as $k=>$v){
- $row[]=$arr[$k];
- }
- return $row;
- }
- }
- $box=new box($db1);
- if($uri[2]==‘sendtitle’){
- $title=$_POST[‘title’];
- $list=$box->getComapnyList($title);
- echo json_encode($list);
- exit;
- }
- $smarty->display("index/test.html");
- exit();
- ?>
效果