PHP+Ajax implementation of paging technology:
Paging technology code based on php and ajax. There are two php files below, one is sn_inq.php and the other is sn_show.php. The former php file calls the next php file to implement ajax paging. Just run sn_inq. To achieve the effect, you have to modify the database. The specific code is as follows: Pay special attention to modifications in the areas marked in red!
My database name is inv, the table name is sn, and the fields are: sn_id, sn_plant, sn_sales, sn_act, sn_type, sn_sts....
1.sn_inq.php
//getFormValue is used to get the values of all input controls in the form, and combine the input values into a string and send it to the server.
//注意:此处如果有filesedset的话,form表单一定要放在fieldset里面,否则出错。
echo '
?>
2.sn_show.php:
//page function
function showpage(total){
global page,pagenav,middle,num,pagenum,offset,prepg,nextpg;
//Get the value of page in page=18. If page does not exist, then the page number is 1.
page=isset(_REQUEST['page'])?intval(_REQUEST['page']):1;
//Each layer of paging bars displays 4 paging connections
middle = '4';
//Display 10 pieces of data per page
num=10;
//Get the total number of pages, which is also the last page
pagenum=ceil(total/num);
//Get homepage
page=min(pagenum,page);
//Previous page
prepg=page-1;
//Next page
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','Homepage'). ' ';
pagenav.=' onclick="javascript:showcomment('.prepg.');">'.iconv('gb2312','gb2312',' Previous page').' ';
}else{
pagenav.="".iconv('gb2312','gb2312','Homepage').""." ";
pagenav.="".iconv('gb2312','gb2312','Previous page')."";
}
if(nextpg){
pagenav.=' onclick="javascript:showcomment('.nextpg.');">'.iconv('gb2312','gb2312',' Next page').' ';
pagenav.=' onclick="javascript:showcomment('.pagenum.');">'.iconv('gb2312','gb2312',' Last page').' ';
}else{
pagenav.="".iconv('gb2312','gb2312','next page').""." ";
pagenav.="".iconv('gb2312','gb2312','Last page').""." ";
}
pagenav.=''.iconv('gb2312','gb2312','total') . pagenum .' '.iconv('gb2312','gb2312','page');
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','Go to ');
pagenav.="";
pagenav.="page";
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';
}
?>