简明现代魔法 -> Ajax技术 -> Ajax 实现 MySQL 分页

Ajax 实现 MySQL 分页

2010-01-21

效果演示

news.html

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
<script> 
function viewpage(p)
{ 
	if(window.XMLHttpRequest)
	{ 
		var xmlReq = new XMLHttpRequest(); 
	} 
	else if(window.ActiveXObject) 
	{ 
		var xmlReq = new ActiveXObject('Microsoft.XMLHTTP'); 
	} 
	var formData = "page="+p; 
	xmlReq.onreadystatechange = function()
	{ 
		if(xmlReq.readyState == 4)
		{ 
			document.getElementById('content2').innerHTML = xmlReq.responseText; 
		} 
	} 
	xmlReq.open("post", "news_main_ajaxpage2.php", true); 
	xmlReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
	xmlReq.send(formData); 
	return false; 
} 
</script> 

<body onLoad="viewpage(1)"> 
<div id="content2"></div>   

news_main_ajaxpage2.php

<table width="786" border="0" cellspacing="0" cellpadding="5" height="27" 
	background="../images/lanmu_top_2.jpg"> 
<tr> 
<td></td><td align="right"></td> 
</tr></table> 
<?php 
	$pagesize=20; 
	require('dbinfo.php'); 

	// 连接到 MySQL 服务器
	$connection=mysql_connect ($host, $username, $password);
	if (!$connection) 
	{
  		die('Not connected : ' . mysql_error());
	}

	// 设置活动的 MySQL 数据库
	$db_selected = mysql_select_db($database, $connection);
	if (!$db_selected) 
	{
  		die ('Can\'t use db : ' . mysql_error());
	}

	$rsbig = mysql_query("select * from article"); 
	$myrowbig = mysql_fetch_array($rsbig); 

	$classid_big=$myrowbig['id']; 
	$bigclassname_big=$myrowbig['classname']; 
	//echo $_POST['page']; 
	mysql_select_db($database, $connection); 
	$result = mysql_query("select count(article_id) from article"); 
	$myrow = mysql_fetch_array($result); 
    // 获得数据库记录条数
	$numrows = $myrow[0]; 
    // 计算分多少页
	$pages=intval($numrows/$pagesize); 
	if ($numrows%$pagesize) 
		$pages++; 
	if (isset($_POST['page']))
	{ 
		$page=intval($_POST['page']); 
	} 
	else
	{ 
		//设置为第一页 
		$page=1; 
	} 
	
	$first=1; 
	$prev = $page-1; 
	$next = $page+1; 
	$last = $pages; 
	// 计算记录偏移量 
	$offset = $pagesize*($page - 1); 
	// 读取指定记录数 
	
	$result = mysql_query("select * from article order by article_id desc limit $offset, $pagesize"); 
	$num = @mysql_num_rows($result); 

	while ($row = @mysql_fetch_array($result,MYSQL_NUM)) 
	{ 
		$id[]=$row[0]; 
		$title[]=$row[1]; 
		$date[]=$row[3]; 
		//$countpeople[] = $row[1]; 
	} 
	echo "<TABLE style=\"MARGIN-BOTTOM: 20px\" cellSpacing=0 cellPadding=0 width=786 border=0 
    	align=center class='tableborder3'>\n"; 
	echo "<TBODY>\n"; 
	for($a=0;$a<$num;$a++) 
	{ 
		echo "<TR>\n"; 
		echo "<TD style=\"PADDING-TOP: 5px\" align=left><a href='../php2html/news_details.php?id=".$id[$a]."' target=_blank title='".$title[$a]."' class='news_toptitle_link' style='line-height:1.4em'>·".$title[$a]."</a></TD>"; 
		echo "<Td align='right' width='180'>(".$date[$a].")</td>"; 
		echo "</TR>"; 
	} 
	echo "</TR></TBODY></TABLE>\n"; 
	echo "<TABLE style=\"MARGIN-TOP: 30px\" cellSpacing=0 cellPadding=0 width=\"100%\""; 
	echo "border=0>"; 
	echo "<TBODY><TR><TD colSpan=3 height=20>"; 
	echo "<DIV align=center class='article'>"; 
	echo "<P align=left><FONT color=red>".$page."/</FONT>".$pages." | Total:".$numrows."| "; 
	if ($page==1) 
		echo "<a>First</a> | "; 
	if ($page>1) 
		echo "<a onclick=\"viewpage(".$first.")\" href='#' class='article'>First</a> | "; 
	if ($page==1) 
		echo "<a>Prev</a> | "; 
	if ($page>1) 
		echo "<a onclick=\"viewpage(".$prev.")\" href='#' class='article'>Prev</a> | "; 
	if ($page<$pages) 
		echo "<a onclick=\"viewpage(".$next.")\" href='#' class='article'>Next</a> | "; 
	if ($page==$pages) 
		echo "<a>Next</a> | "; 
	if ($page<$pages) 
		echo "<a onclick=\"viewpage(".$last.")\" href='#' class='article'>Last</a>"; 
	if ($page==$pages) 
		echo "<a>Last</a> | "; 
		echo " <INPUT maxLength=3 size=1 value=1 name=goto_page> 
        <INPUT hideFocus onclick=\"viewpage(document.all.goto_page.value)\" 
        type=button value=Go name=cmd_goto>"; 
		echo "</P></DIV></TD></TR></TBODY></TABLE>"; 
?>   

dbinfo.php

<?
$username="root";
$password="";
$database="nowamagic";
$host="localhost";
?>  

随机文章推荐
网站分类


注:如需转载本文,请注明出处(原文链接),谢谢。更多精彩内容,请进入简明现代魔法首页。

进入新博客
喜欢本文,就分享它吧
给我留言
您的名字:
您的邮件:
您的网站:


 

copyright © 2009 简明现代魔法    学习、分享、进步

power by Gonn 感谢所有关心和支持本站的朋友们