User-filtered list - efficient way to get total results when using pagination?

  • Hi,


    I have a user-filtered list working great and Perch Runway really is amazing for enabling this powerful filtering.


    But I noticed that if I use pagination, the number of items returned is confined to that number. I want pagination, but I want to inform the user the total results.


    Now... I *could* perform another identical call, but without pagination. It works, but it feels like a waste of energy to do double the load on server just to get that number. Is there any way I get can the total results in just the first call? Seems like that number must be available somewhere, otherwise how would it know how many page links to render?


    code: if 40 results are found and paginated into 20 per page, it only reports 20 were found when I use php count() to get that info. I'm hoping there's an easy way. I wanted to avoid too much complicated php :-)


    edit how do i format code on this forum so it's neat?

    Code
    1. $col_item = perch_collection('my_collection', [ 'filter' => $filters, 'template' => 'my-collection/my-template.html', 'paginate' => true, 'count' => 20, 'sort' => $sort_id, 'sort-order' => $sort_order, 'sort-type' => $sort_type, 'skip-template' => true, 'return-html' => true, ]);
  • Thanks but I already knew about that. I was hoping to get the total in the collection function, as I'm outputting the total at the top of the page, not down near the pagination links.


    The other problem with the pagination template var "total", is it only kicks in when there's pagination. So if there's 20 or less results when pagination count is 20, that variable can't be accessed.


    It's okay I've found a workaround. I'm using a condition where count ($col_item) is used when there's not enough results for pagination, and the pagination template var when there's more than 1 page of results. Bit messy, but works. And to get the total up the page where I need it displayed, I'm using CSS :


    essentially :


    <perch:if exists="paging">

        <style>

            .num-items::before {

                content: "<perch:content id="total" type="hidden">";

            }

            <perch:if id="number_of_pages" match="gt" value="1" >

                .num-items-b {display:none}

            </perch:if>

        </style>

    </perch:if>

  • Actually, the total variable is added to every single item in the list. This means it can be used in perch:before, perch:after or in between:


    HTML
    1. <perch:before>
    2. <perch:content id="total">
    3. </perch:before>
    4. <perch:content id="total">
    5. <perch:after>
    6. <perch:content id="total">
    7. </perch:after>



    And instead of:


    HTML
    1. <perch:exists="paging"><perch:content id="total"></perch:if>


    Use:


    HTML
    1. <perch:exists="total"><perch:content id="total"></perch:if>