ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Query to pull category listing

October 08, 2012 10:15pm

Subscribe [1]
  • #1 / Oct 08, 2012 10:15pm

    Tony Geer

    253 posts

    I have a category group assigned to a channel. The categories are structured like this:

    Author
    - Author1
    - Author2
    ....


    Publisher
    - Publisher1
    - Publisher 2
    ...

    With Author and Publisher being the main categories.

    The list of authors and publishers are both very long and are added to constantly. I’m currently using GWCode Categories to generate a list of each for population into a dropdown menu, but it’s really memory intensive, increasing the load time from just over 1 second to just over 3 seconds.

    I think a query might be a good alternative to try, but I’m really bad at MySQL and the like. Any help?

  • #2 / Oct 10, 2012 11:48am

    Kevin Smith

    4784 posts

    Hi Tony,

    I might be able to come up with something, but since you’re using a third-party add-on to accomplish this at the moment, something tells me your dropdown is more than just a simple list of existing categories. Could you tell me exactly how the data needs to be structured in this dropdown? Perhaps if you provide your existing template code, it’ll help shed some light on it for me.

    Thanks!

  • #3 / Oct 10, 2012 11:29pm

    Tony Geer

    253 posts

    It actually is just a list that I’m outputting to create a dropdown. The reason I’m using it is because it allows me to display all the subcategories of a specific category. My categories are set up like so:

    Authors (cat ID of 1)
    - Author1
    - Author2
    ....


    Publishers (cat ID of 2)
    - Publisher1
    - Publisher2
    ...

    So in one dropdown I’m outputting only:

    - Author1
    - Author2
    ....
    - AuthorN

    and the second dropdown I’m outputting:

    - Publisher1
    - Publisher2
    ...
    - PublisherN

    There are 1,000 categories in total here, split between Authors and Publishers.

  • #4 / Oct 12, 2012 5:51pm

    Kevin Smith

    4784 posts

    Oh ok, well this is actually simpler than I thought then. Is there a reason the native Channel Categories tag won’t work here?

  • #5 / Oct 14, 2012 2:29pm

    Tony Geer

    253 posts

    That’s strange, I’m not sure why I ended up using GW Code. I just tried using the categories tag like so:

    <form>
        <select>
         <option value="">Select author…</option>
          {exp:channel:categories show="not 2"}
          <option value="{site_url}books/author/{category_url_title}">{category_name}</option>
          {/exp:channel:categories}   
        </select>
         
       </form>

    And it works, except that it also includes “Authors” at the top of the list, which I don’t want. Is there any way I can remove that one? Perhaps this is why I used GWCode.

    The other issue right now is this: without any of this code listing all these categories, my benchmarks look like:

    BENCHMARKS
    Loading Time: Base Classes   0.0850
    Controller Execution Time ( Ee / Index )  1.9638
    Total Execution Time   2.0491

    And afterward, like this:


    BENCHMARKS
    Loading Time: Base Classes   0.0645
    Controller Execution Time ( Ee / Index )  3.0686
    Total Execution Time   3.1333

    Sometimes a total execution time of 5 seconds. Now this is obviously because there are 1,000 categories in there. Am I better off loading a manually generated, flat file with the list of categories and then manually updating it as categories are added?

  • #6 / Oct 15, 2012 9:32pm

    Robin Sowell

    13255 posts

    😉  Glad the easy way works.  You should be able to ‘hide’ author by using a conditional:

    {exp:channel:categories show="not 2"}
    {if category_id != 1}
    
          <option value="{site_url}books/author/{category_url_title}">{category_name}</option>
    {/if}
          {/exp:channel:categories}

    That said… I agree- I’m not loving the extra time this takes.  If it was rarely updated?  I’d probably hard code it in a template and embed it.  Depending on how critical it is?  I might even write an extension and generate a static file.

    On flip side- if those options don’t appeal (don’t want to take time for the extension and it’s updated too often to make manual a decent option), I’d consider this a good option for Tag Caching.

    And because it would be quick and easy- may as well see if you get much improvement from using the query module.  Basically:

    {exp:query sql="select * from exp_categories where parent_id = 1 order by cat_name"}
          <option value="{site_url}books/author/{cat_url_title}">{cat_name}</option>
          {/exp:query}

    Note- I had to tweak some of the variable names to match the fields in the db.  I suspect this will cut down a little on the resources.  Combine with tag caching and see if you hit a dynamic result you’re pleased with.

  • #7 / Oct 16, 2012 8:34pm

    Tony Geer

    253 posts

    Thanks, I’ll have to try it this weekend. I’ll let you know how it goes. Thanks for all the help so far.

  • #8 / Oct 17, 2012 12:55pm

    Kevin Smith

    4784 posts

    Let us know how it works out, Tony!

  • #9 / Oct 27, 2012 10:05am

    Tony Geer

    253 posts

    Hi Guys.

    I went ahead and used the query that you wrote and this improves performance dramatically. I also added tag caching so that should help a bit too. Thanks for all the help!

  • #10 / Oct 29, 2012 9:18am

    Robin Sowell

    13255 posts

    😉  Thanks for the confirmation- and glad that helped.  If you run into anything else, just let us know!

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases