We are transitioning over to a new forum platform. Please post new issues there. Existing threads will be dealt with here unless you choose to move them over. Visit the new forum


Thread tagged as: Question, Problem, Runway

Trying to filter category by 'First Letter'

I have a client who has a directory of employee's and they want to be able to search by the first letter of their last names. I thought this would be fairly easy to accomplish, but I've been having some difficulties accomplishing this, and after trying different things and browsing here, I need to ask for help.

I've tried using the 'match' => 'contains' and 'match' => 'regex' options. The contains doesn't work unless a full last name is specified, and I finally got regex to work, but pulls any name with the chosen letter in their name.

I'm using a link by letter of the alphabet like:

<a href="/faculty-staff/staff-directory?last=A">A</a>
<a href="/faculty-staff/staff-directory?last=Z">Z</a>
<a href="/faculty-staff/staff-directory?last=Adams">Adams</a>

Here's the code I have currently:

    $urlLast = perch_get('last');
    echo "Variable (urlLast):" . $urlLast . "<br />\n";
    echo "Last is defined! (" . htmlspecialchars($_GET["last"]) . ")<br />\n" ;

        perch_collection('Directory', [
            'template' => '_display_directory.html',
            'filter' => 'last_name',
            'match' => 'regex',
            'value' => perch_get('last'),  // ($urlLast, -- tried this variable as well
            'sort' => 'last_name',
            'sort-order' => 'ASC',
            'count' => 8,
            'paginate' => true,         

} else {
    echo "Last IS NOT defined!<br />\n";

    perch_collection('Directory', [
            'template' => '_display_directory.html',
            'sort' => 'slug',
            'sort-order' => 'ASC',
            'count' => 8,
            'paginate' => true,         

When I click on one of the links, I get the results expected from whether last name is defined or not along with the correct letter I want to search for. If I click the one with Adams, I get just the one result as expected.

When I had the match = contains, I didn't get any results except Adams. With regex, I get all names with the letter in their name, but want to just filter by the first letter of their last name. Is this possible? I'm sure I'm doing something wrong, or there is a better method. Thanks for your help.

Linda Keele

Linda Keele 0 points

  • 4 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support


'filter' => 'last_name',
'match' => 'eqbetween',
'value' => perch_get('last').', '.perch_get('last').'Z',

Works perfectly! Thanks Drew. As always, you all are awesome.