I’m developing something like a simple and purpose-specific cms.
I’m using a db class that outputs an array as a result from the query, but this array varies in structure depending on the mysql_num_rows.
If there is only one result it outputs a simple array, otherwise it outputs a nested array.
So, I must check the structure of the array to decide whether to do a loop with foreach, or just print the results.
with simple mysql_fetch_assoc we can use while() and then there is no problem with the number of rows. but with an array we must check it.
Till now everything is OK.
The problem is that the code will be repeated for the two conditions. with a slight change in the array name.
The array name is $res, if one result it will be $res['anything here'], else, because we have to use foreach($res as $row), the array will be $row['anything here'].
So the code looks ugly and very big. Please take a lool at it here: http://mohamedkadri.com/code.html and tell me how to make it simpler and smaller.
The solution is simple:
Change your DB layer so you can force it to return a list of rows even if there’s just a single row. That’s the only proper solution.