Php + ajax implement data loading without refreshing the new version _ PHP Tutorial

Source: Internet
Author: User
Tags echo date
Php + ajax implements the data loading technology without refreshing. Php + ajax implements brushless newest dynamic loading of data technology. we have used a lot of new functions without refreshing. next I will introduce you to an example, php + ajax is used to implement the number of refreshing new scrolling screens. php + ajax is used to implement the technology of refreshing new dynamic loading data.

We have used a lot of new features without refreshing. next I will introduce you to an instance, that is, implementing php + ajax to load data without refreshing new scrolling screens, the example is very simple. you only need to follow the process.

When we browse some web pages, when the browser's scroll bar is pulled to the bottom of the page, the page will continue to automatically load more content for users to browse. For the moment, this technology is called the scrolling loading technology. We found that many websites use this technology, and Bing Image Search, Sina Weibo, and QQ space fully applied this technology.

The rolling screen loading technology uses Javascript to monitor the position of the scroll bar. every time the scroll bar reaches the bottom of the browser window, an Ajax request is triggered to the PHP program in the background and corresponding data is returned, and append the returned data to the bottom of the page to achieve dynamic loading, which is actually a typical Ajax application. This article uses jQuery and PHP, mysql, and JSON to explain how to apply the scrolling loading technology to your project. Of course, the premise of reading this article is that you need to have a foundation related to jQuery and PHP.

Index. php

By default, 15 data records are displayed. Therefore, the 15 data records starting from the database are displayed on the page. The newly loaded data is displayed in 15 records each time.

To make the explanation as simple as possible, I use native PHP and mysql Query statements. First, you need to connect to the database, including the connection information of connnect. php. Here I have defined several user IDs.

Query the data table, obtain the result set, and output it cyclically. the code is as follows:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

Require_once ('connect. php ');

$ User = array ('demo1', 'demo2', 'demo3', 'demo3', 'demo3 ');

?>

$ Query = mysql_query ("select * from say order by id desc limit 0, 15 ");

While ($ row = mysql_fetch_array ($ query )){

?>

Note: The data used in this example comes from this article: This article describes how to create a data table.

JQuery

1. First, we need to get the page height of the visible area of the browser:

The code is as follows:

Var winH = $ (window). height ();

2. what you need to do when rolling a page is: calculate the total page height (when rolling the bottom, the page loads data, so the total page height changes dynamically ), calculate the position of the scroll bar (the position of the scroll bar also dynamically changes with the height of the page loaded), and construct a formula to calculate the relative proportion.

?

1

2

3

4

5

$ (Window). scroll (function (){

Var pageH = $ (document. body). height (); // the total page height.

Var scrollT = $ (window). scrollTop (); // top of the scroll bar

Var aa = (pageH-winH-scrollT)/winH;

});

3. when the scroll bar approaches the bottom of the page, ajax loading is triggered. In this example, jQuery's getJSON method is used to send the result to the server. php sends a request. the request parameter is page, that is, the number of pages.

?

1

2

3

4

5

If (aa <0.02 ){

$. GetJSON ("result. php", {page: I}, function (json ){

.....

});

}

4. if the request response returns JSON data, parse the JSON data and append the data to the page DIV # container. If no JSON data is returned, all the data is displayed.

?

1

2

3

4

5

6

7

8

9

10

11

If (json ){

Var str = "";

$. Each (json, function (index, array) {// Traverse

Var str = "..."; // JSON data obtained

$ ("# Container"). append (str); // append

});

I ++; // page number + 1

} Else {

$ (". Nodata" ).show().html ("Don't scroll, it's already done... ");

Return false;

}

The complete jQuery code is as follows:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

$ (Function (){

Var winH = $ (window). height (); // The height of the visible area of the page

Var I = 1; // set the current page number

$ (Window). scroll (function (){

Var pageH = $ (document. body). height ();

Var scrollT = $ (window). scrollTop (); // top of the scroll bar

Var aa = (pageH-winH-scrollT)/winH;

If (aa <0.02 ){

$. GetJSON ("result. php", {page: I}, function (json ){

If (json ){

Var str = "";

$. Each (json, function (index, array ){

Var str ="

";

Var str + ="

"+ Array ['Date'] +"

";

Var str + ="

"+ Array ['author'] +"

";

Var str + ="

"+ Array ['content'] +"

";

$ ("# Container"). append (str );

});

I ++;

} Else {

$ (". Nodata" ).show().html ("Don't scroll, it's already done... ");

Return false;

}

});

}

});

});

Result. php

When you scroll to the bottom of the page, the front-end Ajax requests the result. php, the background program queries the corresponding records in the data table based on the requested data page, and outputs the record set in json format to the front-end for processing.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

Require_once ('connect. php'); // connect to the database

$ User = array ('demo1', 'demo2', 'demo3', 'demo3', 'demo3 ');

$ Page = intval ($ _ GET ['Page']); // gets the requested page number.

$ Start = $ page * 15;

$ Query = mysql_query ("select * from say order by id desc limit $ start, 15 ");

While ($ row = mysql_fetch_array ($ query )){

$ Arr [] = array (

'Content' => $ row ['content'],

'Author' => $ user [$ row ['userid'],

'Date' => date ('M-d H: I ', $ row ['addtime'])

);

}

Echo json_encode ($ arr); // Convert to json data output

Now, the introduction to this article is over. go and check the effect.

The above is all the content of this article. I hope you will like it.

We have used a lot of new features for refreshing and refreshing. next I will introduce you to an example, which is to implement php + ajax to implement the number of refreshing and scrolling loads...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.