editing the category of the posts in php mysql - php

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.

Related

PHP how to make a relationship between two tables

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

Adding Category and Subcategory in Database

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

Linked Tables and SQL SELECT Queries

I have a few linked tables in my custom forum: categories, sub_categories and posts
basically, I have people able to add up to three categories and five sub-categories when they make a new post.
I also enable people to 'listen' to certain categories and sub-categories and have them in an easy to access bar at the side of the page.
My tables are set up thus (only showing relavent fields for ease):
posts:
id INT
category_id VARCHAR(12)
sub_category_id VARCHAR(35)
categories:
id INT
name VARCHAR(20)
sub_categories:
id INT
name VARCHAR(20)
in my posts table, I store the set categories and sub-categories by their ID in the following format:
category_id [2][4][8]
sub_category_id [1][2][3][4][5]
thus enabling me to execute the following query in PHP and get the post based on category and sub-category:
SELECT * FROM posts WHERE category_id LIKE '%[{$id}]%' AND sub_category_id LIKE '%[{$id2}]%'
the problem I have is selecting the sub_categories for the access bar that people 'listen' to...
$sql = "SELECT title, id FROM categories";
$query = mysql_query($sql);
$list = array();
while ($row = mysql_fetch_assoc($query)){
$list[$row['title']] = array();
$sql = "SELECT sub_categories.title FROM sub_categories, posts WHERE (category_id LIKE '%[{$row['id']}]%') AND (????) LIMIT 0,100";
$query = mysql_query($sql);
while($result = mysql_fetch_assoc($query)){
$list[$row['title']][] = $result['title'];
}
}
print_r($list);
Obviously you can see where I am stuck (????), but before I explain what I am trying to do, I'll explain what the output I am looking for is.
when I print the $list array, I want it to print a multi-dimensional array featuring the categories as the first key, with their values being an array of sub-categories that have been tagged in the main category.
The problem I have is that in my sub_category_id field on the post table, remember the values are stored in the format [1][2][3] and I need to check the value against the subcategory field id.
I have tried the following:
"SELECT sub_categories.title FROM sub_categories, posts WHERE (category_id LIKE '%[{$row['id']}]%') AND (sub_category_id LIKE '%[sub_categories.id]%') LIMIT 0,100"
But it didn't work. I don't know whether there is an error in my query or whether it even SHOULD work, but I would be grateful if anyone could tell me how to do it or where I am going wrong in my code!
NB. I am trying to find which sub_categories appear in which categories based on people tagging them in a post together.
You're facing problems because you're not aware about few concepts in database...
In your case, you want to create associations called "many-to-many".
It means that a category can be used in many post and a post can be represented by many category.
To translate that into a "SQL" world, you have to create an intermediate table.
this table will store both identifier of the two tables.
for exemple:
-------------------
| categorisation |
-------------------
| post_id | => is the link to posts
| category_id | => is the link to categories
-------------------
When you create a new post, you create a new object in the table post. But you also create N records in the table categorisation.
When you want to retrieve which categories applied to this post, you can do a query like that:
SELECT post.id, post.name
FROM post
INNER JOIN categorisation on (post.id = categorisation.post_id)
INNER JOIN category ON (categorisation.category_id = category.id)
I think you need to read some articles on the web about database before progressing in you project ;)

Multiple database joins

I have three tables: categories, content_info, and content.
The categories table contains the category's id and the ID of its parent category.
The content_info contains two columns: entry_id for the post's ID and cat_id for the ID of the post's category.
The content table contains multiple columns about the post - such as ID, title, etc.
I have a variable in the URL called parent_id which corresponds to the parent of a category. I want to list all the POSTS (not CATEGORIES) which belong to a category with a parent of the parent_id value.
For example, say the parent_id value is 5. Each post might belong to a category with an ID of 20, but that category belongs to the parent category (whose ID is 5). I want to list all the posts who belong to categories with a parent value of whatever the current parent_id happens to be.
Is there a way of doing this with MySQL joins instead of changing the PHP?
This should do it:
SELECT c.* FROM content
JOIN content_info ci ON ci.entry_id=c.id
JOIN categories cat ON cat.id=ci.cat_id
WHERE cat.parent_id=<parent_id>
This return all posts (content rows) which belong to a category which parent is parent_id
Or with subqueries:
SELECT c.* FROM content
JOIN content_info ci ON ci.entry_id=c.id
WHERE ci.cat_id IN (SELECT id
FROM categories cat
WHERE cat.parent_id=<parent_id>)
SELECT c.*
FROM content c,
categories cat,
content_info ci
WHERE c.id = ci.entry_id
AND cat.id = ci.cat_id
AND cat.parent_id = 5

How to organize this in a mysql table?

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

Categories