这个是我写的从数据库查东西并分页,希望可以帮助你,谢谢,
<php
session_start();
include('connectphp');
include('utilphp');
if(!isset($_SESSION['user'])){
heander('location:loginhtml');
}else{
$page = 1;//当前页码数
if(isset($_GET['page'])){
page = $_GET['page'];
}else{
$page = 1;
}
$rows = getCount();//获得总记录数
$count = 2;//获得每页记录数
$pages = intval($rows/$count);//获得一个整数值,且该值大于最小值,总页数
if($rows%2) $pages++;
$offset = $count($page-1);//获得偏移量,即每次从哪条记录开始取数据
$sql = "select id,name,age,sex,addresses,date from users limit $offset,$count";
$result = mysql_query($sql);
}
>
<html>
<head>
<script>
function remove(id){
windowlocation="delphpid="+id;
}
function findById(id){
windowlocation="modifyphpid="+id;
}
</script>
</head>
<body>
<center>
<table width="95%">
<tr>
<th>编号</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>住址</th>
<th>入职日期</th>
<th colspan="2"> *** 作</th>
</tr>
<php
if($rows<1){
echo '查无记录!';
}else{
while($row=mysql_fetch_array($result,MYSQL_ASSOC)){
>
<tr>
<td align="center"><php echo $row['id']></td>
<td align="center"><php echo $row['name']></td>
<td align="center"><php echo $row['age']></td>
<td align="center"><php echo $row['sex']=1'男':'女' ></td>
<td align="center"><php echo $row['addresses']=''' ':$row['addresses']></td>
<td align="center"><php echo $row['date']></td>
<td align="center"><input type="image" src="delgif" onClick="remove(<php echo $row['id']>)"></td>
<td align="center"><input type="image" src="edgif" onClick="findById(<php echo $row['id']>)"></td>
</tr>
<php
}
}
>
</table>
<a href="mainphppage=1">首页</a>
<a href="mainphppage=<php echo ($page-1)==01:($page-1);>">上一页</a>
<a href="mainphppage=<php echo ($page+1)>$pages$pages:($page+1);>">下一页</a>
<a href="mainphppage=<php echo $pages;>">尾页</a> 当前第<php echo $page;>页/共<php echo $pages;>页
</center>
<a href="addhtml">增加</a>
</body>
</html>
<php
$mysql_server_name="localhost";
$mysql_username='root';
$mysql_password='';
$mysql_database='hp';
$conn=mysql_connect($mysql_server_name,$mysql_username,$mysql_password);
@mysql_connect("localhost","root","") or die("数据库连接失败");
@mysql_select_db("hp") or die("数据库不存在");
mysql_query("set names utf8");
$pagesize = $_GET['pagesize'] > 0 $_GET['pagesize'] : 10; //默认情况下每页显示10条数据
$pagenum = $_GET['pagenum'] > 0 $_GET['pagenum'] : 0;
$query_start = $pagesize $pagenum;
if($_POST['submit']){
$strsql_add = '';
$strsql_add =$_POST['idc'] " and idc='$_POST[idc]' ":"";
$strsql_add =$_POST['jiguihao'] " and jiguihao='$_POST[jiguihao]' ":"";
$strsql_add =$_POST['neicun'] " and neicun='$_POST[neicun]' ":"";
$strsql_add =$_POST['yingpan'] " and yingpan='$_POST[yingpan]' ":"";
$strsql_add =$_POST['xinghao'] " and xinghao='$_POST[xinghao]' ":"";
$strsql_add =$_POST['cpu'] " and cpu='$_POST[cpu]' ":"";
$strsql_add =$_POST['guishu'] " and guishu='$_POST[guishu]' ":"";
$strsql_add =$_POST['ip'] " and ip='$_POST[ip]' ":"";
}
$strsql="select from zichan where 1=1 $strsql_add limit $query_start, $pagesize";
$result=mysql_db_query($mysql_database,$strsql,$conn);
$row=mysql_fetch_row($result);
$nextpage_num = $pagenum++;
$lastpage_num = ceil(count($row)/$pagesize);
echo '<font face="verdana">';
echo '<table border="1" cellpadding="1" cellspacing="2">';
// 显示字段名称
echo "</b><tr></b>";
for ($i=0; $i<mysql_num_fields($result); $i++)
{
echo '<td bgcolor="#00FF00"><b>'
mysql_field_name($result, $i);
echo "</b></td></b>";
}
echo "</tr></b>";
// 定位到第一条记录
mysql_data_seek($result, 0);
// 循环取出记录
while ($row=mysql_fetch_row($result))
{
echo "<tr></b>";
for ($i=0; $i<mysql_num_fields($result); $i++ )
{
echo '<td bgcolor="#00FF00">';
echo $row[$i];
echo '</td>';
}
echo '<td><a href=\'xiugaiphpid='$row[0]'\'>修改</a>';
echo '</td>';
echo '<td><a href="javascript:void(0)" onclick="if(windowconfirm(\'确认删除\')){windowlocationhref=\'delphpid='$row[0]'\'}">删除</a>';
echo '</td>';
echo "</tr></b>";
}
echo "</table></b>";
echo "</font>";
// 释放资源
mysql_free_result($result);
// 关闭连接
mysql_close($conn);
>
html页面要增加的参数:
首页 增加 &pagenum=0 或者 不加
下一页 &pagenum= $nextpage_num
尾页 &pagenum= $lastpage_num
jsp中分页最快捷的办法是用分页组件:
分页组件代码使用taglib实现的:
<%@ tag language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="/WEB-INF/tld/ctld" prefix="c"%>
<%@ attribute name="curIndex" type="javalangLong" required="true"%>
<%@ attribute name="pageSize" type="javalangLong" required="true"%>
<%@ attribute name="pagerRange" type="javalangLong" required="true"%>
<%@ attribute name="totalPage" type="javalangLong" required="true"%>
<%@ attribute name="formId" type="javalangString" required="true"%>
<%
long begin = Mathmax(1, curIndex - pagerRange/2);
long end = Mathmin(begin + (pagerRange-1),totalPage);
requestsetAttribute("p_begin", begin);
requestsetAttribute("p_end", end);
%>
<table class="pager">
<tr>
<% if (curIndex!=1){%>
<td><a href="javascript:gotoPage(1)">首页</a></td>
<td><a href="javascript:gotoPage(<%=curIndex-1%>)">上一页</a></td>
<%}else{%>
<td class="disabled"><a href="#">首页</a></td>
<td class="disabled"><a href="#">上一页</a></td>
<%}%>
<c:forEach var="i" begin="${p_begin}" end="${p_end}">
<c:choose>
<c:when test="${i == curIndex}">
<td class="active"><a href="#">${i}</a></td>
</c:when>
<c:otherwise>
<td><a href="javascript:gotoPage(${i})">${i}</a></td>
</c:otherwise>
</c:choose>
</c:forEach>
<% if (curIndex!=totalPage){%>
<td><a href="#">下一页</a></td>
<td><a href="#">末页</a></td>
<%}else{%>
<td class="disabled"><a href="javascript:gotoPage(<%=curIndex+1%>)">下一页</a></td>
<td class="disabled"><a href="javascript:gotoPage(<%=totalPage%>)">末页</a></td>
<%}%>
<td><a>共${totalPage}页</a></td>
<td class="input_li">跳转到:<input type="text" id="p_pageIndex" size="2" value="<c:out value="${pageIndex}"/>"/>页 <input type="button" id="gotoBtn" onclick="gotoPageByBtn()" value="GO"/></td>
<td class="input_li"> 每页:
<select id="p_pageSizeSelect" onchange="gotoPage(<%=curIndex%>)">
<option value="10" <c:if test="${pageSize==10}">selected</c:if>>10条</option>
<option value="20" <c:if test="${pageSize==20}">selected</c:if>>20条</option>
<option value="50" <c:if test="${pageSize==50}">selected</c:if>>50条</option>
</select>
</td>
</tr>
</table>
jsp中使用方法:
<%@ taglib uri="/WEB-INF/tld/ctld" prefix="c"%>
<%@ taglib uri="/WEB-INF/tld/fmttld" prefix="fmt"%>
<%@ taglib tagdir="/WEB-INF/tags" prefix="tags"%>
<head>
<style><!--分页样式-->
pager { font: 12px Arial, Helvetica, sans-serif;}
pager a {padding: 1px 6px; border: solid 1px #ddd; background: #fff; text-decoration: none;margin-right:2px;line-height:30px;vertical-align:middle;}
pager active a{color:red;border:none;}
pager a:visited {padding: 1px 6px; border: solid 1px #ddd; background: #fff; text-decoration: none;}
pager a:hover {color: #fff; background: #ffa501;border-color:#ffa501;text-decoration: none;}
pager input_li{padding: 1px 6px;}
</style>
<script><!--分页跳转脚本-->
function gotoPage(pageIndex){
var queryForm = documentgetElementById("queryForm");
var action = queryFormaction;
var pageSize = documentgetElementById("p_pageSizeSelect")value;
action += "pageIndex=" + pageIndex + "&pageSize=" + pageSize;
//alert(action);
queryFormaction = action;
queryFormsubmit();
}
function gotoPageByBtn(){
var pageIndex = documentgetElementById("p_pageIndex")value;
var pageIndexInt = parseInt(pageIndex);
var totalPage = ${totalPage};
if(pageIndexInt>0 && pageIndexInt<totalPage){
gotoPage(pageIndex);
}
else{
alert("输入页数超出范围!");
}
}
</script>
</head>
<body>
<form id="queryForm" action="${basePath}/log/list" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="userName" value="<c:out value="${userName}"/>"/> </td>
<td><input type="submit" text="查询"/></td>
</tr>
</table>
</form>
<tags:pager pagerRange="10" pageSize="${pageSize}" totalPage="${totalPage}" curIndex="${pageIndex}" formId="queryForm"></tags:pager>
<table class="border">
<thead>
<tr>
<th width="100">用户名称</th>
<th width="500"> *** 作内容</th>
<th width="200"> *** 作时间</th>
</tr>
</thead>
<tbody>
<c:forEach items="${logList}" var="log">
<tr>
<td>${loguserName}</td>
<td>${logresult}</td>
<td>
<fmt:formatDate value="${logcreateTime}" pattern="yyyy-MM-dd HH:mm:ss"/>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<tags:pager pagerRange="10" pageSize="${pageSize}" totalPage="${totalPage}" curIndex="${pageIndex}" formId="queryForm"></tags:pager>
</body>
以上就是关于PHP高手进,改写个类似百度提交表单后的分页显示,分页模块已写好,只求点上一页或下一页时能显示!全部的内容,包括:PHP高手进,改写个类似百度提交表单后的分页显示,分页模块已写好,只求点上一页或下一页时能显示!、php mysql查询结果分页显示、jsp 如何将查询结果实现分页,最好简单易懂…等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)