附加功能--保存用户查询历史,显示查询史,复习查询过的单词

这些都是学习过程中慢慢添加的,一次性发布在这篇文章把,一片一片的写太累了。。。。。。

1) 首先是设置用户COOKIE,并把COOKIE名存入到数据库,设置一个关联id,这个唯一的id对应的另外一个表,保存了对应的用户的查询单词

<?php
require_once "sql.class.php";
function setOrGetUser()
{
	$mysqli = new mysqli("localhost", "root", "cai123") or die ('连接数据库失败');
	$mysqli->select_db("words");
	$mysqli->query("set names utf8");
	if(empty($_COOKIE))
	{
		$username=md5($_SERVER["REMOTE_ADDR"].time().rand(1,500));
		setcookie("user", $username, time()+30*24*3600);


		$sql="insert into user(username) values('$username')";
		$mysqli->query($sql) or die('插入用户名失败');
		return $username;
	}
	else
	{
		$username = $_COOKIE['user'];
		//下面是一段测试代码,用于检测在清楚数据库保存的cookie时,根据浏览器提交的cookie再次插入数据库,并返回
		$sql = "SELECT * FROM user WHERE username ='$username' ";
		$res = $mysqli->query($sql);
		if($mysqli->affected_rows==0)
		{
			$sql="INSERT INTO user(username) VALUES('$username') ";
			$mysqli->query($sql);
			if($mysqli->affected_rows<1)
			{
				die('插入用户失败');
			}
			return $username;
		}
		else
		{
			$time = $res->fetch_assoc();
			//当超过最大时间时,删除用户的查询史
			if($time['udate']+3600*24*10 > time())
			{
				$sql = new Sql($username);
				$sql->deleteWord();
			}
			$res->free();
		}
		return $username;
	}

}

//echo setOrGetUser();

/这个类sql.class.php封装了保存用户cookie,存储单词和查询单词的一些操作


<?php
//require_once "cookie.php";
class Sql
{
	private $host="localhost";
	private $username="root";
	private $passwd="cai123";
	private $db="words";
	private $conn=null;

	private $uid=null;
	//根据user表的ID主键,关联到对应的DIC的UID字段里,进行查询单词的保存
	public function __construct($cookiename='',$word='')
	{
		$this->conn=new mysqli($this->host, $this->username, $this->passwd) or die('连接mysql失败'.mysql_error);
		$this->conn->select_db($this->db);
		$this->conn->query("set names utf8");

		$sql="SELECT id FROM user WHERE username='$cookiename' ";
		$result = $this->conn->query($sql);
		if($this->conn->affected_rows)
		{
			$this->uid = $result->fetch_row()[0];
			$result->free();
		}
		else
		{
			die("没有关于你的信息");
		}
		if($word!='')
		{
			$this->setWord($word);
		}
	}
	//将用户的查询记录保存到数据库
	public function setWord($word)
	{
		$sql="INSERT INTO dic(uid,word) VALUES($this->uid,'$word')";
		$this->conn->query($sql);
		if($this->conn->affected_rows)
		{
			//file_put_contents("word.txt", "插入单词{$word}成功",FILE_APPEND);
		}
		else
		{
			file_put_contents("word.txt", "插入单词{$word}失败",FILE_APPEND);
		}
	}
	public function getOneWord()
	{
		$sql = "SELECT word FROM dic WHERE uid={$this->uid} LIMIT 1";
		$result=$this->conn->query($sql);
		if(!$this->conn->errno)
		{
			$res=$result->fetch_row()[0];
			$result->free();
			return $res;
		}
		else
		{
			return false;
		}
	}
	public function getAllWord()
	{
		$sql = "SELECT word FROM dic WHERE uid={$this->uid} ";
		$result=$this->conn->query($sql);
		if($this->conn->affected_rows)
		{
			while($res = $result->fetch_row())
			{
				$words[]=$res[0];
			}
			$result->free();
			return array_unique($words);
		}
		else
		{
			return array("你没有查询历史");
		}
	}
	//根据USER表中保存的时间戳字段,当超过一段时间后,删除对应的查询单词保存记录
	public function deleteWord()
	{
		$sql = "DELETE FROM dic WHERE uid=".$this->uid ;
		$this->conn->query($sql);
	}
	public function clearHistory()
	{
		$sq01 = "DELETE FROM user WHERE id = ".$this->uid; 
		$sq02 = "DELETE FROM dic WHERE uid= ".$this->uid;
		$this->conn->query($sq02);
		$this->conn->query($sq01);
	}
	public function __destruct()
	{
		$this->conn->close();
	}
}

/* $sql = new Sql("MTI3LjAuMC4x");
$sql->setWord("hello"); */

这段程序用于提供返回用户查询史


<html>

<body bgcolor="#RGB(67,65,65)">
<img src="../image/logo.jpg" style='margin-left: 400px;' /><br />
<?php
require_once "../sql.class.php";
require_once "../rollpage/rollpage.class.php";
if(empty($_COOKIE['user']))
{
	die("你没有查询历史");
}
else
{
	$username = $_COOKIE['user'];
	$sql = new Sql($username);
	if($words = $sql->getAllWord())
	{
		//将数据传给分页类进行分页
		$href = $_SERVER['PHP_SELF'];
		$page = new Rollpage($words, $href);
		$page->getLink();
	}
	else
	{
		die("你没有查询历史");
	}
}
?>
</body>
</html>

//下面这个类封装了分页代码,当用户查询的单词比较大时,在查看用户查询史时,而已对单词进行分页显示


<?php
/*唯一的参数是控制器传过来的数据,数据的形式以数据传递,可以作为一个通用分页类*/
class Rollpage
{
	//分成多少页
	private $page_total;
	//分页大小
	private $page_size = 15;
	//被分页的页面地址
	private $page_link;
	//当前页
	private $page_current ;
	//需要被分页的数据,这里是对数组进行分页
	private $words;
	//整体翻页大小
	private $rollpage_size=10;
	
	//首页
	private $home;
	//尾页
	private $end;
	//共有多少页
	private $total;
	
	private $link='';
	private $pos='';
	private $nagv='';
	private $table='';
	public function __construct($words, $href)
	{
		$len = count($words);
		$this->words=$words;
		$this->page_total = ($len > 0)? ceil($len/$this->page_size):0;
		$this->page_link=$href;
		$this->check();
	}
	private function check()
	{
		if(!isset($_GET['offset']))
		{
			$this->page_current=1;
		}
		else
		{
			$offset = $_GET['offset'];
			if($offset > $this->page_total)
			{
				$this->page_current = $this->page_total;
			}
			else if($offset < 1)
			{
				$this->page_current = 1;
			}
			else
			{
				$this->page_current = $offset;
			}
		}
	}
	
	//分页数据
	private function data()
	{
		return array_chunk($this->words, $this->page_size)[$this->page_current-1];
	}
	//首页
	private function home()
	{
		$this->home = "<a href =".$this->page_link."?offset=1 style='text-decoration:none ;'>首页</a> ";
	}
	//尾页
	private function end()
	{
		$this->end = "<a href ='".$this->page_link."?offset=".$this->page_total." '  style='text-decoration:none ;' >尾页</a>   ";
	}
	//总页数
	private function total()
	{
		$this->total="共有<span style='color:#eeabf0;'>".$this->page_total."</span>页";
	}
	//创建分页链接,当前页禁用<a>..</a>并突出显示
	private function link()
	{
		if($this->page_total > $this->rollpage_size)
		{
			//根据分页大小,当分页总数大于10页时,每次显示10页内容
			$start = floor(($this->page_current-1)/$this->rollpage_size)*$this->rollpage_size+1;
			$end = $start+$this->rollpage_size;
			//整体向上翻10页
			if($this->page_current > $this->rollpage_size)
			{
				$offset = $start-1;
				$this->link .= "<a href='{$this->page_link}"."?offset={$offset}' "." style=\"text-decoration:none\" > ".'<<<'."<a> ";
			}
			//整体翻十页关键代码
			for($start; $start < $end; $start++)
			{
				if($start < $this->page_total)
				{
					if($this->page_current == $start)
					{
						$this->link .= "<span style='text-decoration:none; font-size: 19px; color: white;' >".$start."</span> ";
					}
					else
					{
						$this->link .= "<a href='{$this->page_link}"."?offset={$start}' "." style=\"text-decoration:none\" >".$start."<a> ";
					}
				}
				
			}
			//根据边界条件,显示上一页
			if($this->page_current > 1)
			{
				$next_page = $this->page_current-1;
				$this->link .= "<a href='{$this->page_link}"."?offset={$next_page}' "." style=\"text-decoration:none\" > ".'上一页'."<a> ";
			}
			if($this->page_current < $this->page_total)
			{
				$next_page = $this->page_current+1;
				$this->link .= "<a href='{$this->page_link}"."?offset={$next_page}' "." style=\"text-decoration:none\" > ".'下一页'."<a> ";
			}
			//整体向下翻10页
			if($end < $this->page_total)
			{
				$this->link .= "<a href='{$this->page_link}"."?offset={$end}' "." style=\"text-decoration:none\" > ".'>>>'."<a> ";
			}
		}
		//根据分页大小,当分页总数小于10页时,显示基本分页信息
		else
		{
			for($i=1; $i <= $this->page_total;$i++)
			{
				//当前页时,突出显示
				if($this->page_current == $i)
				{
					$this->link .= "<span style='text-decoration:none; font-size: 19px; color: white;' >".$i."</span> ";
				}
				else
				{
					$this->link .= "<a href='{$this->page_link}"."?offset=$i' "." style=\"text-decoration:none\" >".$i."<a> ";
				}
			}
		}
	}
	//添加一个跳转表单
	private function pos()
	{
		$this->pos = "<form  action='{$this->page_link}' method='get' style='display: inline;'><input type='text' name='offset' style='width:40px;' > ".
		"<input type='submit' value='跳转' id='submit' style = 'width: 35px; height: 20px ; margin-top: 2px ; padding:1px;' ></form> ";
	}
	//分页信息的头部(1)需要修改为通用的
	private function header()
	{
		$this->table .= "<div style='margin-left: 400px; margin-top: -18px;'>
		<span style='width: 120px; height:30px; background-color: #a00000; color: #00a000; font-size: 18px;'>查询历史</span>
		<span style='width: 120px; height:30px;  margin-left: 10px ;background-color: #a00000;  font-size: 18px;'><a href='clearHistory.php' target='_blank'  style='text-decoration:underline;color: #00a000; '>清空历史</a></span>
		<span style='width: 120px; height:30px;  margin-left: 10px ;background-color: #a00000;  font-size: 18px;'><a href='reviewWord.html' target='_blank'  style='text-decoration:underline;color: #00a000; '>复习单词</a></span></div>";
		
		$this->table .= "<table style=' width: 720px;  margin-left: 400px; border: 1px solid gray; padding-left: 80px;'>";
	}
	//分页主体(2)需要修改为通用的
	private function table()
	{
		$this->header();
		$word = $this->data();
		$len = count($word);
		for($i=0; $i < $len; $i = $i+3)
		{
			$one = isset($word[$i]) ? $word[$i] : "";
			$two = isset($word[$i+1]) ? $word[$i+1] : "";
			$three = isset($word[$i+2]) ? $word[$i+2] : "";
			$this->table .= "<tr>";
			$this->table .= "<td>".$one."</td>"."<td>".$two."</td>"."<td>".$three."</td>";
			$this->table .= "</tr>";
		}
		$this->table .= "</table>";
		
	}
	//用户接口,生成分页信息
	public function getLink()
	{
		$this->home();
		$this->end();
		$this->total();
		$this->link();
		$this->pos();
		$this->table();
		$this->nagv = $this->home.$this->link.$this->end.$this->pos.$this->total;
		echo $this->table;
		echo "<div style='margin-left: 400px; width: 720px; margin-top: 2px;' >".$this->nagv."</div>";;
	}
}
?>

//下面这个类用户提供复习单词时提供服务,这个功能模仿了有道的复习单词的功能,也就是随机回显你查询过的单词,如果你记得对应的翻译,可以点击按钮进入下一个,如果你不记得,可以点击获取翻译,回显对应的翻译,一次性发完:

<?php
require_once "../sql.class.php";
require_once "../processCheck.class.php";
require_once "../storeWord.class.php";
require_once "../format.class.php";
class ReviewWord
{
	private $cookiename=null;
	private $words=null;
	private $check=null;
	private $storeword=null;
	private $trans;
	public function __construct()
	{
		if(empty($_COOKIE['user']))
		{
			die("你没有查询历史");
		}
		else
		{
			$this->cookiename = $_COOKIE['user'];
			
		}
		$sql = new Sql($this->cookiename);
		$this->check = new Check();
		$this->storeword=new StoreWord();
		$this->trans = new FormatTrans();
		if(!($this->words = $sql->getAllWord())) die('你没有查询史');
	}
	public function getWord()
	{
			$key = array_rand($this->words, 1);
			return $this->words[$key];
	}
	public function getTrans($word)
	{
		$wordzone = $this->check->matchDic($word);
		$trans = $this->trans->trans($this->storeword->getWord($wordzone, $word));
		return $trans;
	}
}
?>
//这个事控制器

<?php 
header("content-type: text/html");
require_once "reviewWord.class.php";
if(!empty($_GET['type']))
{
	$type=$_GET['type'];
	$review = new ReviewWord();
	if($type==1)
	{
		$res['flag']=1;
		$res['result']=$review->getWord();
		echo json_encode($res);
	}
	else if($type==2)
	{
		$word=$_GET['word'];
		$res['flag']=2;
		$res['result']=$review->getTrans($word);
		echo json_encode($res);
	}
}


//这个事界面:

<html>
<head>
<style type="text/css">
#layer{width: 1000px; height:500px; background-color:#abcdab;}
#reviewZone{position: absolute; top: 120px;left: 410px;}


#word{width:70px; height:20px; border: 1px solid #bcbcbc; background-color: #ff0000;}
#button{  woidth: 70px ;height: 20px; border:1 solid #FFCC00;color: #FFCCFF;font-size: 12pt;font-style: normal;font-variant: normal;font-weight: 
normal; line-height: normal;background-color: #9900FF;}

#getTrans{width:70px; height:20px;border:1 solid #FFCC00;color: #FFCCFF;font-size: 12pt;font-style: normal;font-variant: normal;font-weight: normal;line-height: normal;background-color: #9900FF;}

#content{position: absolute; top: 140px;left: 410px;}
</style>

<script type="text/javascript">
function getXMLHttpRequest()
{
	var xmlhttp=null;
	if(window.ActiveXObject)
	{
		xmlhttp = new ActiveXObject("Microsoft.XMLHttp");
	}
	else
	{
		xmlhttp=new XMLHttpRequest();
	}
	return xmlhttp;
}
function query(type)
{
	var url="/ciba/niujin-alpha/review/reviewWord.process.php";
	if(type==1)
	{
		var data ="?type=1";
	}
	else if(type==2)
	{
		var word=$("word").innerHTML;
		var data ="?type=2&word="+word; 
	}
	 var xmlhttp=getXMLHttpRequest();
	 if(xmlhttp)
	{
		xmlhttp.open("get", url+data,true);
		//window.alert(url+data);
		xmlhttp.onreadystatechange=function()
		{
			if (xmlhttp.readyState==4 && xmlhttp.status==200)
			{
				var res=xmlhttp.responseText;
				res=eval("("+res+")");
				if(res.flag==1)
				{
					$("word").innerHTML = res.result;
				}
				else if(res.flag==2)
				{
					item='';
					var words = res.result;
					for(i in words)
					{
						item += "<p style = ' border: 1px solid gray; width:650px; background-color:grey; color: #000090;'>"+words[i]+"<p>";
					}
					$("content").innerHTML = item;   
				}
			}
		}
		xmlhttp.send(null);
	}	 
}
function $(id)
{
	return document.getElementById(id);
}
function getWord()
{
	query(1);
}

function getTrans()
{
	if($("word").value !="undefined")
	{
		query(2);
	}
}

</script>
</head>
<body bgcolor="#RGB(67,65,65)">
<img src="../image/ici.jpg" style="margin-left: 400px;">

<div id='reviewZone'>
<a id='getTrans'  οnclick="getTrans();" title="点击查看单词的翻译">查看翻译</a>
<a id="button" οnclick="getWord();" >获取单词</a>
<span id='word' ></span>
</div>

<div id='transZone'>
<span id='content'></span>
</div>

</body>
</html>

//下面这段程序。用户可以选择清楚自己的查询历史,比如在复习完一些单词之后,可能不希望他们再次显示:


<?php
require_once "../sql.class.php";
if(empty($_COOKIE['user']))
{
	echo "<script> alert('你没有查询历史')</script>";
}
else
{
	$sql = new Sql($_COOKIE['user']);
	$sql->clearHistory();
	
	echo "<script> alert('你的查询历史已经被清空');</script>";
	
}
?>



附加说明:   这个模块的功能主要是: 保存用户的查询历史,用户可以查看自己的查询史,清除自己的查询史,对查询过的单词进行复习;.......其实代码都很简单,关键在于创意吧,,,勉强自圆其说了   @_@

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值