Jump to content

[SOLVED] Pagination with emails


nuttycoder

Recommended Posts

Hi,

 

am building an email program web based using the imap functions am wondering is it possible to limit how many emails their is per page?

I have looked at the imap functions but cannot find any mention of limiting the list.

 

I could do this if I stored all the emails in a database but I'd rather just grab the emails straight from the server and then show say 30 per page.

 

here my code to print out all the emails:

 

<table class="stripeMe">
<form action="<?=$_SERVER['PHP_SELF'];?>" method="post">
<tr>
<th> </th>
<th><a href="index.php?order=subject"><b>Subject</b></a></th>
    <th><a href="index.php?order=from"><b>From</b></a></th>
    <th><a href="index.php?order=date"><b>Date</b></a></th>	
<th><a href="index.php?order=size"><b>Size</b></a></th>
</tr>



<?php

$orderReg = $_GET['order'];

switch ($orderReg) {
case "date":
    $orderby = SORTARRIVAL;
    break;
case "from":
     $orderby = SORTFROM;
    break;
case "subject":
     $orderby = SORTSUBJECT;
    break;
case "size":
     $orderby = SORTSIZE;
    break;
default:
    $orderby = SORTARRIVAL;
}


// sort the messages by subject
$sorted = imap_sort($mbox, $orderby, 1);

if($total == 0)
{
echo "<tr><td>No Emails in {$_SESSION['folder']}</td></tr>";
}

// display again
for ($i=0; $i<count($sorted); $i++) {
   $header = imap_header($mbox, $sorted[$i]);


?>
<tr>
<td align="right" valign="top"><input type="checkbox" name="del[]" value="<?php echo trim($header->Msgno); ?>"></td>
<td valign="top"><a href="<?php echo $dir;?>view-<?php echo trim($header->Msgno);?>">


<?php if(trim($header->Subject) !== ''){ 

if($header->Unseen == 'U')
{ 
echo "<b>$header->Subject</b>";
} 
else { echo $header->Subject; }} 
else { echo "No Subject"; 
}
?>
</a></td>
<td valign="top"><?php echo $header->fromaddress;?></td>  
<td valign="top"><?php echo date('d/m/Y h:m:s',strtotime($header->date));?></td>
<td valign="top"><?php echo ceil(($header->Size /1024)), " KB";?></td>
</tr>
<?php } ?>
</form>

 

any help would be great cheers.

Link to comment
https://forums.phpfreaks.com/topic/124376-solved-pagination-with-emails/
Share on other sites

What about looping and each run through of the loop returns one email?

 

for($i = 0; $i < 30; $i ++) {
    // code to return one email.
}

 

Then you would need something like an offset:

 

<a href="email.php?offset=30">Next Page</a>

 

Then you could use this offset variable to choose where to start from in the list of emails.

Well say you have a text file with all the emails on a single line each you can do the following:

 

$emails = file("emails.txt"); //load all the emails into an array

$limit = 1; //limit to 30 per page

//Get the current page
if(!isset($_GET['page'])){
$page = 1;
}else{
$page = $_GET['page'];
}

//Find out how many results there are
$totalResults = count($emails);

//Get the total pages
$totalPages = ceil($totalResults / $limit);

//If the page you are on is bigger then the totalPages then set the page to the totalPages
if($page > $totalPages){
$page = $totalPages;
}

//Find out where to start looping from
$from = ($page * $limit) - $limit;

//Create a temp array
$tmp_emails = array();

//Start looping
for($i = $from; $i < $from + ceil($totalResults / $totalPages); $i++){
$tmp_emails[] = $emails[$i];
}

//Implode the temp array, seperating the elements.
echo implode(", ", $tmp_emails);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.