Php + ajax + SQL implementation paging previous chapter: php + ajax + SQL for data interaction
The previous chapter explains how to asynchronously load data using php + ajax + SQL. now let's take a look at the page. here we use ajax to achieve the paging effect.
Note: most of the code here is similar to that in the previous chapter. I will use this method to display it in different places.
1. create an SQL table. The table content is as follows:
2. create a public file conn. php to connect to the database:
Header ("Content-Type: text/html; charset = utf8"); // Declare the encoding format $ conn = mysql_connect ("Localhost","Root","Aaaaaa")Or die("Database connection error". Mysql_errno (); // Connect to SQL mysql_select_db ("Phptest", $ Conn); mysql_query ('Set NAMES utf8')Or die('Character Set setting error'. Mysql_error (); // sets the input character set encoding.?>
3. the php Server provides the front-end ajax data interface to create the phptoAJAX file. Php
Require_once("Conn. php"); // Import a public file$ Page =Isset($ _ POST ["Index"])? $ _ POST ["Index"]: 1; // limit the number of rows, $ _ POST ["Index"] indicates the data submitted by the front-end ajax.
$ Query = mysql_query ("SElECT * FROM txt LIMIT$ Page, 5")Or die("Error message:". Mysql_error (); // dynamically modify the base number of the retrieved rows $ page$ JsonArray =Array(); // New data is used to receive data groups corresponding to each row of the databaseWhile($ Rows = mysql_fetch_array ($ query )){
// Process automatically corresponding content in the database $ rows ['Content'] = Mb_substr (strip_tags (htmlspecialchars_decode ($ rows ['Content']), 0,100,"UTF-8"); // Add the database content to the new array
Array_push ($ jsonArray, $ rows); // note that $ rows}EchoJson_encode ($ jsonArray); // Convert to json and pass it to the front end
4. create a new phpToAJAX. HTML front-end to receive data. here I use the ajax method encapsulated by jquery, as shown in the page after execution:
html><html><head lang="en"> <meta charset="UTF-8"> <title>
title><Style> // Add a vertex style to the paging node*{Margin: 0 ;}Ul{Height400Px;Width800Px;Margin: 0Auto;}.Page{Width800Px;Height: 30Px;Margin: 0Auto;}Li{Font-size: 14Px;}Span{Padding: 0 2Px;Cursor:Pointer;}.Inline{Background:#009999;Color:# Fff;}.Inline:Hover{Color:#006600;Text-decoration:Underline;}
Style><ScriptType ="Text/javascript"Src =Jquery-1.8.3.min.js">
Script>
Head> <Body>
// Obtain The Table function
$ Pagenum = ceil ($ count_array ['count (*) ']/10 );// Obtain The Table function
For ($ I = 1; $ I <= $ pagenum; $ I ++)// Generate the DOM for paging
{Echo ''. $ I.'';}?><UlId ="List">
Ul> <ScriptType ="Text/javascript">
$(function(){ $.ajax({ Type:"Post", // Transfer method
Url:"PhpToAJAX. php", // Data interface
DataType:"Json", // Receiving format
Success:Function(Msg) // if the receiving succeeds, execute the following
{ var li="";
For(VarI = 0; I
Length-1; I ++) // A limit of 10
{ Li + =""+ Msg [I] ['Title'] +""+ Msg [I] ['Content'] +"... + Msg [I] ['Id'] +"'Target = '_ blank'>Details
";} $ ("# List"Pai.html (li );
},
Error:Function() // If the receiving fails, execute the following
{Console. log ("Link error")}});}); $ ("Span"). Click (Function() // Submit ajax to retrieve data again when you click the page node DOM {VarIndex = $ ("Span"). Index (This); $ ("Span"). Eq (index). addClass ("Inline"). Siblings (). removeClass ("Inline"); $. Ajax ({Type:"Post",Url:"PhpToAJAX. php",DataType:"Json",Data:{"Index": Index}, success:Function(Msg ){VarLi ="";For(VarI = 0; I
Length
; I ++) // 10 {li + =""+ Msg [I] ['Title'] +"Upload time:"+ Msg [I] ['Createtime'] +"
"+ Msg [I] ['Content'] +"... + Msg [I] ['Id'] +"'Target = '_ blank'>Details
"; // Alert (msg. length)} $ ("# List").Html (li) ;}, error:Function(){Console. Log ("Link error")}});})
Script>
Body>
Html>
5. click the "details" link shown in the previous step to view the corresponding article content and create a phpArtcle. php file.
Require_once("Conn. php"); $ Id = $ _ GET ['Art']; // Receives data uploaded by the front-end// Query the corresponding content of the database
$ Query = mysql_query ("SELECT * FROM txt WHERE id = '$ ID'") or die ("article error:". mysql_error ());// Traverse the array to display the content
If($ Rows = mysql_fetch_array ($ query )){Echo"". $ Rows ['Title']."";Echo"". Htmlspecialchars_decode ($ rows ['Content'])."
";}
------------------- Finished -----------------------