PHP+Ajax implements paging technology_PHP tutorial
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.
//showcomment(page) is used to display paging data and is called by the query button onclick event. The url is the file url for background processing data and outputting XML format data.
function showcomment(page) {
var x = new Ajax('statusid', 'XML');
url = 'sn_show.php?page='+page+'&'+getFormValue(document.form1);
x.get(url, function(s) {
if(s.lastChild){
getbyid("show").innerHTML = "Loading...";
getbyid("show").innerHTML = s.lastChild.firstChild.nodeValue;
removeLoading(document.getElementById("show"));
}
else{
document.form1.submit();
}
});
}
//displayLoading is used to display Loading and prompt the user to wait
function displayLoading(element) {
var image = document.createElement("img");
image.setAttribute("src","progressbar.gif");
image.setAttribute("title","loading...");
var text = document.createTextNode("loading...");
element.appendChild(image);
element.appendChild(text);
}
//removeLoading is used to remove Loading
removefunctionLoading(element){
var image = element.getElementsByTagName("img");
for(var i=0;i
}
}
//ajax
function Ajax(statusId, recvType) {
var aj = new Object();
displayLoading(document.getElementById("show"));
var clientHeight = scrollTop = 0;
if(navigator.userAgent.toLowerCase().indexOf('opera') > -1) {
clientHeight = document.body.clientHeight;
scrollTop = document.body.scrollTop;
} else {
clientHeight = document.documentElement.clientHeight;
scrollTop = document.documentElement.scrollTop;
}
if(document.getElementById(statusId)) {
aj.statusId = document.getElementById(statusId);
document.getElementById(statusId).style.top = 10+"px";
} else {
var divElement = document.createElement("DIV");
divElement.id = "xspace-tipDiv";
divElement.className = "xspace-ajaxdiv";
divElement.style.cssText = "width:200px; height:40px; line-height: 40px; text-align: center;";
divElement.style.left = 10+"px";
divElement.style.top = 10+"px";//(clientHeight +scrollTop - 60)
divElement.id = statusId;
document.body.appendChild(divElement);
aj.statusId = divElement;
}
aj.targetUrl = '';
aj.sendString = '';
aj.recvType = recvType ? recvType : 'HTML';//HTML XML
aj.resultHandle = null;
aj.createXMLHttpRequest = function() {
var request = false;
if(window.XMLHttpRequest) {
request = new XMLHttpRequest();
if(request.overrideMimeType) {
request.overrideMimeType('text/xml');
}
} else if(window.ActiveXObject) {
var versions = ['Microsoft.XMLHTTP', 'MSXML.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.7.0', 'Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.5.0', 'Msxml2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP'];
for(var i=0; i
request = new ActiveXObject(versions[i]);
if(request) {
return request;
}
} catch(e) {
//alert(e.message);
}
}
}
return request;
}
aj.XMLHttpRequest = aj.createXMLHttpRequest();
aj.processHandle = function() {
aj.statusId.style.display = '';
if(aj.XMLHttpRequest.readyState == 4) {
if(aj.XMLHttpRequest.status == 200) {
if(aj.recvType == 'HTML') {
aj.resultHandle(aj.XMLHttpRequest.responseText);
} else if(aj.recvType == 'XML') {
aj.resultHandle(aj.XMLHttpRequest.responseXML);
}
aj.statusId.style.display = 'none';
} else {
aj.statusId.innerHTML = xml_http_load_failed;
}
}
}
aj.get = function(targetUrl, resultHandle) {
aj.targetUrl = targetUrl;
aj.XMLHttpRequest.onreadystatechange = aj.processHandle;
aj.resultHandle = resultHandle;
if(window.XMLHttpRequest) {
aj.XMLHttpRequest.open('GET', aj.targetUrl);
aj.XMLHttpRequest.send(null);
} else {
aj.XMLHttpRequest.open("GET", targetUrl, true);
aj.XMLHttpRequest.send();
}
}
aj.post = function(targetUrl, sendString, resultHandle) {
aj.targetUrl = targetUrl;
aj.sendString = sendString;
aj.XMLHttpRequest.onreadystatechange = aj.processHandle;
aj.resultHandle = resultHandle;
aj.XMLHttpRequest.open('POST', targetUrl);
aj.XMLHttpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
aj.XMLHttpRequest.send(aj.sendString);
}
return aj;
}
function getbyid(id) {
if (document.getElementById) {
return document.getElementById(id);
} else if (document.all) {
return document.all[id];
} else if (document.layers) {
return document.layers[id];
} else {
return null;
}
}
//注意:此处如果有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
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-middlepagenum?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_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';
}
?>

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

PHP remains a powerful and widely used tool in modern programming, especially in the field of web development. 1) PHP is easy to use and seamlessly integrated with databases, and is the first choice for many developers. 2) It supports dynamic content generation and object-oriented programming, suitable for quickly creating and maintaining websites. 3) PHP's performance can be improved by caching and optimizing database queries, and its extensive community and rich ecosystem make it still important in today's technology stack.

In PHP, weak references are implemented through the WeakReference class and will not prevent the garbage collector from reclaiming objects. Weak references are suitable for scenarios such as caching systems and event listeners. It should be noted that it cannot guarantee the survival of objects and that garbage collection may be delayed.

The \_\_invoke method allows objects to be called like functions. 1. Define the \_\_invoke method so that the object can be called. 2. When using the $obj(...) syntax, PHP will execute the \_\_invoke method. 3. Suitable for scenarios such as logging and calculator, improving code flexibility and readability.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version
Recommended: Win version, supports code prompts!

Notepad++7.3.1
Easy-to-use and free code editor

Atom editor mac version download
The most popular open source editor