[php] Printing records with multipages?

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

anyone?

if you need more information, or you want me to explain something, please post and tell me, and i will

decfor

i don’t have time to write it all out now, but use LIMIT.

OK, i tried to use LIMIT, but it simply tells me i have 10 messages, and wont let me read the others.

how do i make it so that it only Shows 10, acknowledges that i have more tha that?

thanx, decfor

If I’m right… it’s like this:


$getmail = mysql_query("SELECT * FROM `pms` WHERE `to` = '$stat_id' ORDER BY `timestamp` ASC LIMIT 0, 10");

But then if it’s page 2, it would be LIMIT 11, 20… (I think)… I’m not sure about this!

ok, but that means id have to code out each page. could i not do it like this:


$maxperpage = 10;
$startrow=($page*$maxperpage)-($maxperpage-1);
$endrow = $i=((page*$maxperpage)-($maxperpage-1)+9)
$getmail = mysql_query("SELECT * FROM `pms` WHERE `to` = '$stat_id' ORDER BY `timestamp` ASC LIMIT ".$startrow.", ".$endrow);

you need 2 separate queries:

  1. [font=monospace]SELECT COUNT(*) FROM pms[/font]
  2. select using LIMIT to get the ones you want.

[quote=bwh2;1984647]you need 2 separate queries:

  1. [FONT=monospace]SELECT COUNT(*) FROM pms[/FONT]
  2. select using LIMIT to get the ones you want.[/quote]

i dont understand.

can you give an example?

I don’t get what bwh2 is saying… but instead of doin that query … do this:


$getmail = mysql_query("SELECT * FROM pms WHERE to = '$stat_id' ORDER BY timestamp ASC LIMIT {$startrow}, {$endrow}"); 

You can insert variables in ""s using {}'s. :stuck_out_tongue:

[quote=CriTiCeRz;1984653]I don’t get what bwh2 is saying… but instead of doin that query … do this:


$getmail = mysql_query("SELECT * FROM pms WHERE to = '$stat_id' ORDER BY timestamp ASC LIMIT {$startrow}, {$endrow}"); 

You can insert variables in ""s using {}'s. :P[/quote]

thanks

(thanks anyways bwh2 :))

So did it work?

im working on it now (i used a for statement, so i have to rethink it :P)

[QUOTE=bwh2;1984647]you need 2 separate queries:

  1. [font=monospace]SELECT COUNT(*) FROM pms[/font]

  2. select using LIMIT to get the ones you want.[/QUOTE]
    ^ i was responding to: “how do i make it so that it only Shows 10, acknowledges that i have more tha that?”

  3. shows you how many total you have

  4. selects only the 10

i don’t have much time now. i’ll try to respond later with a more thorough answer.

ok. thanks :slight_smile:

[quote=CriTiCeRz;1984602]If I’m right… it’s like this:


$getmail = mysql_query("SELECT * FROM `pms` WHERE `to` = '$stat_id' ORDER BY `timestamp` ASC LIMIT 0, 10");

But then if it’s page 2, it would be LIMIT 11, 20… (I think)… I’m not sure about this![/quote]

The syntax for SQL’s LIMIT Command is

LIMIT start, max

so you could have LIMIT 10, 20. LIMIT 20, 20 etc

Just have a variable in your url, something like www.mypage.com/inbox.php&show=10

then

$start = intval( $_GET[‘show’] );

LIMIT $start, 20