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 view a particular page. I have faced this problem in many places while play with ajax, and frame content loading.
But now we can solve this by this tutorial: http://yensdesign.com/2008/11/creating-ajax-websites-based-on-anchor-navigation/
Using this i have developed the ajax pagination further, and now we can go directly to a particular page.
Jquery Script (paging.js – inside index.php used to send, receive ajax comm and display results):
No need to change anything here
$().ready(function(){
setInterval("checkAnchor()", 300);
});
//References
var loading = $("#loading");
var content = $("#content");
var pageNum;
var currentAnchor = null;
//show loading bar
function showLoading(){
loading.slideDown("slow");
}
//hide loading bar
function hideLoading(){
loading.slideUp("slow");
};
function checkAnchor(){
//Check if it has changes
if(currentAnchor != document.location.hash){
currentAnchor = document.location.hash;
//if there is not anchor, the loads the default section
if(!currentAnchor)
{
//default - page = 1
pageNum = 1;
}
else
{
//get the page number after #
pageNum = currentAnchor.substring(1);
}
//Send the petition and display the result
showLoading();
var targetUrl = "content.php?page=" + pageNum + "&" + $("#myForm").serialize() + " #content";
content.load(targetUrl, hideLoading);
//reset all link and highlight the current page link
$("#pages a").css({'background-color':''})
$("#pages a[href='#"+ pageNum +"']").css({'background-color':'yellow'});
}
}
PHP file (content.php – receives request and gives response to index.php):
No need to change anything here
<title>Result</title> <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>
index.php (file we run):
Set 7 variables
<title>Ajax Load Table - Pagination - yensdesign.com</title>
body { margin: 0; padding: 0; font-family:Verdana; font-size:10px }
#loading { position: absolute; text-align: center; font-family:Trebuchet MS; font-size:12px; color:#008000; font-weight:bold }
a { float: left; margin-right: 16px; text-transform: uppercase; color: #c2c2c2; }
a:hover { color: #6fa5fd; cursor: pointer; }
<div align="center">
<table border="0" cellpadding="0" cellspacing="0" width="100%" id="table1">
<tr>
<td>
<div id="pages">
<?php
//Show page links
for($i=1; $i<=$pages; $i++)
{
echo '<a href="#'.$i.'">'.$i.'</a>';
}
?>
</div>
</td>
</tr>
<tr>
<td>
<div id="loading" style="position:absolute;">
LOADING...
</div>
</td>
</tr>
<tr>
<td>
<div id="content" style="height:200px;"></div>
<form>
<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="" />
</form>
</td>
</tr>
</table>
</div>

# //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.
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\Documents and Settings\Administrator\Desktop\www\aurl\paging-anchor\content.php on line 32
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\Documents and Settings\Administrator\Desktop\www\aurl\paging-anchor\content.php on line 43
Hey Lauri,
cant understand the warning…
i always use these type of script for fetching data and it works fine for me.
can you explain?
This one doesn’t work at all. Not even page numbers are showing. Just SI.no.
ejem ejem… sure i won’t use these ones… all mysql connection data in clear text ?!?!?!?!
<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="” />
yes,
i have already told that.
better using a separate php file like config.php to store these values.
Thanks Alvaro.
Hi to everyone …
You Should Use <?php echo instead of value <?=
enjoy
hey! this website is the perfect example of a fine word press template am I right? seems all so organized that it even makes me think its perfect! keep it going.
It is a free template, may be well organized.
Thanks.
not usefull very bad script
Hi there beski
Is there a way to not use DB at all? I have a page with anchors that I want to paginate. Thanks in advance for advices and solutions.
Cheers
Adam
how do i play the demo? It ask me for username and password, which i don’t have.
So good article, it help me alot. I dont want to use events base jquery pagination plugins, becos its not SEO friendly. You save my life ^^
Thanks!