Posted by Simon Goodchild at February 5th, 2012
Final part (doubtful, but for now….!) adds search to the mail page, follows on from part 6.
I’m going to focus on the lines of code that matter here, how you use them is entirely down to your imagination! This is using wps_mail_page_7.php.
The most important parts are the additional parameters to $wps_mail->get_inbox() and $wps_mail->get_inbox_count() that filters the returns list/value by a search term, but on that in a bit.
But first the search box. This doesn’t use a WPS UI element as you can make this however you want using HTML, I’m going to create a straightforward text box and button, lines 160-168 of wps_mail_page_7.php.
![]()
Mostly straightforward, except I include a hidden field “action” so I can retain the view state of the mail box (in or sent). I also default the value to anything previously search for ($term).
Getting the search term
Where do I get $term from? That’ll be line 93 that looks for a posted search term. It checks for the term passed by the URL or the post form (URL comes in for the paging, cover that in a minute).
$term = isset($_GET['my-search-term']) || isset($_POST['my-search-term']) ? isset($_POST['my-search-term']) ? $_POST['my-search-term'] : $_GET['my-search-term'] : '';
A check is made if either the URL of the form POST data has a search value (matching the search box name I used in the form), and if so takes the form POST data if available, or if not the URL parameter. If neither is set, $term is set to ” (an empty string).
I’m using a more succinct method of checking values using ternary operators, you could use a set of if {} else {} conditions over several lines:
if ( isset($_GET['my-search-term']) || isset($_POST['my-search-term']) ) {
if ( isset($_POST['my-search-term']) ) {
$term = $_POST['my-search-term'];
} else {
$term = $_GET['my-search-term'];
}
} else {
$term = '';
}
So $term will hold the search term.
Getting mail based on the search term
We use one of the parameters for $wps_mail->get_inbox() that will filter the results based on a search term, line 229. The results will only include mail where display name, subject or mail matches $term:
$inbox = $wps_mail->get_inbox($page_length, $start, 40, $term, true, 75, false, $show_sent_mail);
Notice the fourth parameter? That’s our search term. The results will now be filtered, and if anything matches exactly, it will be highlighted.

Coping with paging
If the results go over one page, we need to be able to move page by page and return the search term, so we add a parameter to the page links, that gets picked up online 93.
Line 274 creates the string to be added to the URL, or an empty string if no search term is available. It’s then added to all the paging links, for example on line 278:
if ($current_page > 1) echo '<a href="'.$wps->get_mail_url().'?action='.$paging_action.'&start=0'.$term_parameter.'">First</a> ';
This is not the only way to do it, but as a simple example it allows us to retain the search term as we move from page to page in the search results.
Search count
It’s quite nice to show the number of search results. If too large, you could avoid the search and prompt for a more precise search, just an idea!
We’ve added a parameter to $wps_mail->get_inbox_count() to filter the count of the mail by the search term. If there is no search term, the count will still just be the total number of mail messages in the inbox/sent list.
$inbox_count = $wps_mail->get_inbox_count(false, $show_sent_mail, $term);
Lines 293-299 display either the number of returned search matches, or if no search was made, just the number of mail items in the mail box:
![]()
Note that a link to clear the search is added, it simply links to the mail box without the search term parameter.
Category: Tutorials

Post Tagged with 


Saving...
Leave a Reply