Hi there,

Im making some changes to category_row.php so that i can change the way sub categories and their products are displayed.

Instead of having sub categories show up, and then the user clicks the sub category to see products, i have modified it to show sub category name, then all products, then next subcategory name, followed by all products.

The problem i have is that i can't use $list_box_contents to output the array because it will always display at the number of columns set in admin.
So, if the number of columns is set to 4 and there are only 3 products, the first product from the next sub category will sit along with the first three.

If i print_r the array it displays exactly as i want, with heading then products, then next heading and so forth.

If i just echo the output instead of using $list_box_contents it will display the products as needed, except that they are all bunched up to the left of the page and will fill the entire page width if sufficient products.
I tried setting columns in that and met the same problem as using list box contents, in that the output will always be four wide.

I did work out a way to have $row increment by one and $col reset to zero on each sub category change, but i couldn't get it to display the sub category name between each set of products doing it this way.

If i tried to work with the echo, i need to have a count on the number of products so that it will automatically move onto the next row once all products in a category have been output. I used $num_products = $subcategories_products->RecordCount(); which returns the correct values, but when i call $num_products it will for instance give 3 3 3 if there are three products. This is because $num_products has to be called after the " while (!$subcategories_products->EOF) { "

Sorry its so long winded.

So in brief, i need to get a column display of the array that will break and move to the next row once all products in the current category have been output.



Thanks for any help.

Code below:

<?php
/**
* 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 http://www.zen-cart.com/license/2_0.txt 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 CATEGORY AND PARENT VARIABLES - DISPLAY ALL PRODUCTS IN SUB CATEGORIES MOD ***
$category = $categories->fields['parent_id'];

$parent_level_query = "select pc.parent_id
from " . TABLE_CATEGORIES . " pc
where pc.categories_id = $category";

$parent_level = $db->Execute($parent_level_query);
$parent = $parent_level->fields['parent_id'];

// *** END SET CATEGORY AND PARENT VARIABLES - DISPLAY ALL PRODUCTS IN SUB CATEGORIES MOD ***

if ($parent == 0){ // *** FOLLOW EXISTING CODE IF AT TOP LEVEL - DISPLAY ALL PRODUCTS IN SUB CATEGORIES MOD ***

// $categories->fields['products_name'] = zen_get_products_name($categories->fields['products_id']);

$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_WIDTH, SUBCATEGORY_IMAGE_HEIGHT) . '<br />' . $categories->fields['categories_name'] . '</a>');

$col ++;
if ($col > (MAX_DISPLAY_CATEGORIES_PER_ROW -1)) {
$col = 0;
$row ++;
}
$categories->MoveNext();

}else{
// *** FOLLOW NEW CODE IF NOT AT TOP LEVEL - DISPLAY ALL PRODUCTS IN SUB CATEGORIES MOD ***


// *** COLLECT LOWEST LEVEL CATEGORY ID'S - DISPLAY ALL PRODUCTS IN SUB CATEGORIES MOD ***

$base_level_query = "select pc.categories_id
from " . TABLE_CATEGORIES . " pc
where pc.parent_id = $category";

$base_level = $db->Execute($base_level_query);
while (!$base_level->EOF){
$bottom_category=$base_level->fields['categories_id'];
$base_level->MoveNext();


// *** END COLLECT LOWEST LEVEL CATEGORY ID'S - DISPLAY ALL PRODUCTS IN SUB CATEGORIES MOD ***


// *** COLLECT CATEGORY NAMES FOR LOWEST LEVEL CATEGORY ID'S - DISPLAY ALL PRODUCTS IN SUB CATEGORIES MOD ***

$cat_name_query = "select pf.categories_name
from ". TABLE_CATEGORIES_DESCRIPTION . " pf
where pf.categories_id = ' " . $bottom_category ." '
and pf.language_id = '" . (int)$_SESSION['languages_id'] . "'";

$cat_name = $db->Execute($cat_name_query);
while (!$cat_name->EOF){
$category_name = $cat_name->fields['categories_name'];

?>
<div id="categoryDescription" class="catDescContent" align="center"><?php echo $category_name; ?></div>
<?php

// *** END COLLECT CATEGORY NAMES FOR LOWEST LEVEL CATEGORY ID'S - DISPLAY ALL PRODUCTS IN SUB CATEGORIES MOD ***


// *** 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_CATEGORIES . " pc, " . TABLE_PRODUCTS_TO_CATEGORIES . " pa, " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd
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);

while (!$subcategories_products->EOF) {

// ********************* FORMAT OUTPUT START ****************************

echo '<a href="' . zen_href_link(zen_get_info_page($subcategories_products->fields['products_id']), 'cPath=' . (($_GET['manufacturers_id'] > 0 and $_GET['filter_id']) > 0 ? zen_get_generated_category_path_rev($_GET['filter_id']) : ($_GET['cPath'] > 0 ? zen_get_generated_category_path_rev($_GET['cPath']) : zen_get_generated_category_path_rev($subcategories_products->fields['master_categories_id']))) . '&products_id=' . $subcategories_products->fields['products_id']) . '">' . zen_image(DIR_WS_IMAGES . $subcategories_products->fields['products_image'], $subcategories_products->fields['products_name'], IMAGE_PRODUCT_LISTING_WIDTH, IMAGE_PRODUCT_LISTING_HEIGHT, 'class="listingProductImage"') . '</a>';


// ********************* FORMAT OUTPUT END ****************************
$subcategories_products->MoveNext();
}
$categories->MoveNext();
$cat_name->MoveNext();
}
}
}
}
}

?>