Forum

Thread tagged as: Question, Runway

Speed up collection display

Hi

I've got a long list/detail page that groups a collection item (herewith called product) into categories. The client wants all the products on one page (I know!) - there are 100. I wondered if there was a better way to display the products than the code I've written below?

The current code cycles through the categories, displays some category blurb and then displays products in those categories. There are seven categories.

Each collection item is only displaying a thumbnail and title on the list page.

Basically it's taking a fair bit of time to read the details page and return the data for each product as it cycles through the categories. Currently the page is about 5 or 6 seconds before it finishes loading! Not ideal but I understand it's doing a lot of querying.

The first bit of the if code only triggers when a category is filtered through the url so isn't relevant here.

I'm not a php expert by any stretch of the imagination so would appreciate any pointers to speeding up the page!


<?php // Put all the categories into a variable $categories = perch_categories(array( 'set'=>'technologies', 'skip-template' => true, )); // Is there a category in the url? if (perch_get('cat')) { // Make the full category path from the set and cat slurrrgs $catpath = perch_get('set').'/'.perch_get('cat'); // Loop through the categories and grab the category info to display perch_category($catpath,array( 'template' => 'category-single.html', )); // Display the products in that category perch_collection('Products', array( 'template' => 'products/product_list.html', 'category' => $catpath, )); } else { // Loop through the categories foreach($categories as $category) { // Loop through the products and check if there are any in that category $products = perch_collection('Products', array( 'category' => $category['catPath'], 'skip-template' => true, )); // Does the category contain a product? if (!empty($products)) { // Loop through the categories and grab the category info to display perch_category($category['catPath'], array( 'template' => 'category-single.html', )); // Display the products in that category perch_collection('Products', array( 'template' => 'products/product_list.html', 'category' => $category['catPath'], )); } } } ?>

Many thanks in advance,

Jon

Jonathan Elliman

Jonathan Elliman 27 points

  • 5 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

I think the main issue is you're doing a lot of the work twice:

  1. Get all the categories
  2. Start looping
  3. Get the products for the category
  4. Get the category again (you've already got it)
  5. Get the products again (you've already got them)
  6. Finish looping

I would attempt to simplify the amount of work you're doing. For the loop:

    foreach($categories as $category) {

        // Pass the category into the template
        PerchSystem::set_vars($category);

        // Display the products in that category

        perch_collection('Products', [
            'template' => 'products/product_list.html',
            'category' => $category['catPath'],
        ]);

    }

and then in product_list.html add a <perch:before> section which outputs the category details, which should now be ready and waiting in the template.

Hi Drew

Thanks so much. That works a treat. I didn't know you could pass arrays through to templates.

1.5 secs for the page load now. Genius.

Kind regards

Jon