Reputation: 61
I am going to create a pagination then
$num_row = mysqli_num_rows($res);
total I have 240 rows then:
if($num_row>5){
$count=0;
$index=1;
while($count < $num_row && $index < 5){ ?>
<button id="<?php echo $count; ?>" class="index_b"><?php echo $index; ?></button>
<?php
$count = $count + 5;
$index++;
}
}
I want first 1 2 3 4 5 then next button so I tried
<div class="next_index_b">
<?php if($num_row > 50){?>
<button class="next_membd_index_button" name="50" ><img src="images/arrow.png" /></button>
<?php } ?>
Now my issue is after 4th pagination number, no number is working.
Upvotes: 0
Views: 225
Reputation: 1308
while($count < $num_row && $index < 5){ ?>
This condition means that when $index equals 5, the loop is stopped
Therefore, the button with 5 will never be shown.
Try using less or equal than instead of less than
if($num_row>5){
$count=0;
$index=1;
while($count < $num_row && $index <= 5){ ?>
<button id="<?php echo $count; ?>" class="index_b"><?php echo $index; ?></button>
<?php
$count = $count + 5;
$index++;
}
}
Or even better, switch to @FrankerZ's solution
Upvotes: 0
Reputation: 22911
Don't use mysqli_num_rows()
! Ideally, you should pull ONLY the data that you're looking for, and then run 2 separate queries. One to fetch the full count, and one to fetch the items per page:
$db = new PDO(...);
$page = isset($_GET['page']) ? (int) $_GET['page'] : 1;
$offset = ($page-1) * 50;
$count = $db->query("SELECT COUNT(*) FROM table")->fetchColumn();
$trans = $db->prepare("SELECT * FROM table LIMIT :offset, 50");
$trans->bindValue(':offset', $offset, PDO::PARAM_INT);
$trans->execute();
while ($row = $trans->fetchRow()) {
// echo data
}
//Add footer here. Use $page variable and $count to determine how many pages are available.
Upvotes: 1