If you go to a website like Newegg or B&H when they show a list of products, there are several filter options like Brand, Price, Size, Color, etc.
What is a good design pattern for something like this?
Also keep in mi开发者_开发知识库nd, not all products have the same options -- A monitor will have a max resolution and screen size, an HDD will have a capacity and connection type, etc.
The only thing I can think of is using EAV
architecture in the data-base to tie all the filtering options for each product, then when loading all the products, pull all the options and display filtering options for the applicable categories. That doesn't sound like it will scale well though, and still leaves me wondering what I do if only half the products shown have a particular option (screen size).
product(id, name, price, etc.) // 1, "Cool Monitor", 200.00
filter_options(id, option) // 3, "Screen Size"
product_filters(product_id, filter_id, value) // 1, 3, "22.in"
I don't want to reinvent the wheel or come up with an overly complex solution if there is already a known pattern for something like this. I am guessing there is since I see this a lot these days.
If it matters I am building this in PHP, CakePHP 1.3, with MySQL.
Use the specification pattern. It was built for this.
I couldn't find a good php implementation though. But it shouldn't be that hard to build.
What I would do is create a filtering function for each table you're querying and call each one respectively and they'll handle how and what they filter on.
My $.02
http://dev.mysql.com/doc/refman/5.1/en/adding-functions.html
精彩评论