This article uses Ajax paging to briefly describe how to use php and ajax to implement the function of refreshing data pages. If you need it, please refer to it.
Simple mysql Data Table Structure
| The Code is as follows: |
Copy code |
|
Create table messages
(
Msg_id int primary key AUTO_INCREMENT,
Message VARCHAR (1, 150)
); |
JavaScript code
In the previous section of ajax, jquery was used for processing.
| The Code is as follows: |
Copy code |
|
<Script type = "text/javascript" src = "http://ajax.googleapis.com/
Ajax/libs/jquery/1.4.2/jquery. min. js "> </script>
<Script type = "text/javascript">
$ (Document). ready (function ()
{
Function loading_show ()
{
Certificate ('{loading'{.html ("}
Function loading_hide ()
{
$ ('# Loading'). fadeOut ();
}
Function loadData (page)
{
Loading_show ();
$. Ajax
({
Type: "POST ",
Url: "load_data.php ",
Data: "page =" + page,
Success: function (msg)
{
$ ("# Container"). ajaxComplete (function (event, request, settings)
{
Loading_hide ();
$ ("# Container" cmd.html (msg );
});
}
});
}
LoadData (1); // For first time page load default results
$ ('# Container. pagination li. activity'). live ('click', function (){
Var page = $ (this). attr ('P ');
LoadData (page );
});
});
</Script> |
Load_data.php
Here we get the data sent by ajax and query the mysql return information through php.
| The Code is as follows: |
Copy code |
|
<? Php
If ($ _ POST ['page'])
{
$ Page = $ _ POST ['page'];
$ Cur_page = $ page;
$ Page-= 1;
$ Per_page = 15; // Per page records
$ Previous_btn = true;
$ Next_btn = true;
$ First_btn = true;
$ Last_btn = true;
$ Start = $ page * $ per_page;
Include "db. php ";
$ Query_pag_data = "SELECT msg_id, message from messages LIMIT $ start, $ per_page ";
$ Result_pag_data = mysql_query ($ query_pag_data) or die ('mysql error'. mysql_error ());
$ Msg = "";
While ($ row = mysql_fetch_array ($ result_pag_data ))
{
$ Htmlmsg = htmlentities ($ row ['message']); // HTML entries filter
$ Msg. = "<li> <B>". $ row ['msg _ id']. "</B>". $ htmlmsg. "</li> ";
}
$ Msg = "<div class = 'data'> <ul>". $ msg. "</ul> </div>"; // Content for data
/* ----- Total count ---*/
$ Query_pag_num = "select count (*) AS count FROM messages"; // Total records
$ Result_pag_num = mysql_query ($ query_pag_num );
$ Row = mysql_fetch_array ($ result_pag_num );
$ Count = $ row ['Count'];
$ No_of_paginations = ceil ($ count/$ per_page );
/* ----- Calculating the starting and endign values for the loop -----*/
// Some Code. Available in download script
}
?> |