這些都是學習過程中慢慢添加的,一次性發布在這篇文章把,一片一片的寫太累了。。。。。。
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>";
}
?>