It is somewhat confusing. It looks as though topic_name relates to the sub_topic_id. Assuming this is the case, then I would use a single <ul> for the sub-topics rather than a <ul> for each sub-topic, with each <li> set as display: inline-block.
I’m wondering if this is more an HTML/CSS question than PHP…
I think I’d be playing with an INNER JOIN rather than running all those individual queries. Maybe something like
SELECT * from sub_topic inner join topic on topic.topic_id = sub_topic.topic_id order by topic.topic_id
then loop through the result set and throw a new HTML list every time the main topic_id changes. Obviously also only retrieve the columns required rather than all of them, but I can’t remember the syntax off the top of my head. Pseudo-code:
last-id = ""
run query
while there are results, get next result {
is the topic-id different to last-id?
yes {
if last-id <> "", close the previous list
show the topic header, open the list
}
show the sub-topic
last-id = topic-id
}
if last-id <> "", close the previous list
As for vertical vs. horizontal, that’s definitely HTML/ CSS rather than anything to do with the PHP code.
I didn’t get a sense of any sort of grouping from the original post, just an alphabetical sort. Perhaps that comes out of reading the PHP, but that’s not my forte.
You show an image of your intended result which looks to me like nothing more a single <ul>
Though the PHP code looks like it’s trying to make multiple nested <ul>s with some kind of hierarchy and structure.
What would be more helpful would be to see (at least a sample of) the intended HTML output illustrating the structure of the data.
Mabe we need to clarify exactly what the problem is we are trying to solve here.
It this meant to be a single list without any hierarchy or nesting, just split over several columns?
If that be the case then semantically it should simply be one single list, then use CSS columns as Chris suggests. That does make it a CSS/layout question.
Or it there meant to be some heirarchy and nesting to the list and you are struggling with the logic to produce that? Which would make it a PHP question?