Home | Store | osCommerce Tutorials

osCommerce Main Categories Content File Structure

Part 6 - How the codes of tep_show_category() function works


In this osCommerce tutorial, we will study how the codes of tep_show_category() function working that listing out all items of the osCommerce Main Categories.

We noticed that the Main Categories Tree or Listing is the most commonly customized item. After you know how the codes works, you should be able to customize in whatever result you like.

Here's the codes of tep_show_category() function:

function tep_show_category($counter) {
global $tree, $categories_string, $cPath_array;

for ($i=0; $i<$tree[$counter]['level']; $i++) {
$categories_string .= "&nbsp;&nbsp;";
}

$categories_string .= '<a href="';

if ($tree[$counter]['parent'] == 0) {
$cPath_new = 'cPath=' . $counter;
} else {
$cPath_new = 'cPath=' . $tree[$counter]['path'];
}

$categories_string .= tep_href_link(FILENAME_DEFAULT, $cPath_new) . '">';

if (isset($cPath_array) && in_array($counter, $cPath_array)) {
$categories_string .= '<b>';
}

// display category name
$categories_string .= $tree[$counter]['name'];

if (isset($cPath_array) && in_array($counter, $cPath_array)) {
$categories_string .= '</b>';
}

if (tep_has_category_subcategories($counter)) {
$categories_string .= '-&gt;';
}

$categories_string .= '</a>';

if (SHOW_COUNTS == 'true') {
$products_in_category = tep_count_products_in_category($counter);
if ($products_in_category > 0) {
$categories_string .= '&nbsp;(' . $products_in_category . ')';
}
}

$categories_string .= '<br>';

if ($tree[$counter]['next_id'] != false) {
tep_show_category($tree[$counter]['next_id']);
}

}

Let's go through the codes briefy. We will use browsing Hardware Parent Categories as an example when illustration of the result of the codes are required.

Remember that the $categories_string is empty at the start of calling the tep_show_category() function. The $categories_string will concatenate other strings as the codes are running.

Here's the first three lines of codes at the start of tep_show_category() function.

for ($i=0; $i<$tree[$counter]['level']; $i++) {
$categories_string .= "&nbsp;&nbsp;";
}

If this is a Sub-categories, indent two character spaces. For example:

<a href="http://osc.cz.cc/index.php?cPath=1"><b>Hardware</b>-&gt;</a>&nbsp;(6)<br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_17">CDROM Drives</a><br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_4">Graphics Cards</a>&nbsp;(2)<br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_8">Keyboards</a>&nbsp;(1)<br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_16">Memory</a><br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_9">Mice</a>&nbsp;(2)<br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_6">Monitors</a><br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_5">Printers</a>&nbsp;(1)<br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_7">Speakers</a><br>
<a href="http://osc.cz.cc/index.php?cPath=2">Software-&gt;</a>&nbsp;(4)<br>
<a href="http://osc.cz.cc/index.php?cPath=3">DVD Movies-&gt;</a>&nbsp;(17)<br>

Here's how the codes display in webpage. Note that the Sub-categories (e.g. CDROM Drives, Graphics Cards, Keyboards, Memory, etc...) are indent two character spaces.

Hardware-> (6)
  CDROM Drives
  Graphics Cards (2)
  Keyboards (1)
  Memory
  Mice (2)
  Monitors
  Printers (1)
  Speakers
Software-> (4)
DVD Movies-> (17)

The codes continue:

$categories_string .= '<a href="';

This line of codes will concatenate an anchor tag to the $categories_string.

<a href="http://osc.cz.cc/index.php?cPath=1"><b>Hardware</b>-&gt;</a>&nbsp;(6)<br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_17">CDROM Drives</a><br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_4">Graphics Cards</a>&nbsp;(2)<br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_8">Keyboards</a>&nbsp;(1)<br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_16">Memory</a><br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_9">Mice</a>&nbsp;(2)<br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_6">Monitors</a><br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_5">Printers</a>&nbsp;(1)<br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_7">Speakers</a><br>
<a href="http://osc.cz.cc/index.php?cPath=2">Software-&gt;</a>&nbsp;(4)<br>
<a href="http://osc.cz.cc/index.php?cPath=3">DVD Movies-&gt;</a>&nbsp;(17)<br>

Next codes are:

if ($tree[$counter]['parent'] == 0) {
$cPath_new = 'cPath=' . $counter;
} else {
$cPath_new = 'cPath=' . $tree[$counter]['path'];
}

If this is Parent Categories, the $cPath_new value is 'cPath=' . $counter.
See the illustration below with fonts highlight in blue color.

If this is Sub-Categories, the $cPath_new value is 'cPath=' . $tree[$counter]['path'].
See the illustration below with fonts highlight in red color.

<a href="http://osc.cz.cc/index.php?cPath=1"><b>Hardware</b>-&gt;</a>&nbsp;(6)<br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_17">CDROM Drives</a><br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_4">Graphics Cards</a>&nbsp;(2)<br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_8">Keyboards</a>&nbsp;(1)<br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_16">Memory</a><br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_9">Mice</a>&nbsp;(2)<br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_6">Monitors</a><br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_5">Printers</a>&nbsp;(1)<br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_7">Speakers</a><br>
<a href="http://osc.cz.cc/index.php?cPath=2">Software-&gt;</a>&nbsp;(4)<br>
<a href="http://osc.cz.cc/index.php?cPath=3">DVD Movies-&gt;</a>&nbsp;(17)<br>

Next codes is:

$categories_string .= tep_href_link(FILENAME_DEFAULT, $cPath_new) . '">';

We learned the tep_href_link a lot before. It simply attached the FILE_DEFAULT and $cPath_new together.

<a href="http://osc.cz.cc/index.php?cPath=1"><b>Hardware</b>-&gt;</a>&nbsp;(6)<br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_17">CDROM Drives</a><br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_4">Graphics Cards</a>&nbsp;(2)<br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_8">Keyboards</a>&nbsp;(1)<br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_16">Memory</a><br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_9">Mice</a>&nbsp;(2)<br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_6">Monitors</a><br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_5">Printers</a>&nbsp;(1)<br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_7">Speakers</a><br>
<a href="http://osc.cz.cc/index.php?cPath=2">Software-&gt;</a>&nbsp;(4)<br>
<a href="http://osc.cz.cc/index.php?cPath=3">DVD Movies-&gt;</a>&nbsp;(17)<br>

Next three lines of codes are:

if (isset($cPath_array) && in_array($counter, $cPath_array)) {
$categories_string .= '<b>';
}

// display category name
$categories_string .= $tree[$counter]['name'];

if (isset($cPath_array) && in_array($counter, $cPath_array)) {
$categories_string .= '</b>';
}

1. If this is the browsing $cPath, then add a BOLD <b> tag.

2. Display the Categories Name.

3. If this is the browsing $cPath, then add a closing BOLD </b> tag.

<a href="http://osc.cz.cc/index.php?cPath=1"><b>Hardware</b>-&gt;</a>&nbsp;(6)<br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_17">CDROM Drives</a><br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_4">Graphics Cards</a>&nbsp;(2)<br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_8">Keyboards</a>&nbsp;(1)<br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_16">Memory</a><br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_9">Mice</a>&nbsp;(2)<br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_6">Monitors</a><br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_5">Printers</a>&nbsp;(1)<br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_7">Speakers</a><br>
<a href="http://osc.cz.cc/index.php?cPath=2">Software-&gt;</a>&nbsp;(4)<br>
<a href="http://osc.cz.cc/index.php?cPath=3">DVD Movies-&gt;</a>&nbsp;(17)<br>

Next codes are:

if (tep_has_category_subcategories($counter)) {
$categories_string .= '-&gt;';
}

$categories_string .= '</a>';

if (SHOW_COUNTS == 'true') {
$products_in_category = tep_count_products_in_category($counter);
if ($products_in_category > 0) {
$categories_string .= '&nbsp;(' . $products_in_category . ')';
}
}

1. If the Categories has Sub-Categories, display the arrow "->" marking. The HTML code of "->" is "'-&gt;". See the illustration below with fonts in BOLD.

2. Closing the hyperText link with </a> tag. See the illustration below with fonts highlight in red color.

<a href="http://osc.cz.cc/index.php?cPath=1"><b>Hardware</b>-&gt;</a>&nbsp;(6)<br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_17">CDROM Drives</a><br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_4">Graphics Cards</a>&nbsp;(2)<br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_8">Keyboards</a>&nbsp;(1)<br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_16">Memory</a><br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_9">Mice</a>&nbsp;(2)<br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_6">Monitors</a><br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_5">Printers</a>&nbsp;(1)<br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_7">Speakers</a><br>
<a href="http://osc.cz.cc/index.php?cPath=2">Software-&gt;</a>&nbsp;(4)<br>
<a href="http://osc.cz.cc/index.php?cPath=3">DVD Movies-&gt;</a>&nbsp;(17)<br>

Another lines of codes follow are:

if (SHOW_COUNTS == 'true') {
$products_in_category = tep_count_products_in_category($counter);
if ($products_in_category > 0) {
$categories_string .= '&nbsp;(' . $products_in_category . ')';
}
}

If SHOW_COUNTS is equal to TRUE the category count will be shown.

The category count is shown in the picture below:

osCommerce category count SHOW_COUNTS

The SHOW_COUNTS can be set to TRUE or FALSE in osCommerce Admin area.

osCommerce category count SHOW_COUNTS

If SHOW_COUNTS is set to FALSE, the category count will not be shown. The picture below shows the effect when the category count is not shown:

osCommerce category count SHOW_COUNTS

Note that it will leave the ugly "->" marks on all Parent Categories. You just learned how the "->" mark was added. Therefore you should know how to remove them.

Note:
- An easier way to remove the category count is remove comment the above 6 lines of codes.
- Comment the three lines to remove the ugly "->" marks
.

The next line of code is:

$categories_string .= '<br>';

This line of code will add a line break HTML <br> code to the $categories_string variable.

Actually this is also the end of individual Categories.

<a href="http://osc.cz.cc/index.php?cPath=1"><b>Hardware</b>-&gt;</a>&nbsp;(6)<br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_17">CDROM Drives</a><br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_4">Graphics Cards</a>&nbsp;(2)<br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_8">Keyboards</a>&nbsp;(1)<br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_16">Memory</a><br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_9">Mice</a>&nbsp;(2)<br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_6">Monitors</a><br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_5">Printers</a>&nbsp;(1)<br>
&nbsp;&nbsp;<a href="http://osc.cz.cc/index.php?cPath=1_7">Speakers</a><br>
<a href="http://osc.cz.cc/index.php?cPath=2">Software-&gt;</a>&nbsp;(4)<br>
<a href="http://osc.cz.cc/index.php?cPath=3">DVD Movies-&gt;</a>&nbsp;(17)<br>

Then it comes to the last codes:

if ($tree[$counter]['next_id'] != false) {
tep_show_category($tree[$counter]['next_id']);
}

If the value of next_id is not empty, the tep_show_category() function will be run again. Note that the $categories_string is not empty at this line. Another line of string will be concatenated to the $categories_string.

The tep_show_category() function will keep on running over again until the value of next_id is empty. Please see previous tutorial how Main Categories is listing.

Now you know how the codes are working. You can customize the look and feel of the osCommerce Main Categories easily.