Jump to content

ajax pagination from mysql db


acctman

Recommended Posts

does anyone have a working example of an ajax pagination with mysql? i've already looked through google and they all reference the same dynamicdrive example in which it does show much as to how to do it. i just want to see a script / example that 1. queries a mysql db, 2. displays the info onto a page with pagination 3. is down with ajax so the page doesnt change. i'm going to be displaying images so any jquery grids or table classes wont help.

Link to comment
Share on other sites

u can try it with prototype.js...here's an example .try it out...

main.php



<?
$con=mysql_connect("localhost","root","");
mysql_select_db("db",$con);

$sql=mysql_query("select fieldname,id from table1");

?><script type="text/javascript" src="prototype.js"></script>

<table><tr><td> <div id="display"></div>  <select  onChange="ajaxRequest('ajax.php',this.value,0)"name=""><? while($row=mysql_fetch_object($sql)){ ?><option value="<? echo $row->fieldname;?>"><? echo $row->fieldname;?></option><? }?></select></td></tr><tr><td></td></tr></table>

<script type="text/javascript">  
   /* ajax.Request */  
   function ajaxRequest(url,data,pagenumber) { 
  
      var aj = new Ajax.Request(  
     url, {  
      method:'post',   
      parameters:{"val": data,"pagenumber":pagenumber},   
     onComplete: getResponse  
      }  
   );  
}  
/* ajax.Response */  
function getResponse(oReq) {  
    $('display').innerHTML = oReq.responseText;  
}  
  </script> 



Link to comment
Share on other sites

ajax.php

<?

$VALUE=$_POST['val'];  




$con=mysql_connect("localhost","root","");
mysql_select_db("db",$con);
$total="select count(*) as count from table2 where fieldname='".$VALUE."'";
$totalcount=mysql_result(mysql_query($total),0);
$perpage=2;
$limitstart=$_POST['pagenumber'] * $perpage;
$limitend=$perpage;
$totalpage=ceil($totalcount/$perpage);
$sql=mysql_query("select * from table2 where fieldname='".$VALUE."' LIMIT ".$limitstart.",".$limitend);


?>
<table><? while($row=mysql_fetch_array($sql))
{?><tr><td><? echo $row["password"];?></td></tr><? }
?><tr><td><a href="javascript:ajaxRequest('ajax.php','<? echo $_POST['val']?>','<? echo $_POST['pagenumber']-1?>')">Previous</a></td><td>Page: <? echo $_POST['pagenumber']+1?>/<? echo $totalpage?></td><td><a href="javascript:ajaxRequest('ajax.php','<? echo $_POST['val']?>','<? echo $_POST['pagenumber']+1?>')">Next</a></td></tr></table>



 

just download prorotype.js from google and paste it in ur directory

 

 

 

 

 

 

Link to comment
Share on other sites

  • 2 weeks later...

I couldn't get that coding to work, do you have any other examples? pretty much what i'd like is a php query that with a limit of 5 set and the results are dumped into a div id=results and then everytime the user clicks next 5 more new results are added to the div id so its like a virtual ajax pagination without having actual pages

Link to comment
Share on other sites

  • 4 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.