Let's say you've got a table like this:
ID Cat1 Cat2
1 a red
2 b red
3 c blue
4 d blue
5 e blue
6 f green
etc etc etc
The goal is to display the ID and Cat1 (unique pairs), split into groups according to Cat2. The easy way out is to run a separate MySQL query for each Cat2, but with a lot of different Cat2 values that produces an order of magnitude more queries to display one page to the viewer than just grabbing the whole lot of data in one query and slicing it apart with PHP.
I've got my formatting finished. But I'm having a hard time figuring out how to limit my row output based on the third column value in the array $value[2]
taken from $value = mysql_fetch_row($result)
after using
$sql = "SELECT ID,Cat1,Cat2 FROM MyTable ORDER BY Cat2,ID"
In plain English (clearly NOT real code) it would look like:
$result = mysql_query($sql);
IF ($value[2] from $value = mysql_fetch_row($result) = "red")
{
echo "Red Group";
WHILE ()
{
echo $value[0] and $value[1] for all the rows where Cat2 = red
}
}
IF ($value[2] from $value = mysql_fetch_row($result) = "blue")
{
echo "Blue Group";
WHILE ()
{
echo $value[0] and $value[1] for all the rows where Cat2 = blue
}
}
etc
Does all that make sense? How do you grab those groups of data out of the $result
array?
Apparently is does NOT all make sense! :-) Here’s an example of desired output:
RED GROUP
1 a
2 b
BLUE GROUP
3 c
4 d
5 e
GREEN GROUP
6 f
etc
Thank you!