还有很多条件没有判断, 我正则不是很会。 写的很粗糙,勉强能用, 希望别介意哈!
<?php
class Database {
public $conn
public function Database() {
$this->conn = mysql_connect("localhost","user","password")
mysql_select_db("gamesearch",$this->conn)
mysql_query("SET NAMES 'UTF8'")
}
public function get_data_array($sql) {
$rs = mysql_query($sql,$this->conn)
$data_array = array()
while($data = mysql_fetch_array($rs,MYSQL_ASSOC)) {
$data_array[] = $data
}
mysql_free_result($rs)
return $data_array
}
}
$db = new Database()
if($_GET['year'] == "") {
echo "请输入年份!"
}else {
if($_GET['year'] == "all") {
$sql = "select GameName from GameSearch"
}else {
$sql = "select GameName from GameSearch where ReleaseYear = ".$_GET['year']
}
$data = $db->get_data_array($sql)
foreach($data as $k=>$v) {
echo $v['GameName']."<br />"
}
}
?>
<form action="test.php" method="get">
年份:<input type="text" name="year" value="all" />
<input type="submit" value="提交" />
</form>
动态的Ajax分页,代码如下:<%@ page language="java" contentType="text/html charset=utf-8"
pageEncoding="utf-8"%>
<%
String path = request.getContextPath()
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath %>" />
<meta http-equiv="Content-Type"
content="text/html charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript" src="scripts/jquery-1.5.1.js"></script>
<script type="text/javascript">
var list // the list of data
var totalPages // the total of pages
var pageSize = 5 // each size of page
var pageIndex = 1 // the index of current page
$(function(){
send()
})
function send() {
$.ajax({
url: "DistrictServlet",
type: "POST",
data: {"function":"list"},
dataType: "json",
success: function(data) {
// list = data
var records = data.length
if (records % pageSize == 0) {
totalPages = records / pageSize
} else {
totalPages = Math.round(records / pageSize)
}
$("#pageIndex").html(pageIndex)
$("#totalPages").html(totalPages)
binding(data)
}
})
}
function changePage() {
$.ajax({
url: "DistrictServlet",
type: "post",
data: {"function":"list"},
dataType: "json",
success: function(data) {
binding(data)
}
})
}
function binding(data) {
var start = (pageIndex - 1) * pageSize
var end = pageIndex * pageSize
var html = ""
$.each(data, function(index, district) {
if (index >= start && index < end) {
// show data
html += "<tr><td>" + district["id"]+ "</td><td>" +district["name"]+"</td></tr>" // .........
}
})
$("table").html(html)
$("#pageIndex").html(pageIndex)
}
function nextPage() {
pageIndex += 1
if (pageIndex > totalPages) {
pageIndex = totalPages
return
}
changePage()
}
function lastPage() {
pageIndex -= 1
if (pageIndex < 1) {
pageIndex = 1
return
}
changePage()
}
function skipPage(index) {
pageIndex = index
changePage()
}
</script>
</head>
<body>
<div><span id="pageIndex"></span>/<span id="totalPages"></span></div>
<div>
<a href="javascript: lastPage()">last</a>
<a href="javascript: nextPage()">next</a>
</div>
<div id="list"><table></table></div>
</body>
</html>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)