I was just wondering whether I am indeed doing something wrong. It just seems both approaches are not very clean. Either way it's not too hard to do, but I'm just thinking there should be a better way.
Description of Problem:
You have a relational database design where you want to query 2 tables.
Say table A & B.
For each row in table A there are three rows in table B connected to it. For instance every Company (table A) has three Documents (Table 😎.
There are two messy solutions which i know of.
1-Query the two tables with a join. Then in php while reading the result read only the first part of the joint row(A,😎 if it changes and always read the second part of the row.
2-First query the company table. Store the companies IDs in an array. Query the document tables with the company IDs in the where query and store the outcome in an array such as $document[companyid][0].
First approach is messy because of the required if statements and reading the first part of row(A,😎 3 times when we only need it once.
The second approach is messy because we query twice when we actually only need it once.
Probably there is a third approach which is a lot cleaner to code and more efficient. At least that's what i hope🙂
Cheers,
Thierry