I have a category table like this:
I can list categories as list and subcategories as dropdown list but My designer send me a design that categories have subcategories with the same name:
How can I manage this type of subcategories?
Thanks in advance
Sounds to me a "design" issue rather than a database issue.
You can add a dash symbol before the subcategories names in the dropdown, so the options values would be:
Category 1
-Subcategory 1
-Subcategory 2
Category 2
-Subcategory 1
-Subcategory 2
My appending " -" before the subcategory's title, you are creating levels in the dropdown, which the user can visually see, and recognize where each subcategory belong to.
It's not a perfect solution, but it's sure easy to implement. If you're willing to spend time on it, try research jquery plugins that have multi level select boxes support, such as http://php4bd.wordpress.com/2007/07/15/javascript-controlled-dependent-or-cascading-select-list/
You have id to uniquely identify your tuples. Duplicated names aren't a problem.
Also read: http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/
Related
I want to show categories and subcategories like a tree menu:
Category
Subcategory
Subcategory 2
Category 2
Subcategory
Subcategory 2
my database contains 3 colomns:
categoryId, categoryName, categoryParentId
I really don't have an idea how to do this?! Can somebody help me?! I found some codes on the net, but I couldn't make them work
The best way to make such a structure would be to implement either an Adjacency List model or Nested Set model. You can read more about them in this excellent article by Mike Hillyer.
Of course, you can always come up with a home-made solution, but in my experience, they often don't come up as good, effective and flexible as the two mentioned above.
I have a system in which I have to select "similar" records. Imagine a database containing a big list of products and when the user enters partial name of a product, a list of products come up as suggestions about the product he is searching for. These products have a longer description field too.
This is NOT about a WHERE product_name LIKE '%entered_string%' query, I think. The logic is akin to the one Stack Overflow might use, id est: when you ask a question, it prompts you with Questions that may already have your answer and Similar questions, both obviously using a method to derive what I want to ask from my question title/content and search against the database, showing the results.
I just wonder whether it is accomplishable with PHP and using MySQL as the database.
Example:
Entering food should give us results like 1kg oranges, bread and cookies. Both of these would have something similar which could help to link them programmatically to each other.
There can lots of methods to approach this scenario. but I think straight one is to have multiple keywords/tags mapped with every item. so when user types in, you would not be searching item table, you should be searching the mapped keywords and based on that searching loading the relevant items.
If you want similar products to show up, you need to put that information in your database.
So, make a category for foods, and assign every food product to that category. That way you can select similar products easily. There is no other efficient way to do this
So your database:
categories:
|id|name
1 fruit
2 Cars
Products
|id|name|category_id
1 apple 1
2 Ford focus 2
And you can select like this:
SELECT `name`,`id` FROM `products` WHERE category_id = 1;
Another way (as suggested in a comment) are tags
Products
|id|name|tags
1 apple "fruit food delicious"
2 Ford focus "Car wheels bumper"
Best way is to use a fulltext search on the tags:
SELECT * FROM `products` WHERE MATCH(tags) AGAINST ('fruit')
Make sure to have a fulltext index on tags.
I know the title may not be exactly what this is about but bear with me.
I don't know how another title for this.
Well look this is my situation.
I'm building a little cms system (for myself and to learn from it). I want the pages inside the CMS to be listed and ordered by categories.
It will look something like this:
Webpages
- Home
-- homepage(this is the web page itself)
- News
-- Latest news
-- Archive
this system would mean I will have sub-categories.
In the database I have made a table:
| ID | Parent_ID | Name | Lable | Order|
1 1 Webpages webpages 1
2 1 Home home 1
3 1 News news 2
As you can see here the main category is the Webpages category and Home and News are sub-categories of it.
And those 2 categories are ordered so the Home category is first then the News second.
The problem I'm facing is this:
If i want to get all the sub-categories means i need to start with the main category Webpages and with that ID i can get the sub-categories of the main category.
I think you can see how deep this can go en that would mean (I think) that there will eventually be many query's that will be run for each sub-category.
So my question is:
Is there a way to get all the sub-categories at once in the correct order in one ore 2 query's.
if you have an answer, please let me know.
thnks
When writing the query you will need to use the ORDER BY clause. For this specific example you will need to ORDER BY Parent_ID, Order. This will return sorted by Parent_ID and then sub sorted by Order.
You can then use mysql_fetch_assoc() for parse the result set into an associative array.
Since you are going to have multiple levels of hierarchy you will have to loop through the result set once store it in a associative array consisting of the hierarchy structure.
This array can then be used to display the appropriate navigation hierarchy on the page.
The first time I tried to do this, I created a field in the category table called query. That contained strings like:
brand = "Burberry" AND type != "Watch"
Which I then inserted into the WHERE clause of a query to find a category's products.
That probably wasn't the best design.
My second attempt was to use a tagging system. I would create a tag table with tags like Burberry and Watch. I had a table tying the tags to the products (HABTM). I also had a table tying the tags to the categories.
The table tying tags to categories had an extra field called include which if it was a 1 then all products selected must also have that tag. Or if it was a 0 then all products selected must NOT have that tag.
This seemed to be a better design then my original, but it required some pretty complex joins.
Now I need to approach this problem once again.
One difference is I am now using the CakePHP (1.3) framework.
Before I try reinventing the wheel again. I was wondering if there are any known patterns/solutions I could use?
Probably you've already done that somehow by now, but here are my 2cents:
I'd drop Categories<->Tags, because I feel that you're unnecessarily duplicating data with it.
I.e. tables should be just categories, categories_products, products, products_tags and tags.
This way:
you wouldn't have to bother about changing category tags when products are added or removed from category
your searches would become more uniform (since there's only one tagging table)
and your tags still would be no more than 3 JOINS away - which is quite comfortable :)
From what I can understand you should have 5 tables:
Categories
Products
Tags
Categories_Tags
Products_Tags
UPDATE: When the user defines what should be selected, the HABTM tables are updated so that the tags/categories link to the products they should be linked to only.
So the query will look something like:
SELECT * FROM products WHERE ID in (SELECT product_id from tag list to include) AND ID NOT IN (select product_id FROM tag list to NOT include)
Maybe I'm missing what you're trying to accomplish here, but this sounds like you're making it more complicated than it needs to be.
Create three tables: Product, Category, and ProductCategory. Product and Category each have an id. Then ProductCategory includes ProductId / CategoryId pairs.
Like:
Product
ProductId Name
1 Lamp
2 Carpet
3 Drill
4 Power cord
5 3/8" bolt
Category
CategoryId Name
1 Electrical
2 Home decor
3 Hardware
ProductCategory
ProductId CategoryId
1 1
1 2
2 2
3 1
3 3
4 1
5 3
Then if you want, e.g., to know all the "Hardware" items:
select product.*
from category
join productcategory using (categoryid)
join product using (productid)
where category.name='Hardware'
I'm looking for a solution to list and browse categories and subcategories and their records (classified ads), when you store category levels in separate tables. In the past I have worked with the adjacency model but I have to stick to this database setup now, and it is new to me. I'm using php and mysql.
The site is a classified ad site structured the common way: it has the main category list on its homepage, when you click one of the category links then only its subcategories are listed and the ads that belong to this category, and so on, at every level.
I'm a bit confused in the following areas:
How do you construct
the category links when browsing
categories in order for the script to know which table it should select categories from if I consider the below
mysql structure? Do I need separate
parameters at every category level I access
like e.g: "mysite.com/?cat2=4" when
accessing category "4" in the cat2
table and "mysite.com/?cat3=9" when
accessing category "9" in cat3 table
in order to identify category
levels? If separate parameter not
needed, then how can php and mysql
tell what table you have to select
the categories from?
And most
importantly in this case, what is
the best way to construct SEO
friendly links? And how will mysql know
which table to select categories
from? I would like to use the most
simplest solution that is possible
like:
mysite.com/electronics/television/sony.
As far as I know, I have to include
at least the cat_id
somewhere in the link... where do I put it? and do I have to include the number of level as well? To
complicate it more the category
names are in foreign language with
accented characters (though I
created a function that changes
accented characters into latin ones
on the fly when generating category
links) so I think it is best to
select them by their ids.
How is a sample mysql select looks
like that selects the child
categories of a certain category?
How can I construct breadcrumb
navigation?
MYSQL STRUCTURE:
Table "cat1" (main category):
cat1_id | cat1_name
Table "cat2" (subcategory):
cat2_id | cat1_id | cat2_name
Table "cat3" (subsubcategory):
cat3_id | cat2_id | cat3_name
Table "ads":
ad_id | cat1_id | cat2_id | cat3_id | ad_title | ad_description
Thanks and sorry for the long post.
My favourite pattern for category (and tag) URLs:
http://mysite.com/articles/brains+zombies+legs+frogs
The + symbol is nice for tags, and friendly to spiders (and SEO). Using the text of the categories is important for both spiders and humans as it's meaningful.
As for the SQL, I suggest 2 tables for anything with categories or tags:
Categories (id, name, description)
CategoryRelationships (catID, thingID)
For any given thing, you join Categories to Things via CategoryRelationships. For example:
SELECT * FROM Things t
JOIN CategoryRelationships ON thingID = t.ID
JOIN Categories c ON catID = c.CatID
The result will be a list of Things and their categories, where you have only one definition of each category, and a bunch of links to the categories via the Relationship table.
As for breadcrumbs, they're a slightly different problem. Breadcrumbs either:
Provide navigation through your site hierarchy, or
Help the user retrace their steps
Depending on the type of breadcrumb you're aiming at, you take a different approach. For a simple site hierarchy set of breadcrumbs, you can simply parse the URL and foreach over the set of segments:
http://mysite.com/people/zombies/brains/brains
Parsing the URI would result in:
people, zombies, brains, brains
For which you would generate links to each segment.