Jump to content

Query to multi dimensional array


liam1412

Recommended Posts

Hi

 

I am trying to streamline my coding practices and am looking at OOP/MVC

 

If I have 3 tables

 

Categories

Forums

Posts

 

I could acheive a forum structure quite easily by getting the categories the searching for all forums with that catid and then searching for all posts with that forumid. 

 

Obviously this is very clumsy and hard to implement in MVC

 

I need a way to get all this in a multi dimensional array with as few queries as possible. Please go easy on me.  I am very new to this.  I have been using php for some time, but only at a basic level.

 

The array would need to hold

 

A list of categories > within each category it would need to hold a list of forums that have that catid > then I would need a post/thread count using each forumid.

 

can I do this with joins and if so How do I do it without having sub queries in my view.

 

Thanks again

 

Link to comment
https://forums.phpfreaks.com/topic/172764-query-to-multi-dimensional-array/
Share on other sites

You could query for subcategories.. or.. 'forums' and then in the query run a subquery to gather the catid's name counterpart.. e.g. "SELECT (SELECT name FROM categories WHERE id = catid) As categoryName"

 

so the entire query will look something like this..

 

 

"SELECT (SELECT COUNT(*) FROM Posts WHERE forumid = Forums.id) As posts, (SELECT name FROM Categories WHERE id = Forums.catid) As categoryName FROM Forums"

 

THAN you loop through them.. seperate them by categoryName.. and then put them into their places.. Goodluck

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.