Ajax Pagination with Jquery,PHP,Mysql
This is my first experiment in Ajax (I don’t know how to explain this, many thanks if experts give me some tips).
Many thanks to http://yensdesign.com , this post http://yensdesign.com/2008/12/how-to-load-content-via-ajax-in-jquery/ of him just give me an idea to do this.
This Package can load data from table and with all fields.
This contains 5 files
1. content.php (php file that get data from db and send back to the test.php file)
2. test.php (this is the file to be run, before run we must set 7 variables)
3. jquery.js
4. paging.js (this contains the script to sendajax request and get the data)
5. testDB.txt (sample mysql db file)
How to use:
1. Edit test.php and set the 7 variables – per_page, db_name, table_name, order_by then connection variables hostname, username, password
2. Make sure the table exist, correct connection variables and dont miss to set the order_by variable
3. Run test.php
How script works
This is how the paging.js script works
1)
showLoading and hideLoading are functions to show/hide the loading div
2)
When a page no clicked, will call pages.click fucntion, then
show the loading div
reset the page link – make the currently clicked page highlighted
set the url to be sent to the content.php page with page number and all other 7 variables set before
call content.load(url,hideLoading)
3)
initially set the first page loaded
Advantages:
No need to specify the table fields, it will fetch all column names and show them automatically
Configuration settings at only one place
Simple and easy to understand (i hope), not much css works here, so we can change the appearance as we want
test.php (change settings/css here)
Ajax Load Table - Pagination - yensdesign.com
body { margin: 0; padding: 0; font-family:Verdana; font-size:10px }
#loading { width: 100%; position: absolute; text-align: center; font-family:Trebuchet MS; font-size:12px; color:#008000; font-weight:bold }
li{ list-style: none; float: left; margin-right: 16px; text-transform: uppercase; color: #c2c2c2; }
li:hover{ color: #6fa5fd; cursor: pointer; }
<div align="center">
<table border="0" cellpadding="0" cellspacing="0" width="100%" id="table1">
<tr>
<td>
<ul>
<?php
//Show page links
for($i=1; $i<=$pages; $i++)
{
echo '
<li>'.$i.'</li>
';
}
?></ul>
</td>
</tr>
<tr>
<td>
<div id="loading" style="position:absolute;">
LOADING...</div></td>
</tr>
<tr>
<td>
<div id="content" style="height:200px;"></div>
<input type="hidden" name="per_page" id="per_page" value="" />
<input type="hidden" name="db_name" id="db_name" value="" />
<input type="hidden" name="table_name" id="table_name" value="" />
<input type="hidden" name="order_by" id="order_by" value="" />
<input type="hidden" name="hostname" id="hostname" value="" />
<input type="hidden" name="username" id="username" value="" />
<input type="hidden" name="passwd" id="passwd" value="" />
</td>
</tr>
</table>
</div>
paging.js (no need to change anything)
$(document).ready(function(){
//References
var pages = $("#menu li");
var loading = $("#loading");
var content = $("#content");
//show loading bar
function showLoading(){
loading
.css({visibility:"visible"})
.css({opacity:"1"})
.css({display:"block"})
;
}
//hide loading bar
function hideLoading(){
loading.fadeTo(1000, 0);
};
//Manage click events
pages.click(function(){
//show the loading bar
showLoading();
//Highlight current page number
pages.css({'background-color' : ''});
$(this).css({'background-color' : 'yellow'});
//Load content
var pageNum = this.id;
var targetUrl = "content.php?page=" + pageNum + "&" + $("#myForm").serialize() + " #content";
content.load(targetUrl, hideLoading);
});
//default - 1st page
$("#1").css({'background-color' : 'yellow'});
var targetUrl = "content.php?page=1&" + $("#myForm").serialize() + " #content";
showLoading();
content.load(targetUrl, hideLoading);
});
content.php (process request and send response)
Result <div align="center" id="content"> <table border="0" cellpadding="2" cellspacing="2" width="100%" id="table1"> <tr> <td>Sl. No</td> <td></td> </tr> <tr> <td> </td> <?php for($j=0; $j <td></td> </tr> </table> </div>
Full Source
click to see live DEMO here
[...] Posted by beski under Tutorial | Tags: ajax, jquery, mysql, php | No Comments In the previous tutorial there is a drawback, we can not go to a specific page directly, we can not give a link to a user to [...]
Ajax Pagination with Anchor Navigation « Beschi’s Portfolio
April 21, 2009
Seems to be a good addition.
Prabhakar Kasi
April 21, 2009
Thanks for your opinion.
beski
April 22, 2009
# //EDIT these 3 variables for connection
# $hostname = “localhost”;
# $username = “root”;
# $passwd = “”;
- Its not advisable to use this. Better using a config.php for db connection.
Because the user can view the connection parameters by viewing thew page source.
beski
April 25, 2009
I don’t think the user can view the connection parameters. The PHP variables are phased out by the PHP interpreter (web server), unless you echo or print the variables of course.
bb
May 3, 2009
The variables are set to be hidden to be transferred to the content fetching page.
So if the user view the source he can view.
So better using a config file for database connection.
There is an other problem in using GET method in ajax, will post the solution in 2 days.
Thanks for your comment.
beski
May 3, 2009
Hello
They can see the connection parameters in the form, i just view the source of my page in IE8 and firefox, so a config.php is better to use for secure reason.
Thanks
ChileCaliente
September 29, 2009
is it possible to make it per 10 pages? i mean, if you have 1000 records, and per page is 10 records, it’s will displaying 1 – 100 numbers. how to make it per 10 pages as like google search
iwanhg
May 29, 2009
You mean displaying only 10 page numbers with PREV and NEXT if the total pages are 100…?
If you are in page 25, then the page numbers 20 to 30 will be displaying…. rite?
I will work out and tell you in a week.
Thanks.
beski
May 30, 2009
correct… i think it’s will be much better than if you display 100 page numbers in our site…
iwanhg
May 30, 2009
Thank you very much its usefull for me
Ramakrishna
June 24, 2009
okey.
beski
June 24, 2009
[...] Ajax Pagination with jQuery,PHP,Mysql [...]
Best Collection of jQuery Tutorials, Gift for Designers & Developers. | guidesigner.net
August 13, 2009
[...] Ajax Pagination with jQuery,PHP,Mysql [...]
Best Collection of jQuery Tutorials, Gift for Designers & Developers. | Web Design GroundBreak
August 16, 2009
[...] Ajax Pagination with jQuery,PHP,Mysql [...]
Noteefy - Be aware » Best Jquery Lessons Of The Month
August 19, 2009
[...] Ajax Pagination with jQuery,PHP,Mysql [...]
I migliori plugin di jQuery in circolazione sono qui | Italian webdesign
August 24, 2009
Hi are u etho dot com ???? i saw ur tutorial and its nice but ur pic seems to be like him
Suresh
September 24, 2009
Hi suresh,
yes, I am yetho.com
after starting that blog i have no time to write tutorials here.
anyway, if I found anything special, sure I will write.
beski
September 25, 2009
[...] Ajax Pagination with Jquery,PHP,Mysql « Beschi’s Portfolio 1 [...]
今日のゆるニュース « attrip
September 30, 2009
Very good and simple script.
Assaad
October 4, 2009
Thanks Assaad.
beski
October 5, 2009
Hi Beschi, I can’t integrate your code with jquery-ui…
Please try insert jquery-ui code inside on file of content.php, it will not work…
any solution
zuki
October 9, 2009
Hi zuki,
I can not understand.
I do not use javascript in content.php file.
Can you mail me your source? i will try something.
beski
October 9, 2009
I have sent source to you, please find email with subject ‘Cannot integrate your code with jqueryui’
Thanks zuki
zuki
October 9, 2009
I can’t get it to work. Paging seems to be working but besides that nothing is being displayed. Any idea what may be wrong with the code.
Margaret
October 23, 2009
hi,
thank for this nice easy fast ajax thing. But
I found that it’s not reading arabic content
correctly. It’s showing arabic letters like
(??????) !! .. even when adding charset=windows-1256
to the headings … Hope you fix this ASAP.
Thanks again,
Thamer from KSA.
Th
October 29, 2009
Thanks Th.
I am not familiar with language handling. Please refer some other articles related to language handling with database and html.
beski
October 30, 2009
Hello Beski, thanks for your post.
I am quite new to this. I see what you mean by the values being shown when you view the page source. How would I go about hiding that information? You say create a config.php, but then? I threw the variables in the file config.php and require ‘config.php’; in test.php but that leads to the same problem.
Help much appreciated! Thanks.
mousesports
November 29, 2009
Thanks mousesports,
Pls refer this post: http://beski.wordpress.com/2009/07/13/ajax-jquery-paging-with-magic-links/
beski
December 1, 2009
Nice script for practicing paging in Jquery.
Arun
December 2, 2009
Thanks ChileCaliente.
beski
September 30, 2009