[eluser]jonez[/eluser]
Instead of doing 20 queries individually do them all as one query (group by) then delegate the response array. As CroNiX said indices will help a lot and foreign key's should be on all your join columns.
Your query should return something like this (16/12 are ex. category ID's);
Code:
1 - 16 - News - Post 1
2 - 16 - News - Post 2
3 - 16 - News - Post 3
4 - 12 - Sports - Post 1
5 - 12 - Sports - Post 2
6 - 12 - Sports - Post 3
...
Loop through it and push records into groups;
Code:
$records = $this->db->query( $sql, $params )->result_array( );
$results = array( );
foreach ( $records as $record ) {
if ( !array_key_exists( $record[ 'cat_id' ], $results ) ) {
$results[ $record[ 'cat_id' ] ] = array( );
}
$results[ $record[ 'cat_id' ][ ] = $record;
}