I have following code:
SELECT q21, q21coding AS Description FROM `tresults_acme` WHERE q21 IS NOT NULL AND q21 <> '' ORDER BY q21coding
It brings back the following (excerpt):
Text Description
Lack of up to date equal pay cases&legislation - t... Content needs updating
The intranet could contain more "up to date traini... Content needs updating
Poorly set out. It is hard to find things. Difficulty in navigating/finding content
Only use the intranet as a necessity. Will ask my ... Difficulty in navigating/finding content
Now, I’d like to display this in a table on a PHP page but am having some problems because of the way I’d like it displayed, it needs to be as follows:
Content needs updating
----------------------
[List all the comments relating to this description]
Difficulty in navigating/finding content
----------------------------------------
[List all the comments relating to this description]
and so on.
Now I think it is a For Each loop in PHP but I am having terrible difficulty getting my head around this – any ideas and suggestions very very welcome!
Thanks,
Simple approach
prev_desctoNULLtextdescriptionis not equal toprev_descprepend with the description for the new “section” and setprev_desc <- descriptionE.g.1 (untested!),
Note: You must keep the
ORDER BY <description-column>in order to have rows “grouped”. Otherwise this simple approach will not work.Less presentation-specific approach
I could be considered more “clean” to create some kind of 2D container to “categorize” the extracted data, e.g.,
You could then loop over these items like so1:
1 As @bobince has noted, make sure that content going directly into the final HTML is properly escaped, see e.g.
htmlspecialchars().