<?
$cats = mysql_query("SELECT id,name from `cats` order by `id` desc " );
while($cat = mysql_fetch_array($cats)) {
$file = mysql_query("SELECT id,name FROM `files` where `id_cat` = '".$cat['id']."' ORDER BY rand() DESC LIMIT 6 " );
while($f= mysql_fetch_array($file)) {
echo $f['name'];
}
}