添加AJAX时分页不起作用 [英] Pagination not working when adding AJAX
问题描述
我有一个基于PHP的分页系统,并且工作正常,我使用GET参数来传递页码:
I have a PHP-based pagination system and it is working fine, I'm using GET parameters to pass the page number:
<?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;
?>
我显示链接的方式是这样的:
and the way I show links is this:
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»»</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'>««Prev</a></li> ";
echo "<li><a href='{$_SERVER['PHP_SELF']}?page=1'>1</a></li> ";
}?></ul>
此分页工作正常.
我的问题是,我现在想向其添加AJAX功能,这样我就应该在分页中同时具有这两种功能,即,如果关闭JavaScript,则该分页将在PHP中工作.
My Question is I now want to add AJAX functionallity to it so that i shoud have both functionality in pagination i.e if JavaScript is turned off, the pagination will work in PHP.
我尝试过:
$(function() {
$('#pagination ul li a, .temp').click(function(ev) {
ev.preventDefault();
$('#temporary').load($(this).attr('href')).modal();
});
});
但是现在,分页不起作用,单击分页链接时什么也没有发生.怎么了?
But now, the pagination isn't working and nothing is happening when the pagination link is clicked. What's wrong?
推荐答案
似乎您的要求有些混乱.据我所知,您希望同时具有PHP分页(禁用js时)和AJAX分页以利用动态加载(启用js时).
Seems like there is some confusion about what you are asking. As far as I can gather, you want to have both PHP pagination (when js is disabled) and AJAX pagination to take advantage of dynamic loading (when js is enabled).
为此,您应该执行以下操作:
In order to do that, you should do the following:
- 将生成列表的代码放入诸如
generateList($page)
之类的函数中
- 在您的php页面中,将上述创建的函数的结果输出为您的初始视图.
- 创建一个ajax_actions.php页面,该页面可以调用您在步骤1中创建的函数并返回结果.
- 使用链接单击处理程序来调用(通过AJAX)ajax_actions.php页面并显示结果.您可能需要从链接点击中解析页面ID并将其传递.
- Put the code that generates your list into a function such as
generateList($page)
- In your php page, output the results of the above created function as your initial view.
- Create an ajax_actions.php page that can call the function you created in step 1 and return the result.
- Use the link click hander to call (via AJAX) the ajax_actions.php page and display the result. You will probably need to parse the page id from your link click and pass it along.
如果您执行这些操作,则在禁用JS时,PHP将处理分页.启用JS后,您将执行ev.preventDefault()并使用AJAX显示内容.
If you do these things, when JS is disabled, PHP will handle the pagination. When JS is enabled, you will do ev.preventDefault() and use AJAX to display the content.
希望有帮助.
这篇关于添加AJAX时分页不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!