First, to get your categories...
If your category nesting is only two levels deep (parent and child) or you are only interested in direct sub-categories (i.e. not subcategories of subcategories), then your SQL is pretty simple:
SELECT cat_id FROM category_table WHERE cat_id = $catid OR parent_id = $catid
If your nesting is deeper, then you'll need to recursively query as far down as your category nesting goes. For example, if you limit yourself to 3 levels, you can do this with SQL like this:
SELECT cat_id FROM category_table ct1
WHERE cat_id = $catid OR parent_id = $catid
OR parent_id in (SELECT cat_id FROM category_table WHERE parent_id = ct1.cat_id)
Similarly, for a 4-level deep nesting, you can make the SQL even more complicated:
SELECT cat_id FROM category_table ct1
WHERE cat_id = $catid OR parent_id = $catid
OR parent_id in (SELECT cat_id FROM category_table ct2 WHERE parent_id = ct1.cat_id
OR parent_id in
(SELECT cat_id FROM category_table ct3 WHERE parent_id = ct2.cat_id))
And so on.
If you do not have a fixed nesting depth, then you'll need to do the looping in the code.
Once you have all categories, then you query articles. So for a simple 2-level dependency, you'll get
SELECT * FROM article_table
WHERE cat_id in (SELECT cat_id FROM category_table WHERE cat_id = $catid OR parent_id = $catid)
Similarly, for a 3-level nesting, you'll get
SELECT * FROM article_table
WHERE cat_id in
(SELECT cat_id FROM category_table ct1
WHERE cat_id = $catid OR parent_id = $catid
OR parent_id in (SELECT cat_id FROM category_table WHERE parent_id = ct1.cat_id))
And so on...
parent_id
there, which, I assume, will benull
for top level categories and contain the parent category id otherwise. – Aleks G$query = SELECT a.art_whatever FROM articles a, categories c WHERE c.cat_id = a.cat_id AND c.parent_id = '$cat'";
– DaOgre