doukui4786 2012-05-26 03:00
浏览 62
已采纳

添加AJAX时分页不起作用

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&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>

This pagination is working fine.

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.

I tried this:

$(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?

  • 写回答

2条回答 默认 最新

  • doumu6941 2012-05-26 03:33
    关注

    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:

    1. Put the code that generates your list into a function such as generateList($page)
    2. In your php page, output the results of the above created function as your initial view.
    3. Create an ajax_actions.php page that can call the function you created in step 1 and return the result.
    4. 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.

    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.

    Hope that helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料