note 73891 added to function.imap-fetch-overview
| From: | raja at aonic dot net | Date: | Wed, 14 Mar 2007 19:22:57 +0000 |
| Subject: | note 73891 added to function.imap-fetch-overview | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-124448@lists.php.net to get a copy of this message | ||
Here is a function to get messages from IMAP and sort them for pagination.
<?php
/**
* Return array of IMAP messages for pagination
*
* @param int $page page number to get
* @param int $per_page number of results per page
* @param array $sort array('subject', 'asc') etc
*
* @return mixed array containing imap_fetch_overview, pages, and total rows if successful,
false if an error occurred
* @author Raja K
*/
public function listMessages($page = 1, $per_page = 25, $sort = null) {
$limit = ($per_page * $page);
$start = ($limit - $per_page) + 1;
$start = ($start < 1) ? 1 : $start;
$limit = (($limit - $start) != ($per_page-1)) ? ($start + ($per_page-1)) : $limit;
$info = imap_check($this->_imap_stream);
$limit = ($info->Nmsgs < $limit) ? $info->Nmsgs : $limit;
if(true === is_array($sort)) {
$sorting = array(
'direction' => array( 'asc' => 0,
'desc' => 1),
'by' => array( 'date' => SORTDATE,
'arrival' => SORTARRIVAL,
'from' => SORTFROM,
'subject' => SORTSUBJECT,
'size' => SORTSIZE));
$by = (true === is_int($by = $sorting['by'][$sort[0]]))
? $by
: $sorting['by']['date'];
$direction = (true === is_int($direction = $sorting['direction'][$sort[1]]))
? $direction
: $sorting['direction']['desc'];
$sorted = imap_sort($this->_imap_stream, $by, $direction);
$msgs = array_chunk($sorted, $per_page);
$msgs = $msgs[$page-1];
}
else
$msgs = range($start, $limit); //just to keep it consistent
$result = imap_fetch_overview($this->_imap_stream, implode($msgs, ','), 0);
if(false === is_array($result)) return false;
//sorting!
if(true === is_array($sorted)) {
$tmp_result = array();
foreach($result as $r)
$tmp_result[$r->msgno] = $r;
$result = array();
foreach($msgs as $msgno) {
$result[] = $tmp_result[$msgno];
}
}
$return = array('res' => $result,
'start' => $start,
'limit' => $limit,
'sorting' => array('by' => $sort[0],
'direction' => $sort[1]),
'total' => imap_num_msg($this->_imap_stream));
$return['pages'] = ceil($return['total'] / $per_page);
return $return;
}
?>
----
Server IP: 216.194.113.175
Probable Submitter: 69.176.213.114
----
Manual Page -- http://www.php.net/manual/en/function.imap-fetch-overview.php
Edit -- https://master.php.net/note/edit/73891
Del: integrated -- https://master.php.net/note/delete/73891/integrated
Del: useless -- https://master.php.net/note/delete/73891/useless
Del: bad code -- https://master.php.net/note/delete/73891/bad+code
Del: spam -- https://master.php.net/note/delete/73891/spam
Del: non-english -- https://master.php.net/note/delete/73891/non-english
Del: in docs -- https://master.php.net/note/delete/73891/in+docs
Del: other reasons-- https://master.php.net/note/delete/73891
Reject -- https://master.php.net/note/reject/73891
Search -- https://master.php.net/manage/user-notes.php