Forum

Thread tagged as: Question, Problem, Blog

Outputting A-Z results

Hi there

I have a directory of businesses, using the Blogs app: https://lovebedfordbusinesses.co.uk/businesses/

Outputting them by category is fine: https://lovebedfordbusinesses.co.uk/businesses/archive.php?cat=fashion

What I'm trying to do is output by A-Z.

Am I going about this completely the wrong way? In archive.php I have the following...

/* --------------------------- POSTS BY A-Z --------------------------- */
                if (perch_get('company')) {
                    echo '<h1 class="archive">Businesses beginning with '.perch_blog_category(perch_get('company'), true).' in Bedford</h1>';

                    perch_blog_custom(array(
                            'filter'=>substr('postTitle', 0,1),
                            'match'=>'eq',
                            'value'=>perch_get('company'),
                            'template'   => $template,
                            'count'      => $posts_per_page,
                            'sort'       => $sort_by,
                            'sort-order' => $sort_order,

                            ));

                    $posts_displayed = true;
                }

Thanks for help in advance.

Rich

Rich Hemery

Rich Hemery 0 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

That looks fine, presuming that

  1. $sort_by is the ID of a field in your template
  2. $sort_order is ASC.

This is my full code, however it doesn't seem to be working, sorry.



<div id="main-content3"> <?php // defaults for all modes $posts_per_page = 1000; $template = 'post_in_list.html'; $sort_order = 'ASC'; $sort_by = 'postTitle'; // Have we displayed any posts yet? $posts_displayed = false; /* perch_get() is used to get options from the URL. e.g. for the URL /blog/archive.php?cat=news perch_get('cat') would return 'news' because cat=news. The code below looks for different options in the URL, and then displays different types of listings based on it. */ /* --------------------------- POSTS BY A-Z --------------------------- */ if (perch_get('company')) { echo '<h1 class="archive">Businesses beginning with '.perch_blog_category(perch_get('company'), true).' in Bedford</h1>'; $title=perch_layout_var('postTitle', true); echo $title; perch_blog_custom(array( 'filter'=>substr($title, 0,1), 'match'=>'eq', 'value'=>perch_get('company'), 'template' => $template, 'count' => $posts_per_page, 'sort' => $pos, 'sort-order' => $sort_order, )); $posts_displayed = true; }

https://lovebedfordbusinesses.co.uk/businesses/archive.php?company=p

is the page in question... or =a or =b etc.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Ah, ok, I misunderstood - you're trying to filter items by the first character, is that right?

Yes that's right. I plan to have A, B, C etc. etc. Z as clickable links which when clicked on output all businesses beginning with the letter 'A' or 'B' etc.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Try:

'filter '=> substr($title, 0,1),
'match' => 'between',
'value' => perch_get('company').','.perch_get('company').'z',

Have tried that. No luck I'm afraid.

My code:

            <?php           
                // defaults for all modes
                $posts_per_page = 1000;
                $template       = 'post_in_list.html';
                $sort_order     = 'ASC';
                $sort_by        = 'postTitle';




                // Have we displayed any posts yet?
                $posts_displayed = false;

                /* 
                    perch_get() is used to get options from the URL.

                    e.g. for the URL 
                        /blog/archive.php?cat=news

                    perch_get('cat') would return 'news' because cat=news.


                    The code below looks for different options in the URL, and then displays different types of listings based on it.
                */





       /* --------------------------- POSTS BY A-Z --------------------------- */
                if (perch_get('company')) {
                    echo '<h1 class="archive">Businesses beginning with '.perch_blog_category(perch_get('company'), true).' in Bedford</h1>';
$title=perch_layout_var('postTitle', true);
echo $title;
                    perch_blog_custom(array(
                            'filter '=> substr($title, 0,1),
                            'match' => 'between',
                            'value' => perch_get('company').','.perch_get('company').'z',
                            'template'   => $template,
                            'count'      => $posts_per_page,
                            'sort'       => $sort_by,
                            'sort-order' => $sort_order,

                            ));

                    $posts_displayed = true;
                }

Page: https://lovebedfordbusinesses.co.uk/businesses/archive.php?company=a

Drew McLellan

Drew McLellan 2638 points
Perch Support

https://lovebedfordbusinesses.co.uk/businesses/archive.php?company=a


Debug Message SELECT * FROM perch2_pages WHERE pagePath='/businesses/archive.php' LIMIT 1 SELECT regionKey, regionHTML FROM perch2_content_regions WHERE regionPage='/businesses/archive.php' OR regionPage='*' ORDER BY regionPage DESC Using template: /templates/search/search-form.html SELECT pageID, pageParentID, pageDepth, pageTreePosition FROM perch2_pages WHERE pagePath='/top' OR pageSortPath='/top' LIMIT 1 SELECT * FROM perch2_pages WHERE pageNew=0 AND pageHidden=0 AND pageDepth >=0 AND pageDepth<=2 ORDER BY pageTreePosition ASC SELECT pageTreePosition FROM perch2_pages WHERE pagePath='/businesses/archive.php' LIMIT 1 SELECT pageID FROM perch2_pages WHERE pageTreePosition IN ('000-003-001', '000-003', '000') Using template: /templates/navigation/item.html Fetching from cache: perch_blog_category0cc175b9c0f1b6a831c399e269772661 Cache file not found: perch_blog_category0cc175b9c0f1b6a831c399e269772661 SELECT * FROM perch2_categories WHERE catPath='blog/a/' LIMIT 1 SELECT SQL_CALC_FOUND_ROWS DISTINCT tbl.* FROM ( SELECT idx.itemID, main.*, idx2.indexValue as sortval FROM perch2_blog_index idx JOIN perch2_blog_posts main ON idx.itemID=main.postID AND idx.itemKey='postID' JOIN perch2_blog_index idx2 ON idx.itemID=idx2.itemID AND idx.itemKey='postID' AND idx2.indexKey='postTitle' WHERE 1=1 AND ((idx.indexKey='' AND (idx.indexValue > 'a' AND idx.indexValue < 'az'))) AND idx.itemID=idx2.itemID AND idx.itemKey=idx2.itemKey GROUP BY idx.itemID ) as tbl WHERE (postStatus='Published' AND postDateTime<='2015-10-15 22:29:00' ) GROUP BY itemID ORDER BY sortval ASC LIMIT 0, 1000 SELECT FOUND_ROWS() AS `count` SELECT DISTINCT settingID, settingValue FROM perch2_settings WHERE userID=0 Using template: /templates/blog/post_in_list.html SELECT pageID, pageParentID, pageDepth, pageTreePosition FROM perch2_pages WHERE pagePath='/footer' OR pageSortPath='/footer' LIMIT 1 SELECT * FROM perch2_pages WHERE pageNew=0 AND pageHidden=0 AND pageTreePosition LIKE '000-006%' AND pageDepth >=1 AND pageDepth<=3 ORDER BY pageTreePosition ASC SELECT pageTreePosition FROM perch2_pages WHERE pagePath='/businesses/archive.php' LIMIT 1 SELECT pageID FROM perch2_pages WHERE pageTreePosition IN ('000-003-001', '000-003', '000') Using template: /templates/navigation/item.html Array ( [type] => 8 [message] => Undefined variable: postTitle [file] => /var/sites/l/lovebedfordbusinesses.co.uk/public_html/businesses/archive.php [line] => 51

Here it is Drew

Drew McLellan

Drew McLellan 2638 points
Perch Support

Oh, I think it's the filter option. It should be the field ID that contains the company name (postTitle?)

'filter '=> 'postTitle',
'match' => 'between',
'value' => perch_get('company').','.perch_get('company').'z',

This should find any postTitle that falls between, for example, A and AZ. That should be anything that begins with A.

Thanks a lot Drew, got it working... https://lovebedfordbusinesses.co.uk/businesses/archive.php?company=b

Just one last thing I can't figure out, the 'Businesses beginning with X in Bedford', obviously the X isn't working - I'm guessing because it's perch_blog_category, what should it be here?

if (perch_get('company')) {
                    echo '<h1 class="archive">Businesses beginning with '.perch_blog_category(perch_get('company'), true).' in Bedford</h1>';

Thanks

Drew McLellan

Drew McLellan 2638 points
Perch Support

Wouldn't that just be the letter you're filtering by?

if (perch_get('company')) {
    echo '<h1 class="archive">Businesses beginning with '.PerchUtil::html(strtoupper(perch_get('company'), true))).' in Bedford</h1>';
}

Hi Drew

Sorry, I'm getting an error on the page: https://lovebedfordbusinesses.co.uk/businesses/archive.php?company=b

Using the following...


if (perch_get('company')) { echo '<h1 class="archive">Businesses beginning with '.PerchUtil::html(strtoupper(perch_get('company'), true))).' in Bedford</h1>'; perch_blog_custom(array( 'filter' => 'postTitle', 'match' => 'between', 'value' => perch_get('company').','.perch_get('company').'z', 'template' => $template, 'count' => $posts_per_page, 'sort' => $sort_by, 'sort-order' => $sort_order, )); $posts_displayed = true; }
Rachel Andrew

Rachel Andrew 394 points
Perch Support

What is on line 48?

Line 48 is: echo '<h1 class="archive">Businesses beginning with '.PerchUtil::html(strtoupper(perch_get('company'), true))).' in Bedford</h1>';

edit The error message is: Parse error: syntax error, unexpected ')', expecting ',' or ';' in /var/sites/l/lovebedfordbusinesses.co.uk/public_html/businesses/archive.php on line 48

Drew McLellan

Drew McLellan 2638 points
Perch Support

Just need to fix the typo:

PerchUtil::html(strtoupper(perch_get('company', true)))