PHP echo repeating rows only once

HeroXx

New Member
I have 2 tables. One table is 'headings' & contains Headings and the other one is 'sub-headings' and contains Sub-headings that are (& must be) classified under the Headings and must have a respective Heading. A Sub-Heading can have only one Heading and many sub-headings can have the same Heading. So consider the following format:\[code\]--Table headingheading_id heading1 H12 H23 H3--Table sub_headingsub_head_id sub_heading heading_id1 SH1 12 SH2 13 SH3 24 SH4 35 SH5 26 SH6 4\[/code\]What I would like to do is query the database only once and get all sub_headings and their corresponding headings (using Inner Join?) and output a Heading only once and list all the sub_headings listed under their respective Heading. So, in practice, I would like to show up the OUTPUT RESULT AS:\[code\]H1--SH1--SH2H2--SH3--SH5H3--SH4H4--SH6\[/code\]As you can see, the Headings are echo'ed only once and all the sub-headings that belong to a Heading are nicely shown under their respective Heading. So I would like to know what query I would write to do so.NOTE: I have already written a code to fetch heading_id and while doing so in a loop, query the database for the respective sub-headings. This again results in loop of the sub_headings. But (I feel) the drawback in this is that for every single Heading that exists, the DB will be queried at least once to get their corresponding sub_headings. So if 50 Headings exist, while fetching them in a loop, a 2nd query is done to get the sub-headings. This needs me to write down a second query to get the sub-headings and process them in a loop. I want to avoid this.So it would really help if anyone can tell me how to query the DB only once and output the result as I have indicated above. Thanks in advance.
 
Back
Top