I want to output the category names, the subcategory names in each category, and the articles in each category and in each subcategory.
The output should be in <ul> and inner <ul> tags in HTML.
Here is how the output should look like:
I have these 5 tables:
table_article_categories(articlecat_id, parent_id, articlecat_status, ...)
table_article_to_article_categories(articlecat_id, articles_id)
table_articles(articles_id, articles_status, ...)
table_articles_description(articles_id, language_id, articles_heading_title, ...)
table_article_categories_description(articlecat_id, articlecat_name, language_id, ...)
I have made this SQL so far:
SELECT
ac.parent_id,
ac.articlecat_id,
a.articles_id,
ad.articles_heading_title AS article_name,
acd.articlecat_name
FROM
table_article_categories AS ac
LEFT JOIN
table_article_to_article_categories AS atac
ON
atac.articlecat_id = ac.articlecat_id
LEFT JOIN
table_articles AS a
ON
a.articles_id = atac.articles_id
AND
a.articles_status = 1
LEFT JOIN
table_articles_description AS ad
ON
ad.articles_id = a.articles_id
AND
ad.language_id = 1
INNER JOIN
table_article_categories_description AS acd
ON
acd.articlecat_id = ac.articlecat_id
AND
acd.language_id = 1
WHERE
ac.parent_id = 99 # --- This is the root article-categori-id ---
AND
ac.articlecat_status = 1
AND
ac.articlecat_id != 77 # --- This is an excluded article-categori-id ---
ORDER BY
acd.articlecat_name ASC,
article_name ASC

Instead of doing it in a single query, I would suggest a solution of looping over separate, multiple queries.
For your case, only 3 separate queries are needed because you have 3-tier (Cat/Subcat/Article). On the other hand, since the number of tiers is already defined (and limited), there is no need to use recursion this time (if there are unlimited (or unknown) levels of hierarchy, we need recursion).
Please change the database schema to a proper, and clearly defined one (as follows):
where
catid is linking to category; andsubcatid is linking to subcategory, if applicableSo the data will be (refering to the picture in your post)
I think the advantage and clarity is obvious in the above schema, so I don't think further explanation is needed. Of course you may add further data fields to the tables but the main structure is as above.
Now, please use PHP to generate the following HTML:
For 1st ul, li (just run once)
(a) loop over category and display the name
then
(b) loop over article display records having (catid=0 or catid is null) and (subcatid=0 or subcatid is null) , then display the name
2nd ul,li
For each of the records in (a) above:
(c) loop over subcategory which as catid=category.catid and display the subcategory name
then
(d) loop over article, display records having catid=category.catid and (subcatid=0 or subcatid is null) , then display the name
3rd ul,li
For each of the records in (c) above:
(e) loop over article, display records having catid=category.catid and subcatid=subcategory.subcatid , then display the name
To demonstrate, I will put the "2nd ul, li" as a function known as branch2(); and "3rd ul, li" as a function known as branch3(), and then call them accordingly.
So, the code will be:
The fully working example can be seen here:
http://www.createchhk.com/SO/testso10Nov2021.php