0

I'm trying to make some kind of pagination system.

Each page has a maximum of 4 elements that came from a DB.

Each page is surronded by a div (div id='p1' class='pagedemo _current'). So I have the following:

$i=0;
$pag=0;
$arr = array();

while($rowNews = mysql_fetch_array($rsNews)){
    $i++;
    $arr[$i] = $rowNews;

    if($i%4==1){
       echo "div id='p1' class='pagedemo _current'"
     }

  ...show content...

    if($i%4 ==0 ){
       echo"</div>"; //close the tag of class="pagedemo"
    }

}//end of while

This open a div when the i is 1; 5 ; 9.... and closes when is multiple of 4 (4; 8; 12...) But I also want to close the div when $i its the last number, ie: If there's only 6 results I want to close the div after the 6th element.

I'm not accomplish it

Any ideas??

4 Answers 4

2

just check $i after your loop and close if i%4 != 0 (means is not closed yet)

$i=0;
$pag=0;
$arr = array();

while($rowNews = mysql_fetch_array($rsNews)){
    $i++;
    $arr[$i] = $rowNews;

    if($i%4==1){
       echo "div id='p1' class='pagedemo _current'"
     }

  ...show content...

    if($i%4 ==0 ){
       echo"</div>"; //close the tag of class="pagedemo"
    }

}//end of while
if ($i%4 !=0) {
echo"</div>"; //close the tag of class="pagedemo"
}
Sign up to request clarification or add additional context in comments.

Comments

1

You need to be able to count how many rows are in your mysql result. Then compare it with your iterator.

$i=0;
$pag=0;
$arr = array();

$total = mysql_num_rows($rsNews);
while($rowNews = mysql_fetch_array($rsNews)){
    $i++;
    $arr[$i] = $rowNews;

    if($i%4==1){
       echo "div id='p1' class='pagedemo _current'"
     }

  ...show content...

    if($i%4 ==0 || $i == $total){
       echo"</div>"; //close the tag of class="pagedemo"
    }

}//end of while

3 Comments

care for $i == $total, that is only because you do a $i++ at the beginning of the loop. If you use a foreach or so later it will be $i == $total-1. By the way I suggest you heavily to remove the $i++ from the beginning,and update your code to reflect this change. indexes are counted from 0 not 1! If you leave it its kinda misleading for someone else.
Ah, I agree. But in his case he wanted his rows to start with 1, instead of 0. If I did start with 0, then I would change the $i%4==0 to ($i+1)%4==0.
ace: actually the comment was for user794035, I though I should advice him how to do it right. I think its bad style to do it so.
0

Set a variable to detect if the close is needed and check at the end:

while($rowNews = mysql_fetch_array($rsNews)){
    $i++;
    $arr[$i] = $rowNews;

    if($i%4==1){
       $close = true;
       echo "div id='p1' class='pagedemo _current'"
     }

  ...show content...

    if($i%4 ==0 ){
       $close = false;
       echo"</div>"; //close the tag of class="pagedemo"
    }

}

if($close)
echo"</div>";

Comments

0

Add this after your while:

if ($i%4 != 0) {
  echo"</div>";
}

EDIT: should be like @evildead

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.