How do I implement categories in php - php

As the title suggests, I want to create category and sub category listing of products for the web store I am working on. Say there is a category named Apparel{parent category} => Shoes, footwear.. etc {child category} and shoes will have all the shoe products.
How do I implement this? ie Should I put all the main categories under one table. Each row would then reference another table?

This is how your table look like
id | item_name | parent_id |
1 | Shoes | 0 |
2 | Jackets | 0 |
3 | Formal | 1 |
4 | Casuals | 1 |
5 | Party Wear | 2 |
here you can see that your parent category has parent_id 0 and all the other sub categories are having id of their parent in parent_id column. In this way you can store your products in same table, differentiating them with parent_id
For example : Record 1 i.e Shoes is a parent and having parent_id 0, and record 3,4 are the sub categories of record 1, because they have parent id 1.

Related

Subcategories in php

I am doing a project in PHP, MySQL. I need to create a database in which there is a category to which there can be many subcategories ( like for a course book I can have physics, computer science, mathematics etc.).
Also there can be some categories which do not have subcategories (say Mythological Books).
The administrator has to add new category or subcategory via a form in PHP page which then updates the database.
How should I design the structure of my table where if admin for following two cases to be incorporated:
Admin only wants a subcategory to be added then upon selecting its parent category, it should be done.
Admin wants to add a new parent category and there exists no subcategory for it.
Please suggest how should I create my table(s).
Create a table called 'categories' with columns 'id', 'name', 'parent'.
Then use it like this:
1, Hardware, null
2, Storage, 1
3, Video, 1
4, Harddisks, 2
...
Here is a simple example with books:
Categories table:
+----+--------------------+--------+
| id | name | parent |
+----+--------------------+--------+
| 1 | Comics | NULL |
| 2 | Programming | NULL |
| 3 | SQL/PHP | 2 |
| 4 | Java | 2 |
| 5 | Marvel | 1 |
| 6 | Mythological Books | NULL |
+----+--------------------+--------+
Books table:
+----+---------------------------------------+----------+
| id | name | category |
+----+---------------------------------------+----------+
| 1 | PHP/MYSQL for dummies | 3 |
| 2 | Iron Man and the prisoners of azkaban | 5 |
+----+---------------------------------------+----------+
If you want to show all the books in the Programming>SQL/PHP category you just simply get them with a select query.
mysql> select * from books where category = 3;
+----+-----------------------+----------+
| id | name | category |
+----+-----------------------+----------+
| 1 | PHP/MYSQL for dummies | 3 |
+----+-----------------------+----------+
1 row in set (0,00 sec)

Relational database with multiple preferences and categories

I'm trying to wrap my head around designing my database which will store one or more preferences for many categories for each user. So in other words, each user can select one or more options from the Colors category, one or more options from the Shapes category, and so on.
My initial thought was to first have a User table with generic user information. Next, there would be a table to store all the different categories as so:
CATEGORY_ID | CATEGORY_VALUE
--------------------------------
1 | Colors
2 | Shapes
3 | Sizes
I'd separate each Category into it's own table (Colors for example):
OPTION_ID | OPTION_VALUE
------------------------------
1 | Red
2 | Blue
3 | Green
Finally, I would have a User Preferences table:
USER_ID | CATEGORY_ID | OPTION_ID
----------------------------------------
1 | 1 | 2
1 | 1 | 3
1 | 3 | 2
2 | 1 | 3
Am I on the right track here or is there a better/more efficient way to designing this. I will be setting up a search results page which will allow visitors to filter through these different categories.
Thanks!

PHP Categories Dropdownlist

I've been trying a lot of algorithms, including functions inside functions, and so on... But I haven't been able to solve this, hope you can help me.
I have a MySQL Table with categories, each category can have a parent category. Top categories are those whose parent is 0.
id | parent | name | desc
------------------------------------------------
1 | 0 | sportwear| test
2 | 0 | bikes | test
3 | 1 | shirts | more tests
4 | 1 | shoes | ....
5 | 3 | men | ....
6 | 3 | women | .....
AND SO ON....
I would like to have a SELECT element with a list like this
CATEGORIES:
-sportwear
-----shirts
--------men
--------women
-----shoes
-bikes
-morecategories
-etc
And it should have all the categories, and show them with their proper depth.
To get them All we would use something like
SELECT * FROM categories;
To get subcategories from a specific parent we would use something like
SELECT * FROM categories WHERE parent = PARENT-ID-HERE;
How can I make a PHP algorithm to build a select list like the one shown before?

Complicated queries with php

I got the following tables with some data in them:
Products:
+---------------------------+
|id|name |details |price|cn|
|__|_____|_________|_____|__|
| 1|pen |somethi |100 |10|
| 2|paper|something|30 |11|
+---------------------------+
Categories:
+----------------------------+
|id | name |parent_id |
|____________________________|
|1 | granny | 0 |
|2 | dad | 1 |
|3 | grandson | 2 |
|4 | grandson 2| 2 |
+----------------------------+
Products2categories:
+-------------------------------+
| id | product_id | category_id|
|_______________________________|
| 1 | 1 | 3 |
| 2 | 1 | 2 |
| 3 | 1 | 1 |
+-------------------------------+
Basically, there are products, categories and a table which match the product to the categories.
As you can see, every product can be related to several categories, just like in the example above where the product with the id of 1 (pen) is related to categories: 1,2,3.
That's how the database is build.
NOTE: there can be ONLY 3 levels per category, which means, grandson and grandson2 can't be parents.
Now, what I'm struggling with is the delete functionallity.
I want to choose from a list a category, the category can be a grandfather category, mabe a father category, or even a grandson category. Once I click on the delete button, I would like to delete the following things:
the products related to the category
all of the "son" categories
all of the products that the "son" categories hold
remove the prodcut_id and the category_id from the table Products2Categories
For example:
If I delete the category granny I would like to delete all of the products related to it, then I would like to delete the "son" category which is dad and all of the products related to it and unlink it from the table Products2Categories then, because dad has a "son" I would like to delete his sons, he actually has 2 of them, grandson and grandson2 and all of the products related to them and ofcourse unlink them from the Products2Categories table.
I've been trying to think of a code that can do that for the past 2 days and I just couldn't come up with any idea, I'm pretty much lost.
Thanks in advance!
NOTE: If you need ANY additional information, please feel free to ask for it!

Sub categories Hierarchy

I designed a SQL structure to represent categories and their subcategories.
I have 3 tables:
articles
articles_categories
categories
Articles table:
id,title,content
Categories table:
id, title, parent_id
articles_categories:
id,article_id,category_id
No problem with SQL, but now - lets say i'm on article id 5
article id 5 has - 3 categories, that 2 of them has parents, and the main has '0' as parent.
How do I fetch them all efficiently? (lets say for - breadcrumbs).
thanks!
Unless the depth of the category hierarchy is fixed, you cannot do this in MySQL with your current model (adjacency list). You'd have to traverse the hierarchy using several SQL statements in a loop.
If the category hierarchy is fairly static, you can "precompute" the tree by using:
Path enumeration
Nested sets
Closure table
All of the above, trades write performance for read performance.
Google or search SO for any of the above and you will find examples of how to implement it.
Quite often, I find that storing the data in a adjacency list (because of best matches the data model) and caching a copy of the tree in the application is good enough, but that depends on your requirements of course :)
This should do the job:
select * from articles_categories
left join categories on categories.id = articles_categories.category_id
where article_id=1;
+------+------------+-------------+------+--------+-----------+
| id | article_id | category_id | id | title | parent_id |
+------+------------+-------------+------+--------+-----------+
| NULL | 1 | 1 | 1 | first | 0 |
| NULL | 1 | 2 | 2 | second | 1 |
| NULL | 1 | 3 | 3 | third | 2 |
+------+------------+-------------+------+--------+-----------+
Additionally, I would remove the "id" column from associative table articles_categories.

Categories