时间:2021-07-01 10:21:17 帮助过:3人阅读
PHP+Ajax实现分页技术:
基于php和ajax的分页技术代码,下面有两个php文件,一个是sn_inq.php,另一个是sn_show.php,前一个php文件调用后一个php文件,实现ajax分页,运行sn_inq即可实现效果,不过得修改数据库哦。具体代码如下:红色标注的地方要特别注意修改哦!
我的数据库名是inv,表名是sn,字段有:sn_id,sn_plant,sn_sales,sn_act,sn_type,sn_sts.....
1.sn_inq.php
//getFormValue 用于获取表单中所有输入控件的值,并将输入值组成一个字符串传到服务器。
//注意:此处如果有filesedset的话,form表单一定要放在fieldset里面,否则出错。
echo '
';
?>
2.sn_show.php:
//page function
function showpage(total){
global page,pagenav,middle,num,pagenum,offset,prepg,nextpg;
//获取page=18中的page的值,假如不存在page,那么页数就是1。
page=isset(_REQUEST['page'])?intval(_REQUEST['page']):1;
//每层分页条显示4个分页连接
middle = '4';
//每页显示10条数据
num=10;
//获得总页数,也是最后一页
pagenum=ceil(total/num);
//获得首页
page=min(pagenum,page);
//上一页
prepg=page-1;
//下一页
nextpg=(page==pagenum ? 0 : page+1);
offset=(page-1)*num;
if(pagenum<=1) return false;
if(prepg){
pagenav.=' onclick="javascript:showcomment(1);">'.iconv('gb2312','gb2312','首页').' ';
pagenav.=' onclick="javascript:showcomment('.prepg.');">'.iconv('gb2312','gb2312','上一页').' ';
}else{
pagenav.="".iconv('gb2312','gb2312','首页').""." ";
pagenav.="".iconv('gb2312','gb2312','上一页')."";
}
if(nextpg){
pagenav.=' onclick="javascript:showcomment('.nextpg.');">'.iconv('gb2312','gb2312','下一页').' ';
pagenav.=' onclick="javascript:showcomment('.pagenum.');">'.iconv('gb2312','gb2312','尾页').' ';
}else{
pagenav.="".iconv('gb2312','gb2312','下一页').""." ";
pagenav.="".iconv('gb2312','gb2312','尾页').""." ";
}
pagenav.=''.iconv('gb2312','gb2312','共 ') . pagenum .' '.iconv('gb2312','gb2312','页');
for(h=(page-middle<1?1:page-middle);h<=(page+middle>pagenum?pagenum:page+middle);h++){
if(h==page){
pagenav.=" h ";
}else{
pagenav.=" ".iconv('gb2312','gb2312',h)." ";
}
}
pagenav.=" ";
pagenav.=iconv('gb2312','gb2312','转到 ');
pagenav.="";
pagenav.="页";
return pagenav;
}
//connect to database
function db_link()
{
access_id = "root";
db_name = "inv";
@ db = mysql_connect('localhost', access_id, '831025') or
die("Could not connect to database. Please contact with IT supporting team ASAP.");
mysql_query("SET NAMES 'GBK'");
mysql_select_db(db_name);
return db;
}
link = db_link();
//get inquiry criteria,用 POST取得数据也行
sn_id_1 = _REQUEST['sn_id_1'];
sn_id_2 = _REQUEST['sn_id_2'];
//inquiry total pages
sn_sql = "SELECT * FROM sn WHERE 1 ";
if (sn_id_1 != ''){
sn_sql .= "AND sn_id >= '".sn_id_1."' ";
}
if (sn_id_2 != ''){
sn_sql .= "AND sn_id <= '".sn_id_2."' ";
}
sn_sql .= "ORDER BY sn_id DESC ";
sn_res = mysql_query(sn_sql);
total = mysql_num_rows(sn_res);
//show page
pageshow = showpage(total);
//inquiry current page
sn_sql .= " limit offset,num";
sn_res = mysql_query(sn_sql);
sn_num = mysql_num_rows(sn_res);
//output inquiry result as XML
header("Content-Type: text/xml");
if(total > 0){
echo '';
echo '
echo '
echo '';
echo ']]>';
echo '
}else{
echo 'find nothing';
}
?>
http://www.bkjia.com/PHPjc/815027.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/815027.htmlTechArticlePHP+Ajax实现分页技术: 基于php和ajax的分页技术代码,下面有两个php文件,一个是sn_inq.php,另一个是sn_show.php,前一个php文件调用后一个ph...