<?
$sql = <<< TXT
SELECT
cats.id as catsid, prod.id as prodid, cats.cat_name, prod.product_name, vars.variant_name
FROM категории cats, товары prod, варианты vars
WHERE
cats.id = prod.cat_id
AND prod.id = vars.product_id
ORDER BY cats.id, prod.id
TXT;
...
$cat_id = -1;
$prod_id = -1;
foreach( ... as $line){
if($cat_id != $line['catsid']){
$cat_id = $line['catsid'];
$prod_id = -1;
echo $line['cat_name'], "\n";
}
if($prod_id != $line['prodid']){
$prod_id = $line['prodid'];
echo "\t", $line['product_name'], "\n";
}
echo "\t\t", $line['variant_name'], "\n";
}