Category display logic or looking for a better way to write the code

Hello,

I have a script similar to a classified script. The script works but part of me things there’s a better way to write the logic. so, basically looking for a new idea or a better way to handle the logic.

looking at the example below:
if a user clicks the buy & sell category, any posting/listing under buy & sell category should appear this includes sports, golf and baseball.
if a user clicks sports, everything under the category sports should appear this includes everything under golf and baseball.
if a user clicks baseball, the script should list everything under baseball nothing else.

example category layout

  • Buy & Sell
    ---- Sports
    -------- Golf
    -------- Baseball

question, what’s a better way to select everything under “buy and sell” if the user clicks on “buy and sell” if “golf” and “baseball” has sports as its parent category?

tbl_category
id | parent | category name
20 | 0 | buy & sell
25 | 20 | art
34 | 20 | sports and goods
141 | 34 | golf
156 | 34 | baseball

Tbl_postings
parent | description
141 | selling golf bag one
156 | selling bat
25 | selling pic

same array queried from tbl_category
[140] => Array
(
[node_inx] => 141
[node_name] => golf
[node_parent] => 34
[up1_inx] => 34
[up1_name] => sports goods
[up1_parent] => 20
[up2_inx] => 20
[up2_name] => buy & sell
[up2_parent] => 0
[up3_inx] =>
[up3_name] =>
[up3_parent] =>
)