5

Is it possible to change the limit of a loop, dynamically?

I'm currently using CartThrob for my eCommerce site, and want to add the option to show, 10, 20, 30 or All products on a page. How easy is it to do this?

Would it require some PHP?

Also - I want to do a "Showing 1 - 10 of 330 Products", how could I go about doing this?

1
  • You might want to split out your second question about how to generate a "Showing 1 - 10 of 330 Products" message and post it as a new question.
    – Alex Kendrick
    Dec 19, 2012 at 19:19

2 Answers 2

7

Is it possible to change the limit of a loop, dynamically?

Would it require some PHP?

You can do it without using PHP. Here are two ways that come to mind, but I'm sure there are others.

Method 1: Using a segment variable

The following loop for a products listing will set a default limit of 10, but uses segment_2 to set a different limit dynamically.

{exp:channel:entries
    channel="products"
    disable="categories|category_fields|member_data"
    limit="{if segment_2 != ''}{segment_2}{/if}{if segment_2 == ''}10{/if}"
    dynamic="no"
    paginate="bottom"
}

        {title}<br>

        {paginate}
            <div>Page {current_page} of {total_pages} pages {pagination_links}</div>
        {/paginate}

{/exp:channel:entries} 

Then to allow the user to show a different number of results per page, you just provide some links like

<a href="/products/10">Show 10 results per page</a> 
<a href="/products/20">Show 20 results per page</a>

etc (building your links according to your preferences). The pagination will continue to work, but with a different limit.

Method 2: Using a form, POST data, and Dynamic Parameters

{exp:channel:entries
    channel="products"
    disable="categories|category_fields|member_data"
    limit="10"
    dynamic="no"
    paginate="bottom"
    dynamic_parameters="limit"
}

        {title}<br>

        {paginate}
            <div>Page {current_page} of {total_pages} pages {pagination_links}</div>
        {/paginate}

{/exp:channel:entries} 

<form method="post" action="/products">
    <select name="limit">
        <option value="10">Result Limit:</option>
        <option value="10">10</option>
        <option value="20">20</option>
        <option value="30">30</option>
    </select>
    <input type="submit" value="Go!" />
</form>
3
  • I think the IF statement is the best option. I don't really want to use a form. Plus I'd have to start playing with jQuery to make it so when they just change the limit it auto refreshes
    – Jason Mayo
    Dec 20, 2012 at 11:13
  • Also, how could I do do this: "Also - I want to do a "Showing 1 - 10 of 330 Products", how could I go about doing this?"
    – Jason Mayo
    Dec 20, 2012 at 11:21
  • I think it will be easier to answer (and more findable for future users) if you split that out and post it as a new, separate question.
    – Alex Kendrick
    Dec 20, 2012 at 16:00
2

Jason,

You could do this easily by using an embed for your channel entries tag like this.

First have the link to change the limit number end up placing that number as the last segment by doing something like this:

<a href="/template_group/template/20">view 20</a>

Then create your embed

{embed="store/_embedded_template" my_limit="{last_segment}"}

Then in your embedded templated your channnel entries tag would look like this

{exp:channel:entries channel="foo" limit="{embed:my_limit}"}

If you wish to avoid embeds you can use Low Variables with pre:parse and set it up essentially the same way.

4
  • Won't {last_segment} become something like P5 when browsing through to paginated results? I think this would goof up the my_limit you are passing through to the embed.
    – Alex Kendrick
    Dec 19, 2012 at 16:46
  • didn't think about pagination, in that case choose {segment_3} or which ever segment is appropriate.
    – CreateSean
    Dec 19, 2012 at 18:17
  • If the limit value is available as a url segment, there isn't a need to pass it to an embed. You can just use segment_x directly in the limit paramter on the channel:entries tag without needing to use an embedded template.
    – Alex Kendrick
    Dec 19, 2012 at 20:25
  • Yeah, my entry loop is already in an embed, so it can be used over and over again throughout my site, with variables like limit etc. But I might make this dynamic and try it this way.
    – Jason Mayo
    Dec 20, 2012 at 11:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.