Reputation: 39
how i can paginating unlimited data from My sql , if i want to use the limit , i must know the number of rows, but i don't know the number of rows.
this is the code for getting data from the database :
<?php
$accid = $_SESSION['accid'];
set_include_path( get_include_path() . PATH_SEPARATOR . $_SERVER['DOCUMENT_ROOT'] . "bulk2/" );
include("/config.php");
$con = mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
mysql_query("SET CHARACTER SET utf8");
$res=mysql_query("SELECT * FROM `sent_msgs_history` WHERE `accid` = $accid" ***LIMIT 0,5*** );
if(mysql_num_rows($res)> 0)
{
for($i=0;$i<mysql_num_rows($res);$i++) {
$row=mysql_fetch_assoc($res);
//$de_name = decrypt($key,$iv,$row['rec_name']);
//$de_mobile = decrypt($key,$iv,$row['mobile_number']);
$de_name = $row['senderName'];
$de_length = $row['MessageLength'];
$de_count = $row['MessageCount'];
$de_date = $row['sent_on'];
$de_sender = $row['sent_by'];
?>
<tr>
<td> <?php echo $de_name ;?> </td>
<td> <?php echo $de_length;?> </td>
<td> <?php echo $de_count;?> </td>
<td> <?php echo $de_sender;?> </td>
<td> <?php echo $de_date;?> </td>
</tr>
<?php }?>
</table>
<br>
<br><br> <br>
<?php
}?>
i don't know the number of row , how can i do that ????? please help me >>>
Upvotes: 0
Views: 603
Reputation: 488
Use a loop with your desired limit range and then use this on the end of query. as (simple pagination)
$limit='';
$i=0;
$q=mysql_query(...)
while($i<=mysql_num_rows($q))
{
$b=$i+5;
$limit='limit '.$i.', '.$b;
mysql_query(...$limit)
}
Upvotes: 0
Reputation: 3939
Similar / same question - MySQL skip first 10 results
Or to get all results after an offset - MySQL skip first 10 results
The LIMIT just needs to be from what ever items_per_page
to the items_per_page * last_index
So something like:
SELECT * FROM sent_msgs_history LIMIT items_per_page, items_per_page * lastindex
So when getting 40 items per page:
page 1 would be LIMIT 40
page 2 would be LIMIT 40, 40
page 3 would be LIMIT 40, 80
Upvotes: 1
Reputation: 43557
What you mean unlimited pagination?
If you wish to know rows count, SELECT COUNT(*) FROM sent_msgs_history
OR
Don't use limit, when user selects "unlimited pagination"
Upvotes: 0