1. 程式人生 > >熱詞搜尋

熱詞搜尋

<pre name="code" class="php"><h2><span style="white-space:pre">				</span>HTML頁面</h2><pre name="code" class="php"><!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>搜尋關鍵字</title>
</head>
<body>
	<center>
		關鍵字:<input type="text" name="sou">
		<button>搜尋</button>
		<div></div>
	</center>
</body>
</html>
<script type="text/javascript" src="../public/jquery.js"></script>
<script type="text/javascript">
	$(function(){
		$("button").click(function(){
			var sou = $("input").val();
			var url = "a.php";
			var data = {'sou':sou};
			var str='';
			$.get(url,data,function(msg){
				if(msg)
				{
					for(var i=0;i<msg.length;i++)
					{
						str += '<p>'+msg[i]+'</p>';
					}
					$("div").html(str);
				}
			},'json');

		});
	});
</script>

PHP頁面

<?php
	header("content-type:text/html;charset=utf-8");
	//接收引數
	$sou = $_GET['sou'];
	//次數達到10次   $mem->set($sou,$num,0,0);
	//取出次數       $mem->get($sou);
	$mem = new Memcache();
	$mem->connect('www.liuyang.com',11211);
	//清空資料
	// $mem->flush();die;
	$num = $mem->get($sou);
	//判斷是否有熱詞已經存在memcache中
	if($num)
	{
		$num++;
		$mem->set($sou,$num,0,0);
		if($num>=3)
		{
			//說明該搜尋條件具備作為熱詞的資格
			//熱詞的儲存方式應該是   $mem->set('hot',$data,0,0);
			//把新的熱詞放入到原來的陣列中     
			$data = $mem->get('hot');
			//判斷熱詞是否已經存在memcache中
			if(!in_array($sou, $data))
			{
				$data[] = $sou;
				//重新儲存
				$mem->set('hot',$data,0,0);
			}
			$data = $mem->get('hot');
			echo json_encode($data);
		}
		else
		{
			//返回其他的熱詞
			$data = $mem->get('hot');
			if($data)
			{
				echo json_encode($data);
			}
		}
	}
	else
	{
		$mem->set($sou,1,0,0);
		//返回其他的熱詞
		$data = $mem->get('hot');
		if($data)
		{
			echo json_encode($data);
		}
	}