Trying to get the Parent ID of the Parent ID - php

I am working on an e-commerce site from scratch using PHP and MYSQL. I have one table for categories, with a column for ID and a column for Parent_ID.
When displaying the products on categories.php, I have it set to display products where the Parent_ID OR the ID equals the $_GET['id'] field. But now I've run into a problem.
In my "Groceries" category, I have the "Cleaning & Home" subcategory, and under "Cleaning & Home" I have several categories like "Laundry", "Bathroom Supplies", etc.
My problem is that products in the third level don't display in the "Groceries" category, because technically the parent ID of "Laundry" is "Cleaning & Home". There will never be more than three levels (Parent, child, grandchild), but I would like categories in the grandchild level to also display in the parent level.
I've tried looking through MYSQL documentation and on other forums but so far no luck.

This requires a couple of joins to get to the top parent:
select c.*,
coalesce(cp2.id, cp.id, p.id) as MostParentId
from categories c left outer join
categories cp
on c.parent_Id = cp.id left outer join
categories cp2
on cp.parent_id = cp2.parent_id
where c.id = $_GET['id'] or cp.id = $_GET['id'] or cp2.id = $_GET['id'];
You can then compare the ids using or for a match to the parent category, subcategory, or whatever.

Related

How to build an admin with multi categories in PHP and MySQL?

I'd like to build an administration page for my website, where I could use multiple checkboxes to store / updates many categories for a single post.
So I've created three tables:
POST Table (id (index, auto-increment, not null), subject, content)
CATEGORIES Table (id (index, auto-increment, not null), name)
POST_CATEGORIES Table (id (index, auto-increment, not null), id_cat (foreign key to CATEGORIES.id), id_post (foreign key to POST.id))
(This last table is building relations between the posts and its categories.)
So I'm trying to list the categories with multiple checkboxes. I'm doing this query, but it doesn't work as expected:
$select = $db->query('SELECT categories.id, categories.name, post_categories.id_cat AS fk_cat, post_categories.id_post AS fk_post, post.id AS post_id
FROM `categories`
LEFT JOIN post_categories
ON categories.id = id_cat
LEFT JOIN posts
ON post.id = id_post');
Result is i have a list of categories, but each category name is repeated depending on the number of posts having this category (e.g : if post 1 and post 2 have both category 1, category 1 will appear 2 times in my list).
EDIT :
Thanks for the replies, here is a sqlfiddle to see what's happening...
http://sqlfiddle.com/#!9/4c304/1
(I'm trying to display a list of categories. I won't select * from categories table because in a next step, i'd like the right categories to be checked when i edit a post.)
SELECT DISTINCT c.id
, c.name
, p.id post_id
FROM categories c
LEFT
JOIN post_categories pc
ON c.id = pc.id_cat
LEFT
JOIN posts p
ON p.id = pc.id_post
If i use GROUP BY categories.id, i don't have doubles anymore...
But it only keeps the first post which is "tagged" with the current category. I need to keep all those informations.
Maybe i should filter by a php query? Something like if count(category.id > 1) then only display the one where post_id = current post edit? (sorry if it's not clear, i feel a bit confuses :( )

Joining three MS SQL Tables in PHP to display random products

I am trying to have a section in a shop I am creating to display 5 random products, from that Category ID.
Firstly every category has an ID, and a set of Sub Catrgories, every Sub Cat has an ID, and within every Sub Cat is a number of products. Every Product also has an ID.
The Products table contains the ProductID and the SubCatID.
The SubCat Table Contains the SubCatID and The CatID
The Cat table contains only the CatID.
SO I need to display 5 random products by the CatID. I can get random products using a query similar to this:
$randomprod = mssql_query("SELECT TOP 5* FROM Products WHERE SubCatID = '1' ORDER BY NEWID()");
while ($echorand = mssql_fetch_array($randomprod)) {
I need a way to join the tables so I can display all products under a certain CatID however, and am finding it difficult because my Products table doesn't contain a CatID. I am aware there are a number of joins, but am fairly new to PHP and even newer to MS SQL. Can anyone tell me what join is best, or point me in the correct direction please?
Join products to sub categories, join sub categories to categories, like this:
SELECT TOP 5 Products.Name, SubCatergory.Name, Category.Name
FROM Products
INNER JOIN SubCatergory ON Products.SubCatID = SubCatergory.SubCatID
INNER JOIN Catergory ON SubCatergory.CatID = Category.CatID
WHERE Category.CatID = 1
ORDER BY NEWID()
I have used INNER JOIN in the above example.

Retrieving and Displaying Categories and their children count

I am using PHP via Codeigniter for this scenario.
It's a basic category listing that has items assigned with to those categories. What I'm having problem with is showing the item count beside the category names on the category listing page.
How I was doing it was a very ineffective way i think, and I would really like to know what would be the common practice for this sort of thing.
So I was getting the category list in my controller, and then in my view, on the loop thru the categories, I call a model method to grab the count of the respective cat_id on every iteration. (horror!)
Should I be:
A) Getting the categories AND it's item count from 1 SQL statement
OR
B) Process the category list in the controller and get the categories' corresponding item counts in the controller
If so, how to ?
I would prefer to have one query with everything I need, if your DB design allow to do so.
Assuming you have a Categories table and a Products table you should be able to do something like this:
Select C.Category, Count(P.Id) as ProductsCount
From Categories C left join Products P on C.Id = P.Category_Id
Group by C.Category
Order by C.Category

PHP how to display rows from two tables

I have two tables namely 'categories' & 'products' as follows:
categories
id | name
products
id | cat_id | name
cat_id in products references the categories table.
Now, I want to display the list of all products on a page, which will have to also display the name of the category the product belongs to(not the cat_id).
I am very new in PHP, so am doing the following way:
foreach($products as $product):
echo $product->id . $product->name;
echo getCategoryName($product->cat_id);
endforeach;
Its obvious that the database call to retrieve the corresponding category name is equal to the number of products to be displayed, and something tells me this is not good at all.
So, is there a better way to do the above? maybe by using SQL JOIN?
You should use SQL join, which is by far the best and easiest way to select data from two tables.
SELECT
p.id AS id, c.name AS category, p.name AS name
FROM
products AS p LEFT JOIN categories AS c ON p.cat_id = c.id
You can also use the statement by Trevor, but that's a different type of join. Both will work in this case, as long as the product ID always exist.
Update: A little update on LEFT JOIN and RIGHT JOIN. When you select from 2 tables, you have a left and right table (products is left here, categories is right). The query I gave you will match all products even if the category does not exist. A RIGHT JOIN would match all categories even if there are no products in that category.
Yeah, you'll need a sql join. Something like this:
categories id | name
products id | cat_id | name
select categories.name, products.id, products.name
from categories, products
where categories.id = products.cat_id

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

Categories