If you want to group the results by category. Try pulling all the results and then building an array of the categories. Then using this array to loop through each of the categories.
It's quite an intensive PHP method, there maybe a better way using MySQL to do it.
//do your query
$query = mysql_query("SELECT id, name, category_id FROM omc_product");
//INIT CATEGORIES AND IMAGES ARRAY
$cateogriesPresent = array();
$images = array();
//LOOP TRHOUGH RESULTS
while ($row=mysql_fetch_assoc($query)) {
//BUILD ARRAY OF IMAGES
$images[] = array('name' => $row['name'], 'category' => $row['id'], 'category' => $row['category_id']);
//ADD EACH UNIQUE CATEGORY TO CATEGORY ARRAY
if(!in_array($row['category_id'], $cateogriesPresent)){
array_push($cateogriesPresent, $row['category_id']);
}
}
//IMAGES BY CAT
foreach ($cateogriesPresent as $cat){
//ECHO HTML FOR EACH CAT HERE
echo "";
foreach ($images as $image){
if($image['category_id'] == $cat){
//ECHO YOUR HTML HERE FOR EACH IMAGE
echo ""
}
echo "";
}
}