I am hoping for some help making my queries more efficient if possible. I have two tables. One which contains the types of content available and another which contains content linked to the type table by a type id.
I am trying to select from the content table and group the results by type_id. I am currently using a query inside a loop to do this so it is selecting again from the second table for each type_id. Is there a better/more efficient way to do this?
Here are my current tables and queries:
type_id type
1 type 1
2 type 2
id title type_id content
1 test1 1 test content 1
2 test2 2 test content 2
3 test3 1 test content 3
$query="select type_id, type from type_table";
foreach($query as $type){
$query="select title, content from content_table WHERE type_id=$type['type_id']";
}
I hope this makes sense.
Thanks
Your example appears to select all possible values from the content table, albeit in batches of a given type at a time. So
select title, content from content_tablewould surely do this whole thing in one go, without looping.To put each type_id into a new div, you could do something like this:
(edited based on comments)