Reputation: 1663
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">\r';
$pstrout .= '<tr class="f5">\r';
$pstrout .= '<td align="center" class="npagecounter" style="height:20px;color:#0000FF">\r';
if ($pg >= 2){
$pstrout .= '<a href="pg=1" id="Page1Link">« First</a> \r';
}
if (pg >=2){
$pstrout .= '<a href="pg='.($pg-1).'" id="Page'.($pg- 1).'Link">« Previous</a> \r';
}
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> ';
}
}
}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> ';
}
}
}
}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> ';
}
}
}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> ';
}
}
}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> ';
}
}
}
}
}else{
for ($ipages=1;$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> ';
}
}
}
if ($max_pg > 1){
if ($pg <> $max_pg){
$pstrout .= '<a href="?pg='.($pg+1).'" id="Page'.($pg+1).'Link">Next »</a> ';
}
}
if ($max_pg > 1){
if ($pg <> $max_pg){
$pstrout .= '<a href="?pg='.$max_pg.'" id="Page'.$max_pg.'Link">Last »</a>';
}
}
$pstrout .= '</td>\r';
$pstrout .= '</tr>\r';
$pstrout .= '<tr>\r';
$pstrout .= '</table>\r';
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!
Upvotes: 1
Views: 256
Reputation: 1819
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
Upvotes: 1
Reputation: 953
i added some echo to your code to run a quick test :
if ($pg == 1){
for ($ipages=$pg;$ipages=($pg+4);$ipages++){
echo "case 0 ".$ipages." ... ";
[....]
}
} else if ($pg == 2){
for ($ipages=($pg-1);$ipages=($pg+3);$ipages++){
echo "case 1 ".$ipages." ... ";
[...]
}
} else {
for ($ipages=($pg-2);$ipages=($pg+2);$ipages++){
echo "case 2 ".$ipages." ... ";
[...]
}
}
i got the following result :
case 1 5 ... case 1 5 ... case 1 5 ... case 1 5 ... case 1 5 ... case 1 5 ...
You should replace your operators as others mentioned
Upvotes: 1
Reputation: 406
second loop
}else{
for ($ipages=$pg;HERE --> $ipages=$max_pg <---HERE;$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> ';
}
}
}
Upvotes: 1
Reputation: 42736
you have wrong use of comparison operators, several times, you are using assignment operations
for ($ipages=$pg-$npage_count_diff2;$ipages=$max_pg;$ipages++)
$ipages=$max_pg;
should be $ipages==$max_pg;
would probably be better with
$ipages<=$max_pg;
as with just == it only stops if it hits $max_pg, there might be an instance where it skips over $max_pg and never hits it.
Upvotes: 1