Complicated queries with php - 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!

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!

How do I implement categories in 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.

Laravel: how to make a key of a column name?

I'm bending my mind for some time now over this problem. Could someone please help me?
I have two tables: products and product_attributes. The product has all basic product information and product_attributes has all specific information for products on different categories. It's much like the magenta attribute system. this table has 4 columns: id, product_id, attribute_name, attribute_value.
Now let's say a product has 2 attributes:
------------------------------------------------------
| id | product_id | attribute_name | attribute_value |
------------------------------------------------------
| 1 | 123 | length | 123cm |
------------------------------------------------------
| 2 | 123 | material | Denim |
------------------------------------------------------
| 3 | 123 | season | Summer |
------------------------------------------------------
Now if I set up the eloquent relationships and query a product, I get a product object with all three attributes. So far this is what I wanted. But now in a blade template I would like to be able to do something like this:
$product->attribute->length
Is this even possible or do I need to achieve these kind of things with a total different approach (like creating different tables for different product types/categories)?
Thanks in advance!
length is a tuple value not an attribute you need
$product->attribute->where('attribute_name', 'length')
or
$product->attribute->whereAttributeName('length')

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?

Categories