Home | Store | osCommerce Tutorials

osCommerce Main Categories Content Customization Example

Example 4-1: Use an Inner Table to contain the osCommerce Main Categories Content


Using a table to contain the osCommerce Main Categories Content provide more customization possibilities. If you follow along the osCommerce tutorial, you should be able to write the codes of the Inner Table easily.

Here's the original 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']);
}

}

Here's the codes that use the Inner table to contain the osCommerce Main Categories Content. In order to be able to see the table effect, let's assign one pixel to the table border.

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

if ($counter == 1) {
echo "<table border=\"1\" width=\"100%\" cellspacing=\"0\" cellpadding=\"3\"
class=\"infoBoxContents\" >" . "\n";
}

// start a new row, therefore need to set $categories_string to empty
$categories_string = '';

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 . ')';
}
}

// comment the line break since data will print in row
// $categories_string .= '<br>';

// Use a row to display the $categories_string
echo "<tr><td>";
echo $categories_string;
echo "</td></tr>" . "\n";

if ($tree[$counter]['next_id'] != false) {
tep_show_category($tree[$counter]['next_id']);
} else { // Close the table if there is no more Categories
echo "</table>" . "\n";
}

}

The codes are very simple and we will not explain each line in details.

if ($counter == 1) {
echo "<table border=\"1\" width=\"100%\" cellspacing=\"0\" cellpadding=\"3\"
class=\"infoBoxContents\" >" . "\n";
}

// start a new row, therefore need to set $categories_string to empty
$categories_string = '';

Here's the result:

osCommerce Main Categories Customization example

osCommerce Main Categories Customization example

osCommerce Main Categories Customization example

Once the osCommerce Main Categories is contained within a table, we can play around with the customization much easier.