Subcategories in php - 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)

Related

Generating link during fetch vs during creation

Suppose I a table like this:
Categories:
| cat_id | cat_name |
|--------|----------|
| 1 | tvs |
| 2 | phones |
| 3 | tablets |
And then I have:
Products:
| product_id | product_name | product_category |
|------------|-----------------|------------------|
| 1 | tv sony | 1 |
| 2 | tv samsung | 1 |
| 3 | phone htc | 2 |
| 4 | phone motorolla | 2 |
| 5 | tablet apple | 3 |
And someone goes into index.php where I get 10 random products from the DB. For creating the link I'd have to fetch the database again (to get the name of the category) and then I could do something like mysite.com/phones/4 (that would be the link to phone motorolla).
Of course 10 wouldn't be too hard on the server but it's still another fetch on the database.
The other option is to fetch for the category name during the product creation, and generate a link there, something like:
| product_id | product_name | product_category | product_link |
|------------|-----------------|------------------|--------------|
| 1 | tv sony | 1 | tvs/1 |
| 2 | tv samsung | 1 | tvs/2 |
| 3 | phone htc | 2 | phones/3 |
| 4 | phone motorolla | 2 | phones/4 |
| 5 | tablet apple | 3 | tablets/5 |
However the name of the category may change and so the link wouldn't work anymore. So what should I do? Is there another way to do this?
Alright Nick so in SQL you have something very powerful called JOIN. A join in SQL allows you to merge columns from different tables based on a key. Usually the key is a primary key or a foreign key.
In your case, it seems you have as primary keys : cat_id and product_id
And as foreign key you have : product_category
Based on your database schema, you can query your tables like this :
SELECT p.product_id, p.product_name, c.cat_name
FROM PRODUCTS p INNER JOIN CATEGORIES c
ON p.product_category = c.cat_id
With this query you retrieve your product_id, product_name and cat_name based with only one query for your database.
Here is more info about joins in SQL => https://www.w3schools.com/sql/sql_join.asp
Hope it will help you.

Get last inserts from multiple tables

i am trying to build something like an order management tool for a car garage.
The main function of the tool is to manage repair orders and inspection orders as well
Therefore i have created this following three tables.
In the first one you can see the cars and the state of the repair and inspection.
The second one shows the information about the single repair orders the relation between the first and this is 1:n.
The last one shows the automaticly created inspection orders for each car out of the first table. This is an 1:n relation as well.
So what i try to do is to show all the open repair and inspection orders for the cars in one table. But only the open ones.
I tried it with some where statements but i got totaly confused.
My question is, how i can realise it?
+------+--------------------------+----------+------------+
| IDWZ | wz_name | wz_stand | wz_vistand |
+------+--------------------------+----------+------------+
| 1 | Querbr?cke vorn | 0 | 0 |
| 2 | Front Lateral Support | 0 | 1 |
| 3 | Rear Support | 1 | 1 |
| 4 | MID-X-Member Upper Shell | 1 | 1 |
| 5 | Front Lateral Support | 1 | 1 |
+------+--------------------------+----------+------------+
+---------+-----------------+--------------+
| IDWZTBL | rep_wzrepstatus | rep_wzfehler |
+---------+-----------------+--------------+
| 2 | 1 | REP 1 |
| 1 | 1 | REp2 |
| 1 | 1 | REp 3 MASS |
| 1 | 0 | 444 |
| 2 | 0 | |
+---------+-----------------+--------------+
+--------+-------------+
| VIWZID | vi_repstand |
+--------+-------------+
| 1 | 0 |
+--------+-------------+
Sry for that!
So the IDWZ is the foreign KEY in the second table(IDWZTBL) and in the third (VIWZID).
I tried it with
SELECT wz_name, wz_stand, wz_vistand, rep_wzrepstatus, vi_repstand FROM tbl_wz LEFT JOIN tbl_orders ON tbl_wz.IDWZ = tbl_orders.IDWZTBL LEFT JOIN tbl_vi ON tbl_wz.IDWZ = tbl_vi.VIWZID WHERE wz_stand='0' AND rep_wzrepstatus='0' ...
Only for the first table cars to the second one repair orders, that WHERE staement (WHERE wz_stand='0' AND rep_wzrepstatus='0') works fine.
But if i try to add the third table (VI) doing the same, i could fetch the result i wanna have.
What i wanna see in the Overview table is only the last open repair order and the last open inspection order.

Sql Select data pick id from string

Category Table
+----+-----------------------+
| id | category_name |
+----+-----------------------+
| 1 | Buy Book |
| 2 | Buy other thinks |
+----+-----------------------+
Buy Table
+----+-----------------------+----------+-------------+----------+--------+-------+
| id | identity | name | description | per_rate | bought | costs |
+----+-----------------------+----------+-------------+----------+--------+-------+
| 1 | PROJECT[1]CATEGORY[1] | BOOK | PHP BOOK | 10 | 50 | 5000 |
| 2 | PROJECT[1]CATEGORY[1] | BOOK | PHP BOOK | 10 | 40 | 4000 |
| 3 | PROJECT[2]CATEGORY[1] | BOOK | JS BOOK | 2 | 50 | 100 |
+----+-----------------------+----------+-------------+----------+--------+-------+
I Want to Select category name from Other table when I select this table.
identity: PROJECT[project_id]CATEGORY[category_id]
So There are any way to pick the category id and select category name from other table
I Want Like This Table
+----+---------------+-----------------------+----------+-------------+----------+--------+-------+
| id | category_name | identity | name | description | per_rate | bought | costs |
+----+---------------+-----------------------+----------+-------------+----------+--------+-------+
| 1 | Buy Book | PROJECT[1]CATEGORY[1] | BOOK | PHP BOOK | 10 | 50 | 5000 |
| 2 | Buy Book | PROJECT[1]CATEGORY[1] | BOOK | PHP BOOK | 10 | 40 | 4000 |
| 3 | Buy Book | PROJECT[2]CATEGORY[1] | BOOK | JS BOOK | 2 | 50 | 100 |
+----+---------------+-----------------------+----------+-------------+----------+--------+-------+
You have a really bad data structure. The project and category should be in their own columns, with numbers stored properly as numbers, and proper foreign key relationships. In MySQL, doing this might require a trigger, but it is worth it.
Sometimes, we are stuck with other people's bad decisions. You can do what you want using like:
select b.*, c.category_name
from buy b join
category c
on b.identity like concat('%CATEGORY[', c.id, ']');
However, you should probably put effort into fixing the broken data structure.

Junction tables in MySQL

(Sorry, my english isn't very good)
Hi, I am trying to learn how to work with junction tables in MySQL and I can't figure how to do something. I know the basics of MySQL but I have never worked with "JOIN".
In this test project, I would like to be able to show on a page the app of a given category (you click on "Games", only the apps that are in the "Games" category will be displayed on the page). I would like to know what the SQL request should look like.
Second question, let's say that an App could fit 2 different categories, how can I manage to give that app 2 different Category_ID in my database ?
Here is what my Database looks like at the moment :
Table name: APPS
+------------+-------------------+
| App_ID (pk)| App_Name |
+------------+-------------------+
| 1 | Weather Network |
| 2 | Is it sunny 2.0 |
| 3 | The Weather App |
| 4 | Zelda |
| 5 | Megaman |
| 6 | Doom 3 |
+------------+-------------------+
Table name : CATEGORY
+-----------------+-----------------+
| Category_ID (pk)| Category_Name |
+-----------------+-----------------+
| 1 | Games |
| 2 | Weather |
+-----------------+-----------------+
Table name : JUNCTION_APP_CATEGORY
+----------------+--------------------+
| APP_ID (pk) | Category_ID (pk) |
+----------------+--------------------+
| 1 | 2 |
| 2 | 2 |
| 3 | 2 |
| 4 | 1 |
| 5 | 1 |
| 6 | 1 |
+----------------+--------------------+
For your first question, the answer is
SELECT a.*, c.*
FROM APPS a, CATEGORY c, JUNCTION_APP_CATEGORY ac
WHERE a.App_ID=ac.APP_ID
AND c.Category_ID=ac.Category_ID
AND ac.Category_ID=<category_id for category "Games">
For your second question, you can use both APP_ID and Categor_ID as the primary key of table JUNCTION_APP_CATEGORY(note NOT TWO pks, but use the two columns together as ONE pk). So that you can put data like this:
+----------------+--------------------+
| APP_ID (pk) | Category_ID (pk) |
+----------------+--------------------+
| 1 | 1 | <-- APP_ID=1 belongs to both cat 1 & 2
| 1 | 2 |
| 2 | 2 |
| 3 | 2 |
| 4 | 1 |
| 5 | 1 |
| 6 | 1 |
+----------------+--------------------+

Merge and sort two mysql tables with different columns

Table 1: user posts
+----+----------+-------------+--------+------+------+---------------------+
| id | parentID | commenterID | userID | post | tags | date |
+----+----------+-------------+--------+------+------+---------------------+
| 1 | NULL | 1 | 2 | hi | NULL | 2013-08-01 16:24:49 |
+----+----------+-------------+--------+------+------+---------------------+
Table 2: blog posts
+----+------+------+----------+-------+------+-------+----------+---------------------+
| id | type | tags | username | title | body | views | comments | date |
+----+------+------+----------+-------+------+-------+----------+---------------------+
| 1 | post | tag1 | user1 | lol | test | 0 | 0 | 2013-08-29 21:58:52 |
+----+------+------+----------+-------+------+-------+----------+---------------------+
I am trying to merge these two tables and sort them by date. None of these tables have anything in common so I am afraid I cannot use JOIN.
Table 1 represents "user posts" and table 2 represents "blog posts". I am building a "stream" of information such as it displays in the user's feed any updates/new posts coming in.
Any help will be greatly appreciated.
Thank you

Categories