รับทำเว็บไซต์ครบวงจร
เลือกภาษา Thai English     facebook
รายการหลัก



บทความ


Page Ranking Tool

การทำปุ่ม Loading More นำข้อมูลจาก Mysql โดยใช้ jQuery Ajax


สอน jQuery
> การทำปุ่ม Loading More นำข้อมูลจาก Mysql โดยใช้ jQuery Ajax


You might have seen the implementation of Ajax based data loading technique at Facebook, Twitter or some other sites. Instead of showing pagination links, why not let users load data dynamically on a button click. In this article we’ll try to adopt the technique to make similar loading page for our web projects using jQuery.


jQuery Load More Data

You may also want to checkout similar tutorial Auto Load More Data On Page Scroll. In my earlier post Simple Ajax Pagination, I have explained how to create Ajax Pagination using jQuery. Here we will be doing precisely the same thing, in-fact we’ll be borrowing all sample files from previous post. Everything stays the same, only index.php file will be modified here.

Configuration

As explained in previous post, this is the configuration file to store our MySql database username and password, and also to connect to MySQL using PHP mysqli_connect.

PHP

1
2
3
4
5
6
7
8
9

<?php
$db_username = 'root';
$db_password = '';
$db_name = 'demo';
$db_host = 'localhost';
$item_per_page = 5;

$connecDB = mysqli_connect($db_host, $db_username, $db_password,$db_name)or die('could not connect to database');
?>

Index Page

Here we wont be generating any pagination links like before, we only need to get total records and break them to page numbers, which we will be using with jQuery.

PHP

1
2
3
4
5
6
7
8
9

<?php
include("config.inc.php");

$results = mysqli_query($connecDB,"SELECT COUNT(*) FROM paginate");
$get_total_rows = mysqli_fetch_array($results); //total records

//break total records into pages
$total_pages = ceil($get_total_rows[0]/$item_per_page);
?>

jQuery

Now we have total page number, its time to write some jQuery codes. Like I said, we’ll only be modifying index.php page here.

Since we need to append results into the element, and jQuery .load() method won’t let us insert results as the last child, we will be using jQuery .post() instead of .load() method we previously used in Simple Ajax Pagination examples.

JQUERY

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

$(document).ready(function() {

var track_click = 0; //track user click on "load more" button, righ now it is 0 click

var total_pages = <?php echo $total_pages; ?>;

$('#results').load("fetch_pages.php", {'page':track_click}, function() {track_click++;}); //initial data to load


$(".load_more").click(function (e) { //user clicks on button

$(this).hide(); //hide load more button on click
$('.animation_image').show(); //show loading image

if(track_click <= total_pages) //user click number is still less than total pages
{
//post page number and load returned data into result element
$.post('fetch_pages.php',{'page': track_click}, function(data) {

$(".load_more").show(); //bring back load more button

$("#results").append(data); //append data received from server

//scroll page smoothly to button id
$("html, body").animate({scrollTop: $("#load_more_button").offset().top}, 500);

//hide loading image
$('.animation_image').hide(); //hide loading image once data is received

track_click++; //user click increment on load button

}).fail(function(xhr, ajaxOptions, thrownError) { //any errors?
alert(thrownError); //alert with HTTP error
$(".load_more").show(); //bring back load more button
$('.animation_image').hide(); //hide loading image once data is received
});


if(track_click >= total_pages-1) //compare user click with page number
{
//reached end of the page yet? disable load button
$(".load_more").attr("disabled", "disabled");
}
}

});
});

HTML

Everything’s set, here’s our HTML that contains Load more button, a loading image and place to append the data from fetch_pages.php. Loading image is hidden at first, when user clicks on load more button, we need to show the loading image to indicate data is loading.

HTML

1
2
3
4
5

<div id="results"></div>
<div align="center">
<button class="load_more" id="load_more_button">load More</button>
<div class="animation_image" style="display:none;"><img src="ajax-loader.gif"> Loading...</div>
</div>

Fetching Pages

In previous post, I talked about posting page number to fetch_pages.php from index page, to track the starting point of record. Here, we apply the same method. We pass page number to get starting point of record in fetch_pages.php file.

PHP

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

<?php
include("config.inc.php"); //include config file
//sanitize post value
$page_number = filter_var($_POST["page"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);

//throw HTTP error if page number is not valid
if(!is_numeric($page_number)){
header('HTTP/1.1 500 Invalid page number!');
exit();
}

//get current starting point of records
$position = ($page_number * $item_per_page);

//Limit our results within a specified range.
$results = mysqli_query($connecDB,"SELECT id,name,message FROM paginate ORDER BY id DESC LIMIT $position, $item_per_page");

//output results from database
echo '<ul class="page_result">';
while($row = mysqli_fetch_array($results))
{
echo '<li id="item_'.$row["id"].'"><span class="page_name">'.$row["id"].') '.$row["name"].'</span><span class="page_message">'.$row["message"].'</span></li>';
}
echo '</ul>';
?>

Conclusion

That’s it! we should have a nifty jQuery based data loading page by now. Don’t forget to checkout the demo and download the whole thing in your computer, downloadable file comes along with style file, image and latest jQuery file. Good luck! If you want to make Ajax Pagination with Next-Previous buttons, you can checkout this updated article — Ajax Pagination with jQuery & PHP.

Thanks: http://www.sanwebe.com/2013/03/loading-more-result...



Short URL click!
<< กลับคืน : เข้าชม 4,345 ครั้ง : ขึ้นไปด้านบน

รับทำเว็บไซต์ รับสร้างเว็บไซต์ รับออกแบบเว็บ รับเขียนเว็บ รับสอนทำเว็บ รับเช่า hosting รับเช่าพื้นที่เว็บไซต์ จดชื่อเว็บ รับโปรโมทเว็บไซต์ รับดูแลเว็บ SiteMap
สปริงเกอร์http://www.xn--22c2c4blb9n.xn--o3cw4h/