Reputation: 885
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??
Upvotes: 0
Views: 1464
Reputation: 2369
Add this after your while:
if ($i%4 != 0) {
echo"</div>";
}
EDIT: should be like @evildead
Upvotes: 0
Reputation: 7583
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
Upvotes: 1
Reputation: 4757
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"
}
Upvotes: 2
Reputation: 5947
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>";
Upvotes: 0