I'm trying to modify category_row.php so that any products in bottom level categories will show up like this:

--------Sub Category ---------
--Sub1--
All products
--Sub 2--
All products and on for as many bottom level categories and products there are.

So far i have the following code:
PHP Code:
/**
 * index category_row.php
 *
 * Prepares the content for displaying a category's sub-category listing in grid format.  
 * Once the data is prepared, it calls the standard tpl_list_box_content template for display.
 *
 * @package page
 * @copyright Copyright 2003-2006 Zen Cart Development Team
 * @copyright Portions Copyright 2003 osCommerce
 * @license [url]http://www.zen-cart.com/license/2_0.txt[/url] GNU Public License V2.0
 * @version $Id: category_row.php 4084 2006-08-06 23:59:36Z drbyte $
 */
if (!defined('IS_ADMIN_FLAG')) {
  die(
'Illegal Access');
}
$title '';
$num_categories $categories->RecordCount();
$row 0;
$col 0;
$list_box_contents '';
if (
$num_categories 0) {
  if (
$num_categories MAX_DISPLAY_CATEGORIES_PER_ROW || MAX_DISPLAY_CATEGORIES_PER_ROW == 0) {
    
$col_width floor(100/$num_categories);
  } else {
    
$col_width floor(100/MAX_DISPLAY_CATEGORIES_PER_ROW);
  }
  while (!
$categories->EOF) {
    if (!
$categories->fields['categories_image']) !$categories->fields['categories_image'] = 'pixel_trans.gif';
    
$cPath_new zen_get_path($categories->fields['categories_id']);
    
// strip out 0_ from top level cats
    
$cPath_new str_replace('=0_''='$cPath_new);
    
            
// *** SET VARIABLE FOR PARENT CATEGORY NUMBER - DISPLAY ALL PRODUCTS IN SUB CATEGORIES MOD  ***
      
$parent $categories->fields['parent_id'];
    
//  echo ' parent id is ' .  $parent; //                        ************ TESTING USE ONLY *************** REMOVE
             // ***END SET VARIABLE FOR PARENT CATEGORY NUMBER ***    
      
//   $categories->fields['products_name'] = zen_get_products_name($categories->fields['products_id']);
    
        
if ($num_categories >1) { // *** SKIP QUERY IF CATEGORY DOES NOT HAVE SUB CATEGORIES - DISPLAY ALL PRODUCTS IN SUB CATEGORIES MOD***
            
        
        // *** COLLECT CATEGORY ID'S RELEVANT TO PARENT ID - DISPLAY ALL PRODUCTS IN SUB CATEGORIES MOD ***
$base_level_query "select pc.categories_id    
                            from " 
TABLE_CATEGORIES " pc
                            where pc.parent_id = 
$parent";
                            
                            
$base_level $db->Execute($base_level_query);
                            while (!
$base_level->EOF){
                            
$bottom_category=$base_level->fields['categories_id'];
                            
$base_level->MoveNext();
        
// *** END COLLECT CATEGORY ID'S RELEVANT TO PARENT ID ***



        // *** COLLECT PRODUCT DATA STORED IN LOWEST LEVEL CATEGORIES - DISPLAY ALL PRODUCTS IN SUB CATEGORIES MOD ***                        
$subcategories_products_query =  "select pc.categories_id, pa.products_id, p.products_id, p.products_image, p.products_tax_class_id, pd.products_name,
                                p.products_date_added, p.products_price, p.products_type, p.master_categories_id
                                  from " 
TABLE_PRODUCTS_TO_CATEGORIES " pa,  " TABLE_PRODUCTS " p, "  TABLE_PRODUCTS_DESCRIPTION " pd, " TABLE_CATEGORIES " pc
                           where pc.categories_id = pa.categories_id
                           and p.products_id = pa.products_id
                           and p.products_id = pd.products_id
                           and pd.language_id = '" 
. (int)$_SESSION['languages_id'] . "'
                           and pc.categories_id = 
$bottom_category";              
                         
        
$subcategories_products $db->Execute$subcategories_products_query);
        
        
// *** COLLECT PRODUCT DATA STORED IN LOWEST LEVEL CATEGORIES

 
while (!$subcategories_products->EOF) {

        
// ***OUTPUT DATA COLLECTED IN $subcategories_products_query - DISPLAY ALL PRODUCTS IN SUB CATEGORIES MOD ***
        
//print_r($subcategories_products);                                        ************ TESTING USE ONLY *************** REMOVE    ******THIS IS POPULATED******
        

     // ***************** NOTHING ENTERED HERE GENERATES OUTPUT TO SCREEN.
    
        
$list_box_contents[$row][$col]['params'] = 'class="categoryListBoxContents"' ' ' 'style="width:' $col_width '%;"';
                                       
    
$list_box_contents[$row][$col]['text'] = '<a href="' zen_href_link(FILENAME_DEFAULT$cPath_new) . '">' zen_image(DIR_WS_IMAGES $subcategories_products->fields['products_image'], $subcategories_products->fields['products_name'], SUBCATEGORY_IMAGE_WIDTHSUBCATEGORY_IMAGE_HEIGHT) . '</a>'
    
$subcategories_products->MoveNext();
}
  }
   
$list_box_contents[$row][$col] = array('params' => 'class="categoryListBoxContents"' ' ' 'style="width:' $col_width '%;"',
                                          
'text' => '<a href="' zen_href_link(FILENAME_DEFAULT$cPath_new) . '">' zen_image(DIR_WS_IMAGES $categories->fields['categories_image'], $categories->fields['categories_name'], SUBCATEGORY_IMAGE_WIDTHSUBCATEGORY_IMAGE_HEIGHT) . '<br />' $categories->fields['categories_name'] . '</a>');
}
    
$col ++;
    if (
$col > (MAX_DISPLAY_CATEGORIES_PER_ROW -1)) {
      
$col 0;
      
$row ++;
    }
    
$categories->MoveNext();
  }
}    
?> 
It will display products in single level categories, it will display all sub categories under a category, but no matter what i try, i cannot get it to display products in the deepest level.

The sql query works because a print_r($variable); shows the data held from the query. If i echo a field, the data is also there, but for some reason i just can't format it to display using $list_box_contents.

Can anyone spot the error i have in here? It's obviously not far off as i have done the hardest part, which is getting the required data from the db...

I've just spent too long looking at it and maybe i overlooked something.

Thanks for any help