doulu1325 2013-07-24 20:29
浏览 88
已采纳

PHP中的分页和无限循环

I am trying to convert an old vbscript function into a php function and run I run it, it appears to be stuck in a infinite loop. Here is the script:

<?php
function pagination($str,$max_pg){
    $pg = (int)$str;
    $max_pg = (int)$max_pg;
    if($max_pg <= 1){
        exit();
    }

    $pstrout = '<table cellpadding="0" cellspacing="0" border="0">';
    $pstrout .= '<tr class="f5">';
    $pstrout .= '<td align="center" class="npagecounter" style="height:20px;color:#0000FF">'; 

    if ($pg >= 2){
        $pstrout .= '<a href="pg=1" id="Page1Link">&laquo;&nbsp;First</a>&nbsp;';
    }

    if (pg >=2){
        $pstrout .= '<a href="pg='.($pg-1).'" id="Page'.($pg-    1).'Link">&laquo;&nbsp;Previous</a>&nbsp;';
    }

    if (max_pg > 5){
        $npage_t = $pg + 4;
        $npage_count_diff = $max_pg - $pg;
        if ($npage_count_diff == 0){
            $npage_count_diff2 = 4;
        }elseif ($npage_count_diff == 1){
            $npage_count_diff2 = 3;
        }elseif ($npage_count_diff == 2){
            $npage_count_diff2 = 2;
        }else{
            $npage_count_diff2 = 1;
        }
        if ($npage_t > $max_pg){
            if ($npage_count_diff <= 3){
                for ($ipages=$pg-$npage_count_diff2;$ipages=$max_pg;$ipages++){
                    if($ipages == $pg){
                        $pstrout .= '<span style="padding:2px 6px" class="fb">'.$ipages.'</span>';
                    }else{
                        $pstrout .= '<a href="?pg='.$ipages.'" id="Page'.$ipages.'Link">'.$ipages.'</a>&nbsp;';
                    }
                }
            }else{
                for ($ipages=$pg;$ipages=$max_pg;$ipages++){
                    if ($ipages == $pg){
                        $pstrout .= '<span style="padding:2px 6px" class="fb">'.$ipages.'</span>';
                    }else{
                        $pstrout .= '<a href="?pg='.$ipages.'" id="Page'.$ipages.'Link">'.$ipages.'</a>&nbsp;';
                    }
                }
            }
        }else{
            if ($pg == 1){
                for ($ipages=$pg;$ipages=($pg+4);$ipages++){
                    if ($ipages == $pg){
                        $pstrout .= '<span style="padding:2px 6px" class="fb">'.$ipages.'</span>';
                    }else{
                        $pstrout .= '<a href="?pg='.$ipages.'" id="Page'.$ipages.'Link">'.$ipages.'</a>&nbsp;';
                    }
                }
            }elseif ($pg == 2){
                for ($ipages=($pg-1);$ipages=($pg+3);$ipages++){
                    if ($ipages == $pg){
                        $pstrout .= '<span style="padding:2px 6px" class="fb">'.$ipages.'</span>';
                    }else{
                        $pstrout .= '<a href="?pg='.$ipages.'" id="Page'.$ipages.'Link">'.$ipages.'</a>&nbsp;';
                    }
                }
            }else{
                for ($ipages=($pg-2);$ipages=($pg+2);$ipages++){
                    if ($ipages == $pg){
                        $pstrout .= '<span style="padding:2px 6px" class="fb">'.$ipages.'</span>';
                    }else{
                        $pstrout .= '<a href="?pg='.$ipages.'" id="Page'.$ipages.'Link">'.$ipages.'</a>&nbsp;';
                    }
                }
            }
        }
    }else{
        for ($ipages=1;$ipages=$max_pg;$ipages++){
            if ($ipages == $pg){
                $pstrout .= '<span style="padding:2px 6px" class="fb">'.$ipages.'</span>&nbsp;';
            }else{
                $pstrout .= '<a href="?pg='.$ipages.'" id="Page'.$ipages.'Link">'.$ipages.'</a>&nbsp;';
            }
        }
    }
    if ($max_pg > 1){
        if ($pg <> $max_pg){
            $pstrout .= '<a href="?pg='.($pg+1).'" id="Page'.($pg+1).'Link">Next&nbsp;&raquo;</a>&nbsp;';
        }
    }
    if ($max_pg > 1){
        if ($pg <> $max_pg){
            $pstrout .= '<a href="?pg='.$max_pg.'" id="Page'.$max_pg.'Link">Last&nbsp;&raquo;</a>';
        }
    }

    $pstrout .= '</td>';
    $pstrout .= '</tr>';
    $pstrout .= '<tr>';
    $pstrout .= '</table>'; 
    return $pstrout;
}

$current_page = 2;
$total_page = 24;
echo pagination($current_page,$total_page);
?>

I probably have one bracket out of place. Please can you help me resolve this issue, with the infinite loop.

Many thanks!

  • 写回答

4条回答 默认 最新

  • douzhenao6515 2013-07-24 20:42
    关注

    your for loops are wrong.

    Change this:

    for ($ipages=$pg;$ipages=$max_pg;$ipages++){
    

    to this:

    for ($ipages=$pg;$ipages<=$max_pg;$ipages++){
    

    you need to do this for all the for loops and also makes changes according to comments to your question. There might be other errors in the code too but this is a good place to start.

    I made some other changes to your code and added it to PHP Fiddle here: http://phpfiddle.org/api/raw/qdt-r7t The logic still does not seem to be working correctly but that page should be a good starting point.

    Also, questions like this are better suited for the Code Review site: https://codereview.stackexchange.com/?as=1

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

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line