Zen Cart Logo
Forums / Templates, Stylesheets, Page Layout / Top Categories list under Product list

Top Categories list under Product list

Views: 1,787

Results 1 to 13 of 13
24 Mar 2014, 1:38 PM
#1
ivanc avatar

ivanc

New Zenner

Join Date:
Dec 2007
Posts:
54
Plugin Contributions:
0

Top Categories list under Product list

Hi!

I am trying to add a top categories list (the same as it appears on main page centerb) under product listing.
I believe it will make navigating my site much easier because I don't use sideboxes at all, so there isn't a categories list on any side.

I have tried adding the following code just before the code for featured, specials, etc. centerboxes:

  1. added following code from tpl_index_categories.php to tpl_index_product_list.php
<div class="centerColumn" id="indexCategories">
<div id="indexCategoriesMainContent" class="content"><?php
/**
 * require the html_define for the index/categories page
 */
  include($define_page);
?></div>

<?php
if (PRODUCT_LIST_CATEGORIES_IMAGE_STATUS_TOP == 'true') {
// categories_image
  if ($categories_image = zen_get_categories_image($current_category_id)) {
?>
<div id="categoryImgListing" class="categoryImg"><?php echo zen_image(DIR_WS_IMAGES . $categories_image, '', SUBCATEGORY_IMAGE_TOP_WIDTH, SUBCATEGORY_IMAGE_TOP_HEIGHT); ?></div>
<?php
  }
} // categories_image
?>

<?php
// categories_description
    if ($current_categories_description != '') {
?>
<div id="categoryDescription" class="catDescContent"><?php echo $current_categories_description;  ?></div>
<?php } // categories_description ?>
<!-- BOF: Display grid of available sub-categories, if any -->
<?php
  if (PRODUCT_LIST_CATEGORY_ROW_STATUS == 0) {
    // do nothing
  } else {
    // display subcategories
/**
 * require the code to display the sub-categories-grid, if any exist
 */
   require($template->get_template_dir('tpl_modules_category_row.php',DIR_WS_TEMPLATE, $current_page_base,'templates'). '/tpl_modules_category_row.php');
  }
?>
</div>
<!-- EOF: Display grid of available sub-categories -->
  1. added following code I found on the forum to tpl_index_product_list.php
<?php
$cat_id=0;
 
    $categories_query = "SELECT c.categories_id, cd.categories_name, c.categories_image, c.parent_id
                          FROM   " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd
                          WHERE      c.parent_id = :parentID
                          AND        c.categories_id = cd.categories_id
                          AND        cd.language_id = :languagesID
                          AND        c.categories_status= '1'
                          ORDER BY   sort_order, cd.categories_name";
  
    $categories_query = $db->bindVars($categories_query, ':parentID', $cat_id, 'integer');
    $categories_query = $db->bindVars($categories_query, ':languagesID', $_SESSION['languages_id'], 'integer');
    $categories = $db->Execute($categories_query);
    $number_of_categories = $categories->RecordCount();
  
 require($template->get_template_dir('tpl_modules_category_row.php',DIR_WS_TEMPLATE, $current_page_base,'templates'). '/tpl_modules_category_row.php');
?>
  1. tried also with these two lines
<?php require($template->get_template_dir('tpl_modules_category_row.php',DIR_WS_TEMPLATE, $current_page_base,'templates'). '/tpl_modules_category_row.php'); ?>
<?php require($template->get_template_dir('tpl_index_categories.php',DIR_WS_TEMPLATE, $current_page_base,'templates'). '/tpl_index_categories.php'); ?>

And neither works!
The product listing looks and works normal, completely ignoring any of my attempts.

I am sure there is a very simple solution to this issue, but I can't seem to find it.

Can anyone help?

Thanks!

24 Mar 2014, 1:46 PM
#2
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: Top Categories list under Product list

Is there a reason you don't want to use the categories-tabs menu bar in the header? You can even add a simple test to it to hide it on the homepage if you don't need it there. Seeing your site live will help us make appropriate suggestions.

24 Mar 2014, 2:03 PM
#3
ivanc avatar

ivanc

New Zenner

Join Date:
Dec 2007
Posts:
54
Plugin Contributions:
0

Re: Top Categories list under Product list

gjh42:

Is there a reason you don't want to use the categories-tabs menu bar in the header? You can even add a simple test to it to hide it on the homepage if you don't need it there. Seeing your site live will help us make appropriate suggestions.

Yes, there are two! :)

  1. my main page top categories list is heavily modified and I would like to use it if it's possible
  2. categories-tabs don't fit into my current design. I could make it work but that would require a significant redesign. Simply adding the existing categories under the product list is much simpler and quicker (well, at least - should be).

You can check out the website here: http://bit.ly/1dGPsur

Thank your extremely fast reply!

24 Mar 2014, 2:17 PM
#4
ivanc avatar

ivanc

New Zenner

Join Date:
Dec 2007
Posts:
54
Plugin Contributions:
0

Re: Top Categories list under Product list

ivanc:

Thank your extremely fast reply!

Thank you for your extremely fast reply! :)

24 Mar 2014, 2:57 PM
#5
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: Top Categories list under Product list

You don't want this bit in your product listing page, as $define_page will not be set for a product page. The include may not cause an error, but it will not do anything you want.```php

<div id="indexCategoriesMainContent" class="content"><?php /** * require the html_define for the index/categories page */ include($define_page); ?></div> ``` In what order did you add the code snippets to the file? The sql query must be run before output files are called, or there will be no content to output.
24 Mar 2014, 3:20 PM
#6
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: Top Categories list under Product list

Looking closer, you don't want any of your first block of code. The second block with the sql query will probably get the data (I haven't checked it thoroughly). You also don't want tpl_index_categories.php as that outputs a whole page content. You do want tpl_modules_category_row.php after the sql query is run and $categories is populated.

You don't need
$number_of_categories = $categories->RecordCount(); as a similar variable is set by category_row.php (which is called by tpl_modules_category_row.php).

24 Mar 2014, 3:24 PM
#7
ivanc avatar

ivanc

New Zenner

Join Date:
Dec 2007
Posts:
54
Plugin Contributions:
0

Re: Top Categories list under Product list

Sorry, didn't see your second post before replying, will check now!

24 Mar 2014, 3:26 PM
#8
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: Top Categories list under Product list

If you are still not getting results, you might try adding debug code, say after php $categories = $db->Execute($categories_query); like```php
echo ' $categories populated: ' . var_dump($categories);

24 Mar 2014, 3:56 PM
#9
ivanc avatar

ivanc

New Zenner

Join Date:
Dec 2007
Posts:
54
Plugin Contributions:
0

Re: Top Categories list under Product list

No luck, anything I try is completely ignored.

I am posting my complete tpl_index_product_list.php including the changes you suggested:

<?php
/**
 * Page Template
 *
 * Loaded by main_page=index<br />
 * Displays product-listing when a particular category/subcategory is selected for browsing
 *
 * @package templateSystem
 * @copyright Copyright 2003-2010 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: tpl_index_product_list.php 15589 2010-02-27 15:03:49Z ajeh $
 * modified 2012-06-26 by A. Sarfraz
 * modified 2012-09-22 & 2012-10-06 by Glenn Herbert (gjh42)
 */
?>
<div class="centerColumn" id="indexProductList">

<h1 id="productListHeading"><?php echo $breadcrumb->last(); ?></h1>

<?php
if (PRODUCT_LIST_CATEGORIES_IMAGE_STATUS == 'true') {
// categories_image
  if ($categories_image = zen_get_categories_image($current_category_id)) {
?>
<div id="categoryImgListing" class="categoryImg"><?php echo zen_image(DIR_WS_IMAGES . $categories_image, '', CATEGORY_ICON_IMAGE_WIDTH, CATEGORY_ICON_IMAGE_HEIGHT); ?></div>
<?php
  }
} // categories_image
?>

<?php
// categories_description
    if ($current_categories_description != '') {
?>
<div id="indexProductListCatDescription" class="content"><?php echo $current_categories_description;  ?></div>
<?php } // categories_description ?>

<?php
  $check_for_alpha = $listing_sql;
  $check_for_alpha = $db->Execute($check_for_alpha);

  if ($do_filter_list || ($check_for_alpha->RecordCount() > 0 && PRODUCT_LIST_ALPHA_SORTER == 'true') || (defined('PRODUCT_LISTING_LAYOUT_STYLE_CUSTOMER') and PRODUCT_LISTING_LAYOUT_STYLE_CUSTOMER == '1')) {//form if list/grid enabled
  $form = zen_draw_form('filter', zen_href_link(FILENAME_DEFAULT), 'get') . '<label class="inputLabel">' .TEXT_SHOW . '</label>';
?>

<?php
  echo $form;
  echo zen_draw_hidden_field('main_page', FILENAME_DEFAULT);
  echo zen_hide_session_id();
?>
<?php
  // draw cPath if known
  if (!$getoption_set) {
    echo zen_draw_hidden_field('cPath', $cPath);
  } else {
    // draw manufacturers_id
    echo zen_draw_hidden_field($get_option_variable, $_GET[$get_option_variable]);
  }

  // draw music_genre_id
  if (isset($_GET['music_genre_id']) && $_GET['music_genre_id'] != '') echo zen_draw_hidden_field('music_genre_id', $_GET['music_genre_id']);

  // draw record_company_id
  if (isset($_GET['record_company_id']) && $_GET['record_company_id'] != '') echo zen_draw_hidden_field('record_company_id', $_GET['record_company_id']);

  // draw typefilter
  if (isset($_GET['typefilter']) && $_GET['typefilter'] != '') echo zen_draw_hidden_field('typefilter', $_GET['typefilter']);

  // draw manufacturers_id if not already done earlier
  if ($get_option_variable != 'manufacturers_id' && isset($_GET['manufacturers_id']) && $_GET['manufacturers_id'] > 0) {
    echo zen_draw_hidden_field('manufacturers_id', $_GET['manufacturers_id']);
  }

  // draw sort
  echo zen_draw_hidden_field('sort', $_GET['sort']);

  // draw filter_id (ie: category/mfg depending on $options)
  if ($do_filter_list) {
    echo zen_draw_pull_down_menu('filter_id', $options, (isset($_GET['filter_id']) ? $_GET['filter_id'] : ''), 'onchange="this.form.submit()"');
  }
  
  if (defined('PRODUCT_LISTING_LAYOUT_STYLE_CUSTOMER') and PRODUCT_LISTING_LAYOUT_STYLE_CUSTOMER == '1') {
    echo '<div id="viewControl">' . zen_draw_pull_down_menu('view', array(array('id'=>'rows','text'=>PRODUCT_LISTING_LAYOUT_ROWS),array('id'=>'columns','text'=>PRODUCT_LISTING_LAYOUT_COLUMNS)), (isset($_GET['view']) ? $_GET['view'] : (defined('PRODUCT_LISTING_LAYOUT_STYLE')? PRODUCT_LISTING_LAYOUT_STYLE: 'rows')), 'onchange="this.form.submit()"') . '</div>';  
  }
  // draw alpha sorter
  require(DIR_WS_MODULES . zen_get_module_directory(FILENAME_PRODUCT_LISTING_ALPHA_SORTER));
?>
</form>
<?php
  }
?>
<br class="clearBoth" />

<?php
/**
 * require the code for listing products
 */
 require($template->get_template_dir('tpl_modules_product_listing.php', DIR_WS_TEMPLATE, $current_page_base,'templates'). '/' . 'tpl_modules_product_listing.php');
?>


<?php
//// bof: categories error
if ($error_categories==true) {
  // verify lost category and reset category
  $check_category = $db->Execute("select categories_id from " . TABLE_CATEGORIES . " where categories_id='" . $cPath . "'");
  if ($check_category->RecordCount() == 0) {
    $new_products_category_id = '0';
    $cPath= '';
  }
?>

<?php
$show_display_category = $db->Execute(SQL_SHOW_PRODUCT_INFO_MISSING);

while (!$show_display_category->EOF) {
?>

<!-- INSERT CATEGORIES HERE TOP -->
<?php 
$cat_id=0; 
  
    $categories_query = "SELECT c.categories_id, cd.categories_name, c.categories_image, c.parent_id 
                          FROM   " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd 
                          WHERE      c.parent_id = :parentID 
                          AND        c.categories_id = cd.categories_id 
                          AND        cd.language_id = :languagesID 
                          AND        c.categories_status= '1' 
                          ORDER BY   sort_order, cd.categories_name"; 
   
    $categories_query = $db->bindVars($categories_query, ':parentID', $cat_id, 'integer'); 
    $categories_query = $db->bindVars($categories_query, ':languagesID', $_SESSION['languages_id'], 'integer'); 
    $categories = $db->Execute($categories_query);
	echo ' $categories populated: ' . var_dump($categories);  
   
 require($template->get_template_dir('tpl_modules_category_row.php',DIR_WS_TEMPLATE, $current_page_base,'templates'). '/tpl_modules_category_row.php'); 
?>
<!-- INSERT CATEGORIES HERE BOTTOM -->

<?php
  if ($show_display_category->fields['configuration_key'] == 'SHOW_PRODUCT_INFO_MISSING_FEATURED_PRODUCTS') { ?>
<?php
/**
 * display the Featured Products Center Box
 */
?>
<?php require($template->get_template_dir('tpl_modules_featured_products.php',DIR_WS_TEMPLATE, $current_page_base,'templates'). '/tpl_modules_featured_products.php'); ?>
<?php } ?>

<?php
  if ($show_display_category->fields['configuration_key'] == 'SHOW_PRODUCT_INFO_MISSING_SPECIALS_PRODUCTS') { ?>
<?php
/**
 * display the Special Products Center Box
 */
?>
<?php require($template->get_template_dir('tpl_modules_specials_default.php',DIR_WS_TEMPLATE, $current_page_base,'templates'). '/tpl_modules_specials_default.php'); ?>
<?php } ?>

<?php
  if ($show_display_category->fields['configuration_key'] == 'SHOW_PRODUCT_INFO_MISSING_NEW_PRODUCTS') { ?>
<?php
/**
 * display the New Products Center Box
 */
?>
<?php require($template->get_template_dir('tpl_modules_whats_new.php',DIR_WS_TEMPLATE, $current_page_base,'templates'). '/tpl_modules_whats_new.php'); ?>
<?php } ?>

<?php
  if ($show_display_category->fields['configuration_key'] == 'SHOW_PRODUCT_INFO_MISSING_UPCOMING') {
    include(DIR_WS_MODULES . zen_get_module_directory(FILENAME_UPCOMING_PRODUCTS));
  }
?>
<?php
  $show_display_category->MoveNext();
} // !EOF
?>
<?php } //// eof: categories error ?>

<?php
//// bof: categories
$show_display_category = $db->Execute(SQL_SHOW_PRODUCT_INFO_LISTING_BELOW);
if ($error_categories == false and $show_display_category->RecordCount() > 0) {
?>

<?php
  $show_display_category = $db->Execute(SQL_SHOW_PRODUCT_INFO_LISTING_BELOW);
  while (!$show_display_category->EOF) {
?>

<?php
    if ($show_display_category->fields['configuration_key'] == 'SHOW_PRODUCT_INFO_LISTING_BELOW_FEATURED_PRODUCTS') { ?>
<?php
/**
 * display the Featured Products Center Box
 */
?>
<?php require($template->get_template_dir('tpl_modules_featured_products.php',DIR_WS_TEMPLATE, $current_page_base,'templates'). '/tpl_modules_featured_products.php'); ?>
<?php } ?>

<?php
    if ($show_display_category->fields['configuration_key'] == 'SHOW_PRODUCT_INFO_LISTING_BELOW_SPECIALS_PRODUCTS') { ?>
<?php
/**
 * display the Special Products Center Box
 */
?>
<?php require($template->get_template_dir('tpl_modules_specials_default.php',DIR_WS_TEMPLATE, $current_page_base,'templates'). '/tpl_modules_specials_default.php'); ?>
<?php } ?>

<?php
    if ($show_display_category->fields['configuration_key'] == 'SHOW_PRODUCT_INFO_LISTING_BELOW_NEW_PRODUCTS') { ?>
<?php
/**
 * display the New Products Center Box
 */
?>
<?php require($template->get_template_dir('tpl_modules_whats_new.php',DIR_WS_TEMPLATE, $current_page_base,'templates'). '/tpl_modules_whats_new.php'); ?>
<?php } ?>

<?php
    if ($show_display_category->fields['configuration_key'] == 'SHOW_PRODUCT_INFO_LISTING_BELOW_UPCOMING') {
      include(DIR_WS_MODULES . zen_get_module_directory(FILENAME_UPCOMING_PRODUCTS));
    }
?>
<?php
  $show_display_category->MoveNext();
  } // !EOF
?>

<?php
} //// eof: categories
?>

</div>

Perhaps my positioning is wrong?

ps. featured products on product listing will be turned off when (or IF) I get this working

24 Mar 2014, 5:02 PM
#10
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: Top Categories list under Product list

You have ```php

<?php $show_display_category = $db->Execute(SQL_SHOW_PRODUCT_INFO_MISSING); while (!$show_display_category->EOF) { ?> <!-- INSERT CATEGORIES HERE TOP -->
24 Mar 2014, 5:24 PM
#11
ivanc avatar

ivanc

New Zenner

Join Date:
Dec 2007
Posts:
54
Plugin Contributions:
0

Re: Top Categories list under Product list

I tried a little bit different approach and did it

Added this code to the END of the file (after the main /div): ==> which suggests that the loop was the problem, as you have noticed

<br class="clearBoth" />
<div id="indexCategories" class="centerColumn">
<?php 
$cat_id=0; 
  
    $categories_query = "SELECT c.categories_id, cd.categories_name, c.categories_image, c.parent_id 
                          FROM   " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd 
                          WHERE      c.parent_id = :parentID 
                          AND        c.categories_id = cd.categories_id 
                          AND        cd.language_id = :languagesID 
                          AND        c.categories_status= '1' 
                          ORDER BY   sort_order, cd.categories_name"; 
   
    $categories_query = $db->bindVars($categories_query, ':parentID', $cat_id, 'integer'); 
    $categories_query = $db->bindVars($categories_query, ':languagesID', $_SESSION['languages_id'], 'integer'); 
    $categories = $db->Execute($categories_query);
?>
<?php
/**
 * display the TOP CATEGORIES Center Box
 */
?>
<?php require($template->get_template_dir('tpl_modules_category_row.php',DIR_WS_TEMPLATE, $current_page_base,'templates'). '/tpl_modules_category_row.php'); ?>
</div>

And now an obvious question appeared - how do you exclude the top category you are currently in from appearing in the list?

I suppose the answer is here:

    $categories_query = "SELECT c.categories_id, cd.categories_name, c.categories_image, c.parent_id 

p.s. Glenn thank you so much for your help!

25 Mar 2014, 4:11 PM
#12
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: Top Categories list under Product list

I would adapt from the "where" clause in the query: ```php
WHERE c.parent_id = :parentID
AND c.categories_id = cd.categories_id
AND cd.language_id = :languagesID
AND c.categories_status= '1'
ORDER BY sort_order, cd.categories_name";

$categories_query = $db->bindVars($categories_query, ':parentID', $cat_id, 'integer'); 

The variable in this file that holds the id is $current_category_id, so:php
AND c.categories_id != :currentCatID

$categories_query = $db->bindVars($categories_query, ':currentCatID', $current_category_id, 'integer'); 
Check the SQL syntax for "not equal" - not certain offhand whether it is !=.
25 Mar 2014, 5:28 PM
#13
ivanc avatar

ivanc

New Zenner

Join Date:
Dec 2007
Posts:
54
Plugin Contributions:
0

Re: Top Categories list under Product list

It worked without problems, thank you again!

Here is the complete final code for anyone interested:

<div id="indexCategories" class="centerColumn">
<?php 
$cat_id=0; 
  
    $categories_query = "SELECT c.categories_id, cd.categories_name, c.categories_image, c.parent_id 
                          FROM   " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd 
                          WHERE      c.parent_id = :parentID 
                          AND        c.categories_id = cd.categories_id 
						  AND        c.categories_id != :currentCatID  
                          AND        cd.language_id = :languagesID 
                          AND        c.categories_status= '1' 
                          ORDER BY   sort_order, cd.categories_name"; 
   
    $categories_query = $db->bindVars($categories_query, ':parentID', $cat_id, 'integer'); 
    $categories_query = $db->bindVars($categories_query, ':currentCatID', $current_category_id, 'integer');  
    $categories_query = $db->bindVars($categories_query, ':languagesID', $_SESSION['languages_id'], 'integer'); 
    $categories = $db->Execute($categories_query);
?>
<?php
/**
 * display the TOP CATEGORIES Center Box
 */
?>
<?php require($template->get_template_dir('tpl_modules_category_row.php',DIR_WS_TEMPLATE, $current_page_base,'templates'). '/tpl_modules_category_row.php'); ?>
</div>

You need to insert it into tpl_index_product_list.php
The easiest way is to add it as is to the end of the file. You can omit the div's if you want for any reason.
If you have other centerboxes turned on (like Featured or Specials) you might want to position it before them, in which case you need to add the code right after these lines:

<?php
/**
 * require the code for listing products
 */
 require($template->get_template_dir('tpl_modules_product_listing.php', DIR_WS_TEMPLATE, $current_page_base,'templates'). '/' . 'tpl_modules_product_listing.php');
?>