Browsing Categories

Locked
Results 1 to 9 of 9
This thread is locked. New replies are disabled.
03 May 2007, 22:11
#1
tameboy avatar

tameboy

New Zenner

Join Date:
May 2007
Posts:
6
Plugin Contributions:
0

Browsing Categories

Is there an easy way to set up a navigation bar to browse categories? I would want this to appear above the category listing - in a similar way to the one that appears above the product listing, so you can click on the next and previous categories.

I have searched the admin facility, wiki and this forum and don't seem to be able to find reference to such a feature. Is that because there isn't one??

Many thanks for any help.
03 May 2007, 22:30
#2
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Posts:
21,876
Plugin Contributions:
6

Re: Browsing Categories

How many categories do you have? Dozens? Unless you have too many to fit across the top of your page, you can use the Categories-Tabs menu in a similar fashion.

If you must have a prev/next function, you will need to code it. Probably best to start by looking at how the product prev/next functions.
04 May 2007, 07:57
#3
tameboy avatar

tameboy

New Zenner

Join Date:
May 2007
Posts:
6
Plugin Contributions:
0

Re: Browsing Categories

Many thanks for the info. There wont be that many categories, so perhaps I will look at the categories tabs. It was actually a specific request from a client.

I may also consider coding it.

Thanks again.
13 May 2007, 08:14
#4
tameboy avatar

tameboy

New Zenner

Join Date:
May 2007
Posts:
6
Plugin Contributions:
0

Re: Browsing Categories

I ended up coding my own solution for this issue. I've copied the code below. I saved it as tpl_categories_next_previous.php in my installation and called it from tpl_index_product_list.php in the same way tpl_products_next_previous.php is called from tpl_product_info_display.php in the standard installation.

I needed a quick solution here (site needs to be online by the end of May) so I apologise if this code doesn't make used of existing database classes and global variables within zen cart, I just didn't have the time to work out how to access these. Perhaps when I am more familiar with Zen internal workings I will improve this code and upload it as a plugin?

Anyway it works very well for my site, I have a happy client and am on schedule for the end of May. Here it is for anyone who might make use of it...


[php]<?php
## my plugin : does next /previous for categories
## current category / top cat
$tame_cats = explode("_",$_GET['cPath']);
$tame_top_cat = $tame_cats[0];
$tame_this_cat = $tame_cats[1];
$query = "SELECT * FROM my_mysql_database.zen_categories where parent_ID=$tame_top_cat and categories_status = 1 order by sort_order";
$result = mysql_query($query);
$i = 0;
$tame_cat_data = array();
while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
foreach($row as $key => $value) {
$tame_cat_data[$key][$i] = $value;
}
$i++;
}
foreach($tame_cat_data['categories_id'] as $key => $value) {
if($value = $tame_this_cat) $tame_this_key = $key; ## so we know which cat we have
}
$tame_last_cat_key = $key;
## do prev cat
if($tame_cat_data['categories_id'][0] != $tame_this_cat) { ## ie don't do it if it is the first cat
$tame_prev = $tame_this_cat - 1;
}
## do next cat
if($tame_cat_data['categories_id'][$tame_last_cat_key] != $tame_this_cat) { ## ie don't do it if it is the last cat
$tame_next = $tame_this_cat + 1;
}
?>
<div class="navNextPrevWrapper centeredContent">

<? if($tame_prev) {
?><div class="navNextPrevList"><a href="index.php?main_page=index&cPath=<? echo $tame_top_cat."_".$tame_prev; ?>"><span class="cssButton button_prev" onmouseover="this.className='cssButtonHover button_prev button_prevHover'" onmouseout="this.className='cssButton button_prev'" style="width: 80px;"> Previous </span></a></div><?
}?>

<div class="navNextPrevList"><a href="index.php?main_page=index&cPath=<? echo $tame_top_cat; ?>"><span class="cssButton button_prev" onmouseover="this.className='cssButtonHover button_prev button_prevHover'" onmouseout="this.className='cssButton button_prev'" >Spring Summer 2007 Catalogue</span></a></div>
<?
if($tame_next) {
?><div class="navNextPrevList"><a href="index.php?main_page=index&cPath=<? echo $tame_top_cat."_".$tame_next; ?>"><span class="cssButton button_next" onmouseover="this.className='cssButtonHover button_next button_nextHover'" onmouseout="this.className='cssButton button_next'" style="width: 80px;"> Next </span></a></div><?
}
?>
</div>[/php]
13 May 2007, 09:42
#5
tameboy avatar

tameboy

New Zenner

Join Date:
May 2007
Posts:
6
Plugin Contributions:
0

Re: Browsing Categories

Just added some improved error handling and other bits which make it work better...

[php]<?php
## my plugin : does next /previous for categories
## current category / top cat
$tame_cats = explode("_",$_GET['cPath']);
$tame_parent_cat = $tame_cats[0];
$tame_this_cat = $tame_cats[1];
## check parent cat exists and get name:
$query = "select zen_categories_description.categories_name from my_database.zen_categories_description,my_database.zen_categories where zen_categories_description.categories_id = relaxshoe.zen_categories.categories_id and relaxshoe.zen_categories.categories_status = 1 AND zen_categories.categories_id = $tame_parent_cat";
$result = mysql_query($query);
$tame_numrows = mysql_num_rows($result);
if(!($tame_numrows > 0 && !mysql_error())) exit ("Error finding category");
$row = mysql_fetch_array($result,MYSQL_ASSOC);
$tame_parent_cat_name = $row['categories_name'];
## check child cat exits
$query = "SELECT * FROM my_database.zen_categories where parent_ID=$tame_parent_cat and categories_status = 1 and categories_id=$tame_this_cat order by sort_order";
$result = mysql_query($query);
$tame_numrows = mysql_num_rows($result);
if(!($tame_numrows > 0 && !mysql_error())) exit ("Error finding category");
## get active ranges for this parent cat...
$query = "SELECT * FROM my_database.zen_categories where parent_ID=$tame_parent_cat and categories_status = 1 order by sort_order";
$result = mysql_query($query);
$tame_numrows = mysql_num_rows($result);
$i = 0;
if(!($tame_numrows > 0 && !mysql_error())) exit ("Error finding category");
## now we can go ahead:
$tame_cat_data = array();
while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
foreach($row as $key => $value) {
$tame_cat_data[$key][$i] = $value;
}
$i++;
}
foreach($tame_cat_data['categories_id'] as $key => $value) {
if($value = $tame_this_cat) $tame_this_key = $key; ## so we know which cat we have
}
$tame_last_cat_key = $key;
## do prev cat
if($tame_cat_data['categories_id'][0] != $tame_this_cat) { ## ie don't do it if it is the first cat
$tame_prev = $tame_this_cat - 1;
}
## do next cat
if($tame_cat_data['categories_id'][$tame_last_cat_key] != $tame_this_cat) { ## ie don't do it if it is the last cat
$tame_next = $tame_this_cat + 1;
}
?><div class="navNextPrevWrapper centeredContent"><?
if($tame_prev) {
?><div class="navNextPrevList"><a href="index.php?main_page=index&cPath=<? echo $tame_parent_cat."_".$tame_prev; ?>"><span class="cssButton button_prev" onmouseover="this.className='cssButtonHover button_prev button_prevHover'" onmouseout="this.className='cssButton button_prev'" style="width: 80px;"> Previous </span></a></div><?
}
?><div class="navNextPrevList"><a href="index.php?main_page=index&cPath=<? echo $tame_parent_cat; ?>"><span class="cssButton button_prev" onmouseover="this.className='cssButtonHover button_prev button_prevHover'" onmouseout="this.className='cssButton button_prev'" ><? echo $tame_parent_cat_name;?></span></a></div><?
if($tame_next) {
?><div class="navNextPrevList"><a href="index.php?main_page=index&cPath=<? echo $tame_parent_cat."_".$tame_next; ?>"><span class="cssButton button_next" onmouseover="this.className='cssButtonHover button_next button_nextHover'" onmouseout="this.className='cssButton button_next'" style="width: 80px;"> Next </span></a></div><?
}
?></div>[/php]
25 Jan 2008, 19:37
#6
manage_it avatar

manage_it

Inactive

Join Date:
May 2006
Posts:
80
Plugin Contributions:
0

Re: Browsing Categories

Could you please upload this as a plugin?
I really would like to use it.

Regards,

Bram
04 Feb 2008, 17:16
#7
tameboy avatar

tameboy

New Zenner

Join Date:
May 2007
Posts:
6
Plugin Contributions:
0

Re: Browsing Categories

Hi Bram

I am not sure I am likely to be in a position to upload this as a plugin in the near future. However, I will try and help you implement the code on your site.

Lets see if I can give you some more specific instructions. but NB please back up all your files before making any of these changes so if there are any problems you can just restore the original files. I will not be held responsible for breaking your site!

First of all you need to have two files in your site.co.uk/includes/templates/my_template/templates
directory:

tpl_index_product_list.php and
tpl_categories_next_previous.php

where my_template is the name of the template your site is currently using - it might be "classic" if you are using the standard installation template.

If these files are not there do this:

1) copy the standard version of tpl_index_product_list.php from the default_template directory:
site.co.uk/includes/templates/default_templates/templates
and put it in to
site.co.uk/includes/templates/my_template/templates

2) create a new file called tpl_categories_next_previous.php and put it in the directory as above. Cut and paste the code from my second code posting into this new file and save it.

Now you need to make some changes to tpl_index_product_list.php in order for it to call the code in tpl_categories_next_previous.php:

In the file includes/templates/my_template/templates/tpl_index_product_list.php
go to line 85 or there abouts and you should see code that looks like this:

[php]// 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()"');
}

// draw alpha sorter
require(DIR_WS_MODULES . zen_get_module_directory(FILENAME_PRODUCT_LISTING_ALPHA_SORTER));
?>
</form>
<?php
}
?>

<?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]You need to insert this code

[php]<!--bof tame_cat prev next -->
<?php require($template->get_template_dir('/tpl_categories_next_previous.php',DIR_WS_TEMPLATE, $current_page_base,'templates'). '/tpl_categories_next_previous.php'); ?>
<!--eof tame_cat prev next -->[/php]so you end up with

[php]// draw alpha sorter
require(DIR_WS_MODULES . zen_get_module_directory(FILENAME_PRODUCT_LISTING_ALPHA_SORTER));
?>
</form>
<?php
}
?>

<!--bof tame_cat prev next -->
<?php require($template->get_template_dir('/tpl_categories_next_previous.php',DIR_WS_TEMPLATE, $current_page_base,'templates'). '/tpl_categories_next_previous.php'); ?>
<!--eof tame_cat prev next -->
<?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]You should then see the navigation facility appear on your product listing page.

Let me know how you get on.
04 Feb 2008, 18:50
#8
manage_it avatar

manage_it

Inactive

Join Date:
May 2006
Posts:
80
Plugin Contributions:
0

Re: Browsing Categories

I get the following error message.
I think the code in tpl_categories_next_previous.php is not entirely correct

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /var/www/vhosts/atisco-developments.nl/httpdocs/includes/templates/template_default/templates/tpl_categories_next_previous.php on line 17
Error finding category

Regards,

Bram
05 Feb 2008, 21:15
#9
tameboy avatar

tameboy

New Zenner

Join Date:
May 2007
Posts:
6
Plugin Contributions:
0

Re: Browsing Categories

Hi Bram

Sorry, I should have mentioned that you will need to change the 3 mysql queries in the code to match your database. So in line 15:

[php]$query = "SELECT * FROM my_database.zen_categories where parent_ID=$tame_parent_cat and categories_status = 1 and categories_id=$tame_this_cat order by sort_order";[/php]you change my_database to the name of your MySQL database. This might resolve the error you had. There are queries on lines 8,15 and 20 where you will need to replace every incidence of my_database with the name of your MySQL database.

Let me know if the problem persists beyond this. If it does it would be very helpful to know what you get if you insert this line after line 15:

[php]exit("$query");[/php]Obviously that would be something you would only do briefly - record the result and then delete the inserted line again.