Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP echo repeating rows only once
    primarykey
    data
    text
    <p>I have 2 tables. One table is 'headings' &amp; contains Headings and the other one is 'sub-headings' and contains Sub-headings that are (&amp; 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:</p> <pre><code>--Table heading heading_id heading 1 H1 2 H2 3 H3 --Table sub_heading sub_head_id sub_heading heading_id 1 SH1 1 2 SH2 1 3 SH3 2 4 SH4 3 5 SH5 2 6 SH6 4 </code></pre> <p>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 <strong>OUTPUT RESULT AS</strong>:</p> <pre><code>H1 --SH1 --SH2 H2 --SH3 --SH5 H3 --SH4 H4 --SH6 </code></pre> <p>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.</p> <p><strong>NOTE:</strong> 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.</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload