如何增加阿贾克斯现有的PHP和放大器; MYSQL的分页 [英] How to add Ajax on existing PHP & Mysql based pagination

查看:148
本文介绍了如何增加阿贾克斯现有的PHP和放大器; MYSQL的分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我是一个新的程序员, 我有一个 PHP和放大器; MYSQL的分页,我想为添加Ajax 内容的功能。 我已经通过很多教程走了,但我没能找到任何它讲述有关添加AJAX到现有的分页他们都告诉使得基于Ajax的分页。

我希望用户能够如果JavaScript被关闭,以分页均匀。所以我想一些Ajax添加到我的code,这样我可以能够分页使用Ajax和PHP。

我可以使用 jQuery的.load()方式进行分页。

请看看我的code,并建议我怎么可以抓取页面URL的AJAX进行分页 我想这样的事情有工作。我无法弄清楚如何,请大家帮忙。 或者告诉我一些教程中,我可以借鉴。

jQuery的code

  $(文件)。就绪(函数(){

    $('#分页)。点击(函数(){
    $('PAGEURL')负载('是-test2.php #PaginationDiv');});
});
 

PHP和放大器;基于MySQL的分页

 < PHP
    require_once('_ LS-全球/ PHP / connection.php');
$分贝= mysql_select_db($数据库,$连接)或trigger_error(SQL,E_USER_ERROR);
$ SQL1 =SELECT COUNT(*)FROM $表;
$ RESULT1 =请求mysql_query($ SQL1,$连接)或trigger_error(SQL,E_USER_ERROR);
$行= mysql_fetch_row($ RESULT1);
$ = numRows行$行[0];
$ rowsperpage = 2;
$总页数= CEIL($ numRows行/ $ rowsperpage);
如果(使用isset($ _ GET ['页'])及和放大器; is_numeric($ _ GET ['页'])){
   $当前页=(INT)$ _GET ['页'];
} 其他 {
   $当前页= 1;
}
如果($当前页> $总页数){
   $当前页= $总页数;
}
如果(当前为$&小于1){
   $当前页= 1;
}
$抵消=($当前页 -  1)* $ rowsperpage;
$ SQL2 =SELECT * FROM internet_security ORDER BY ID DESC LIMIT $抵消,$ rowsperpage;
$结果2 =请求mysql_query($ SQL2,$连接)或trigger_error(SQL,E_USER_ERROR);
$名单= mysql_fetch_assoc($结果2);
$ STARTROW =($当前第-1)* $ rowsperpage;
 

code。在HTML

  H3>结果< PHP的echo($ STARTROW + 1)&GT?; - <?PHP的回声分($ STARTROW + $ rowsperpage,$行)GT;的< PHP的echo($总页数* $ rowsperpage)>< / H3>
< UL>< PHP
如果($当前页!= ​​$总页数){
回声<李>< A HREF ='{$ _ SERVER ['PHP_SELF']}页= $总页数?'> $总页数< / A>< /李>中;
$下一页= $当前页+ 1;
回声<李>< A HREF ='{$ _ SERVER ['PHP_SELF']}页= $下一页?>接下来&安培; RAQUO;&安培; RAQUO;< / A>< /李>中;
}>< / UL>



< UL>< PHP
如果($当前页< $总页数){
为($ X =($当前第 -  3); $ X≤(($当前第+ 3)+ 1); $ X ++){
如果(($ X大于0)及及($ X&其中; = $总页数)){
如果($ X = = $当前页){
回声<李ID ='pcurrent'>< A HREF ='{$ _ SERVER ['PHP_SELF']}页= $ X?> $ X< / A>< /李>中;
} 其他 {
回声<李>< A HREF ='{$ _ SERVER ['PHP_SELF']}页= $ X?> $ X< / A>< /李>中;
}}}
}
}
?> < / UL>


< UL>< PHP
如果(当前为$→1){
$prevpage = $当前页 -  1;
回声<李>< A HREF ='{$ _ SERVER ['PHP_SELF']}页= $prevpage?'>&安培; LAQUO;&安培; LAQUO; preV< / A>< /李>中;
回声&其中;丽>&所述; A HREF ='{$ _ SERVER ['PHP_SELF]}?页= 1'→1&其中; / a取代;&所述; /利>中;
}>< / UL>
 

解决方案

因为你让你的网页变量与GET方法PHP脚本,你可以通过像变量:

  $(文件)。就绪(函数(){
    $('#分页UL李一)。点击(函数(){
        即preventDefault();
        $('#divtoreplace)加载($(本).attr(HREF));
    });
});
 

Hi i am a new programmer, i have a PHP & Mysql based pagination, i want to add Ajax functionality to it. i have gone through many tutorials but i was not able to find any which tells about adding ajax onto existing pagination they all tell about making Ajax based pagination.

i want user be able to paginate even if javascript is turned off. so i want to add some Ajax to my code so that i can be able to paginate with Ajax and PHP.

i can use jquery .load() method to paginate.

please look at my code and suggest me how i can fetch page url for ajax to paginate i guess something like this has to work. i cant figure out how, please help. or tell me some tutorial i can learn from.

Jquery Code

    $(document).ready(function(){

    $('#pagination').click(function(){
    $('pageurl').load('is-test2.php #PaginationDiv');});        
}); 

PHP & MySQL Based Pagination

<?php
    require_once('_ls-global/php/connection.php'); 
$db = mysql_select_db($database,$connection) or trigger_error("SQL", E_USER_ERROR);
$sql1 = "SELECT COUNT(*) FROM $table";
$result1 = mysql_query($sql1, $connection) or trigger_error("SQL", E_USER_ERROR);
$row = mysql_fetch_row($result1);
$numrows = $row[0];
$rowsperpage = 2;
$totalpages = ceil($numrows / $rowsperpage);
if (isset($_GET['page']) && is_numeric($_GET['page'])) {
   $currentpage = (int) $_GET['page'];
} else {
   $currentpage = 1;
}
if ($currentpage > $totalpages) {
   $currentpage = $totalpages;
}
if ($currentpage < 1) {
   $currentpage = 1;
}
$offset = ($currentpage - 1) * $rowsperpage;
$sql2 = "SELECT * FROM internet_security ORDER BY id DESC LIMIT $offset, $rowsperpage";
$result2 = mysql_query($sql2, $connection) or trigger_error("SQL", E_USER_ERROR);
$list = mysql_fetch_assoc($result2);
$startrow = ($currentpage-1) * $rowsperpage;

Code in html

h3>Results <?php echo ($startrow+1) ?> - <?php echo min($startrow + $rowsperpage, $row) ?> of <?php echo ($totalpages *$rowsperpage) ?></h3>
<ul><?php 
if ($currentpage!=$totalpages) {
echo " <li><a href='{$_SERVER['PHP_SELF']}?page=$totalpages'>$totalpages</a></li> ";
$nextpage = $currentpage + 1;
echo " <li><a href='{$_SERVER['PHP_SELF']}?page=$nextpage'>Next&raquo;&raquo;</a></li> ";
}?></ul>



<ul><?php    
if($currentpage<$totalpages){
for ($x = ($currentpage - 3); $x < (($currentpage + 3) + 1); $x++) {
if (($x > 0) && ($x <= $totalpages)) {
if ($x == $currentpage) {
echo " <li id='pcurrent'><a href='{$_SERVER['PHP_SELF']}?page=$x'>$x</a></li>";
} else {
echo " <li><a href='{$_SERVER['PHP_SELF']}?page=$x'>$x</a></li> ";
}}}  
}
}
?> </ul>


<ul><?php
if ($currentpage > 1){
$prevpage = $currentpage - 1;
echo " <li><a href='{$_SERVER['PHP_SELF']}?page=$prevpage'>&laquo;&laquo;Prev</a></li> ";
echo "<li><a href='{$_SERVER['PHP_SELF']}?page=1'>1</a></li> ";
}?></ul>

解决方案

since you are getting your page variable into the PHP script with GET method, you can pass the variable like :

$(document).ready(function(){
    $('#pagination ul li a').click(function(){
        e.preventDefault();
        $('#divtoreplace').load($(this).attr("href"));            
    });        
}); 

这篇关于如何增加阿贾克斯现有的PHP和放大器; MYSQL的分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆