hey.
im back.
so im working on this PHP project, and on one page, i need to print out all record in a MySQL table. The thing is, i only want to have a limited number of records per page, as a variable.
say 10 for example.
how do i retrieve the records, but only print out 10 on each page?
(its a PM system…)
so far i have this:
<?php
include("session.php");
$page = $_GET['page'];
if($page==""){
$page='1';
}
$nextpage = $page + 1;
$prevpage = $page - 1;
$action = $_POST['action'];
mysql_connect ("localhost", "XXXXXX", "XXXXXX") or die ('Cannot connect to the database because: ' . mysql_error());
mysql_select_db ("XXXXXX");
$getuser = mysql_query("SELECT * FROM contacts WHERE user = '$user_info[0]'");
$userdetails = mysql_fetch_array($getuser);
$stat_id = $userdetails['stat_id'];
$getmail = mysql_query("SELECT * FROM `pms` WHERE `to` = '$stat_id' ORDER BY `timestamp` ASC");
?>
//break for html
//further down the page:
<?php
$numofrows = mysql_num_rows($getmail);
$maxperpage = 10;
if($numofrows > $maxperpage){
$use = $maxperpage;
}elseif($numofrows <= $maxperpage){
$use = $numofrows;
}
$lastpage = ceil($numofrows/$maxperpage);
if($page==1){
$i=0;
}else{
$i=($page*$maxperpage)-($maxperpage-1);
}
if($numofrows>=1){
echo 'You have: <strong>'.$numofrows.'</strong> messages';
echo '<br />';
echo'page: <strong>'.$page.'</strong> / <strong>'.$lastpage.'</strong>';
echo '<br>start row: '.$i;
echo'<table width="75%" border="0" cellspacing="5" cellpadding="0" align="center">';
echo'<tr>';
echo'<td width="50%" style="border: 1px #666666 solid;"><h3><div align="center">From:</div></h3></td>';
echo'<td width="50%" style="border: 1px #666666 solid;"><h3><div align="center">Sent:</div></h3></td>';
echo'</tr>';
echo'</table>';
echo'<br>';
}else{
echo'You have no mail.';
}
for ($i; $i < $use; $i++){
echo'<table width="75%" style="border: 1px #666666 solid;" cellspacing="5" cellpadding="0" align="center">';
echo '<tr>';
echo '<td>';
echo '<a href="stats.php?id=' . mysql_result( $getmail,$i,'sender_id') . '"><center>' . mysql_result( $getmail,$i,'from') . '</center></a></td>';
echo '<td><center>'. mysql_result( $getmail,$i,'sent') .'</center></td>';
echo '</tr>';
echo'<tr>';
echo'<td colspan="2" align="center"><hr width="100%"></td>';
echo'</tr>';
echo '<tr>';
echo '<td colspan="2" bgcolor="#333333" style="border: 1px #666666 solid;"><center><br>'. mysql_result( $getmail,$i,'message') .'<br><br></center></td>';
echo '</tr>';
echo '<tr>';
echo '<td width="50%" align="right">';
?>
<form name="Reply" method="post" action="newmail.php">
<input name="id" type="hidden" value="<?php echo $mail['sender_id']; ?>">
<input name="action" type="submit" class="general" id="action" value="Reply">
</form>
<?php echo '</td><td width="50%" align="left">'; ?>
<form name="Reply" method="post" action="inbox.php">
<input name="id" type="hidden" value="<?php echo $mail['sender_id']; ?>">
<input name="action" type="submit" class="general" id="action" value="Delete">
</form>
<?php
echo'</td>';
echo'</tr>';
echo '</table>';
echo'<br>';
}
echo'<table width="75%" border="0" cellspacing="5" cellpadding="0">';
echo'<tr>';
if($page!=1){
echo'<td><p align="left"><a href="inbox.php?page='.$prevpage.'">Previous</a></p></td>';
}
if($page!=$lastpage){
echo'<td><p align="right"><a href="inbox.php?page='.$nextpage.'">Next</a></p></td>';
}
echo'</tr>';
echo'</table>';
?>
the “if($numofrows > $maxperpage){” determines how many records there are. if there are less than the maximum allowed for each page, then print all of them. if not, then print the maximum.
$i is the row (record in the table) to start printing out from.
the for statement is supposed to get the start row, and print out records up the maximum number allowed for each page.
it does restrict to the correct amount per page, but when you view page 2, the records that should be there arent.
anyone any ideas why?
thanks, Decfor