直接給出源代碼,可自行分析,寫的不好請留言指正,謝謝!
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/Html4/loose.dtd">
<Html>
<head>
<title>投票結果</title>
<meta http-equiv="content-type" content="text/Html; charset=utf-8" />
<script type="text/Javascript">
function GetXMLHttpObject()
{
var XMLHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return XMLHttp;
}
function check
AJax(){
xmlHttp=GetXMLHttpObject()
if (XMLHttp==null)
{
alert ("您的浏覽器不支持AJax!");
return ;
}else
{
return XMLHttp;
}
}
function preshow(){
ajaxobj = checkAJax();
var url = "page.PHP?page=1";
AJaxobj.onreadystatechange=stateChanged;
AJaxobj.open("GET",url,true);
AJaxobj.send(null);
}
function showHint1(str)
{
ajaxobj = checkAJax();
document.getElementById("msg").innerHtml="正在讀取數據……";
var url = str;
AJaxobj.onreadystatechange=stateChanged;
AJaxobj.open("GET",url,true);
AJaxobj.send(null);
}
function showHint2(str)
{
ajaxobj=checkAJax();
document.getElementById("msg").innerHtml="正在讀取數據……";
var url = str;
AJaxobj.onreadystatechange=stateChanged;
AJaxobj.open("GET",url,true);
AJaxobj.send(null);
}
function showHint3(str)
{
ajaxobj=checkAJax();
document.getElementById("msg").innerHtml="正在讀取數據……";
var url = str;
AJaxobj.onreadystatechange=stateChanged;
AJaxobj.open("GET",url,true);
AJaxobj.send(null);
}
function showHint4(str)
{document.getElementById("msg").style.display="none";
ajaxobj=checkAJax();
document.getElementById("msg").innerHtml="正在讀取數據……";
var url = str;
AJaxobj.onreadystatechange=stateChanged;
AJaxobj.open("GET",url,true);
AJaxobj.send(null);
}
function stateChanged()
{
if (XMLHttp.readyState==4)
{
document.getElementById("message").innerHtml=XMLHttp.responseText;
}
}
</script>
</head>
<body onload="preshow()">
<div id="wrapper">
<h4>用戶留言如下:</h4>
<div id="message">
</div>
</div>
</body>
</Html>
<?PHP
$link = mysql_connect('localhost', 'root', '') or dIE(MySQL_error());
mysql_select_db('vote', $link) or dIE(MySQL_error());
MySQL_query("set names utf8");
$num = 3;
$url = "page.PHP";
$con = "<ul id='msg'>";
$page = (isset($_REQUEST['page'])) ? $_REQUEST['page'] : 1;
$offset = ($page - 1) * $num;
$result = MySQL_query("SELECT COUNT(*) FROM clIEnt");
$total = MySQL_fetch_row($result);
$total = $total[0];
$pagenum = ceil($total / $num);
$page = min($pagenum, $page); //獲得首頁
$prepg = $page - 1; //上一頁
if ($prepg <= 1)
$prepg = 1;
$nextpg = ($page == $pagenum ? 1 : $page + 1); //下一頁
//如果只有一頁則跳出函數:
if ($pagenum <= 1)
return false;
$sql = "SELECT `name`,`content` FROM `clIEnt` LIMIT " . $offset . "," . $num;
$res = MySQL_query($sql);
while ($content = MySQL_fetch_row($res)) {
$con .= "<li><span>$content[0]:</span> $content[1]</li>";
}
$con .= "</ul>";
$con .= <<< PAGE
<p id="page"><a href="#" id="first" onclick="showHint1('$url?page=1')">首頁</a>|<a href="#" id="pre" onclick="showHint2('$url?page=$prepg')">上一頁</a>|<a href="#" id="next" onclick="showHint3('$url?page=$nextpg')">下一頁</a>|<a href="#" id="last" onclick="showHint4('$url?page=$pagenum')">尾頁</a></p>
PAGE;
echo $con;
?>