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).
I guess you should create table category with fields
id
parent_id
name
if it's root level category, its parent_id = 0,
otherwise its parent_id equals id of parent category.
You are talking about two entities, categories and subcategories. There is no further hierarchy (which would only make matters much more complicated). So I see two tables:
Category
category_id = primary key
name = not nullable
Subcategory
subcategory_id = primary key
name = not nullable
category_id = not nullable, foreign key to Category table
Related
I'm an absolute beginner to PHP and MySQL. I have a pre-coded project which I currently making personalised modifications.
Following are the screenshots of my database.
From the front-end I need to list all categories, and all respective products under each category.
I can't figure out how to bind two different tables and show products related to each category.
you need manyToOne relation with product table and categorie table:
one categorie have one or more product:
you need join between tow table:categorie(id_categorie,name,etc..),product(id_product,name,etc...,#id_categorie)id_categorie:foreignKey in product table.
In SQL table should have foreign keys. In PHP just take all categories and all products. When you list a category just list all of product with have categor_id equal to id of category with you listed:
categories: ID, name
peroducts: ID, name, category_id
List:
category 1
- products: all with have product.category_id = category.id
I have categories in my blog and i want the result if you edit the category name, then category names of the posts will change as well
if(isset($_POST['update_cat'])){
$cat_id = $_POST['cat_id'];
$cat_name = $_POST['cat_name'];
$var=mysqli_query($con,"UPDATE category SET category_name = '$cat_name' WHERE category_id = $cat_id");
if ($var) {
$var2=mysqli_query($con,"UPDATE post INNER JOIN category on post.postCategory=category.category_name SET postCategory = '$cat_name' WHERE postCategory = $cat_name");
}
There are two options for this:
1) You should create two database tables. One for category and one for posts, such that the ID(primary key) of the Categories table should be referenced from category_id(foreign key) of the posts table. The category name(which is in the category table), whenver changed, all posts that refer that corresponding ID, shall also bear the new name.
2) If you are maintaining a category_name as a column in the posts table, then you should use an after update trigger on the category table, such that whenever a category name changes, the trigger changes all the category_names in the Posts table.
The first option is the correct way of proceeding as the second might bring a lot of inconsistency in your records.
Yii beginner here. I am facing some problem with how to query the database with the scenario I have. Here is what my two tables look like. The reason category is maintained in a separate table is because a product can belong to multiple categories.
Product Table
-------------
id
product_name
product_desc
product_color
product_price
Category Table
--------------
id
product_category
product_id
A product can belong to multiple category.
Now, let's say I want to find the products of category 'xyz' with color 'blue'. I am not sure how do I query both the tables using two different models (or not) to achieve this. Any help?
Here you have many to many relationship where one product can belong to multiple categories and one category can belong to multiple products.
You will definitely need a third table
Product Table
-------------
id
product_name
product_desc
product_color
product_price
Category Table
--------------
id
category_name
Product_Category Table
----------------------
product_category_id
product_id ( foreign key to Product->id )
category_id (foreign key to Category->id )
You can implement simple query without having a model like this:
$connection=Yii::app()->db;
$sql = "SELECT .....";
$command = $connection->createCommand($sql);
$dataReader=$command->query();
$rows=$dataReader->read();
print_r($rows);
Arfeen's solution is perfectly valid.
Although, I would assume that you have already read the official Relational Active Record documentation on yii website.
I have some items, each item has a category and a sub-category. What is the best way to organize this in mysql ?
Create a table
catID
CategoryName
ParentCategory ....any other fields
Parent Category column will store the catID of Parent category if any.
or
CategoryTable
catID
CategoryName
CategoryRel Table
----------------------------------
ID
catID
SubcatID
where catID and subCatID are the IDs from category table.
First, you have a big decision to make. Will you design your database so that there are only two levels of category? Or will you allow for more than two levels in the future?
If you are willing to accept a permanent limit of category and sub-category, you can add two tables. Categories will list the allowed categories. Subcategories will list the allowed Subcategories and what Category they belong to. And your data table will include only the Subcategory. (If there is no information about a category other than its name, you could dispense with the Categories table and just include the category name in the Subcategories table.)
If you want to allow any number of levels, you will have only a single Categories table. Each row will have the category name and another column with the parent category. Your data table will still include a single category column.
categories
-----------
id
title
description
subcategories
-------------
id
title
description
category_id
items
-----------
id (*i had misspelled id as 'if'. corrected it here)
title
description
subcategory_id
I want to design a database for an e-commerce application with category/subcategory management. Please suggest a database schema where we can create categories and subcategories and add products to those categories. Each product can have multiple categories and we can select products belong to multiple categories using a boolean database query
Thanks
For categories and sub-categories to any level, along with products belonging to multiple categories, I would start with:
Categories:
category_id
parent_category_id foreign key (Categories.category_id)
... other category information ...
primary key (category_id)
Products:
product_id
... other product information ...
primary key (product_id_id)
ProductCategories:
product_id foreign key (Products.product_id)
category_id foreign key (Categories.category_id)
primary key (category_id,product_id)
index (product_id)
This way you can reach your goal of a hierarchy of categories as well as achieving a many-to-many relationship between products and categories.
You can select a product ID belonging to multiple categories (e.g., 3 and 4) with a query like:
select a.product_id
from Products a, Products b
where a.product_id = b.product_id
and a.category_id = 3
and b.category_id = 4
This would be best solved with a join table. That is, you have a products table, and a categories table, each with a primary key. Then, you create a third table, which is the join table. It has 3 columns: it's primary key, category_id, and product_id. Then, when you want to add a relationship between a product and a category, you insert a row into the join table with the category_id and product_id that are related. You can then use the 3 tables together with joins to display the relationships.