I’ll start by explaining what I’m doing – I feel a ‘real world’ explanation will make this easier to understand. I have a Category & Subcategory list for an online store. The database table is configured in a simple fashion – id, category_name and subcategoryof – subcategoryof being a 0 to represent a top-level category.
In the main index of the store, I’m listing all categories and subcategories – using a combination of a single query using a left join, and cfoutput within the cfoutput query (code pasted below). This works well and allows me to style the top-level categories and subcategories well enough to distinguish between them.
However, the list is getting a bit long now and a fair amount of scrolling is now required to navigate the page. What I would like to do is this;
Output all the top-level categories as at present
Output only 2 of the Subcategories
Show a count of how many extra subcategories there are (i.e +7 More Subcategories).
Now, I will actually output everything as I do now – however after the 2nd subcategory, I’ll set the following subcategories as hidden and use the ‘+7 More Subcategories’ text to toggle the display (still with me here?).
My problem, and question, is this – how do I get a count of subcategories for use with my ‘+x More Subcategories’ text? I assume my current plan of using an incremental count to determine the point at which I start hiding rows is the best route?
Existing code below
<cfquery name="getcategories">
SELECT p.ID AS CategoryID, p.Cat_Name as CategoryName, p.Cat_Shortname, c.ID AS SubCategoryID, c.Cat_Name as SubCategoryName, c.Cat_Shortname AS SubCatShortname
FROM product_categories p LEFT JOIN product_categories c ON p.ID = c.SubcategoryOf
WHERE p.SubcategoryOf = 0
</cfquery>
<ul>
<cfoutput query="getcategories" group="CategoryName">
<li class="catli"><a href="">#CategoryName#</a></li>
<cfoutput><li class="subli"><a href="">#SubcategoryName#</a></li></cfoutput>
<li class="subli moreli"><a href="">+ 7 More Subcategories</a></li>
</cfoutput>
</ul>
1 Answer