Hi all. I’m working on a website for a record store, there are between 8 and 10 categories. On the frontpage I show the latest arrivals for which I use a tapped jQuery content slider. It works great but I would like to limit the number of records per group (category_id) . If I use maxrows in my query it limit the total number of records to the number specified. If I use maxrows in the output it limit the total number of categories to the number specified. What should I do to limit the number of records per group, please see query and output below
Query
<cfquery name="getProducts" datasource="Highfidelity">
SELECT p.product_id, p.artist, p.title, p.publisher, p.release_date, p.price, p.category_id, c.category
FROM products p
INNER JOIN categories c ON p.category_id = c.category_id
ORDER BY p.category_id
</cfquery>
Hi rudi! thank you very very much. You’re right the order by was ambiguous indeed. It is now working like a charm.
About the joins! I have the queiries like this so that for example in the navigation only the categories are shown that have products related to them (hope this make any sense). In total I have 7 categories. If only 4 categories have products related only those categories will show in the drop down menus.
The database is more settled now. For the new arrivals I have added two new fields (isNew int(1)) and (added(timestamp default current_timestamp)). For isNew 1 will indicate that it is a new arrival. The Owner will update database once a week on Friday. When new arrivals will be added the value for older records with value 1 will be set to 0. This is the query:
<cfquery name="getProducts" datasource="#application.dsn#">
SELECT p.product_id, p.artist, p.title, p.publisher, p.release_date, p.condition, p.price, p.category_id, c.category, p.genre_id, g.genre
FROM products p
LEFT JOIN categories c ON p.category_id = c.category_id
LEFT JOIN genres g ON p.genre_id = g.genre_id
WHERE isNew = 1
ORDER BY category_id
</cfquery>
But I still don’t have a sollution for the limited group output. I would like to limit the output per group to 4 records. I tried a conditional loop:
let’s start with the query, which won’t work as written because of the ambiguous column in the ORDER BY clause
(hint: qualify all columns used in a query that involves more than one table)
is it possible that you have a product for a category that doesn’t exist? is it possible that you have a product for a genre that doesn’t exist? if the answer to these questions is no, then you should be using inner joins, not outer joins
as for your output, you don’t need the CFLOOP but you do need a CFIF to test the counter against your number 4
Hi rudi, thank you for the reply. Your absolutly right about the aditional categories query. I should have think about that.
You don’t see the additional clause for latest arrivals as yet since the client is not sure himself how he would like to handle this (adding new arrivals once a week to the database or more frequently per week is the question) so that wil be something I need to adjust later.
There will be a possibility for multple photos per product, since the store has quite a large selection of Collectors Items which the owner would like to promote as good as possible (with multiple photos)
Finally the descriptions, I have to admid your right again This comes, because the site will come in three languages, and somehow I thought it would be practical to sepperate the descriptions from the main products table. If you say It is not a good idea, than I think I probably reconsider.
Leaves me with the question, I started off with. How do I limit the records per group. I realy can’t think of something
first of all, you need to rewrite that section of the code where you do two additional queries inside the cfoutput loop for the categories – use a single join query instead
second, i don’t see anything in your code that handles the “latest arrivals” aspect – the only ORDER BY is on category_id
finally, i’m puzzled why you would have photos and descriptions in separate tables – i could see the need for a separate photos table if you wanted to have multiple photos per product (but then you’re not handling that in the code at present), but i’m really curious why there isn’t just a description column in the main products table
you should probably resolve the above issues before thinking about limiting the number of products displayed for each category