Home | Store | osCommerce Tutorials

osCommerce Navigate Through Categories - Categories Content Code Study

Part 1 - Remove all un-used osCommerce codes


The osCommerce Categories Content is immediate below the Categories Header.

osCommerce Categories Content

osCommerce categories content

Codes for the Categories Contents

The codes below are used to generate the Categories Content:

<?php

require('includes/application_top.php');

if (isset($cPath) && strpos('_', $cPath)) {
// check to see if there are deeper categories within the current category
$category_links = array_reverse($cPath_array);
for($i=0, $n=sizeof($category_links); $i<$n; $i++) {
$categories_query = tep_db_query("select count(*) as total from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.parent_id = '" . (int)$category_links[$i] . "' and c.categories_id = cd.categories_id and cd.language_id = '" . (int)$languages_id . "'");
$categories = tep_db_fetch_array($categories_query);
if ($categories['total'] < 1) {
// do nothing, go through the loop
} else {
$categories_query = tep_db_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 = '" . (int)$category_links[$i] . "' and c.categories_id = cd.categories_id and cd.language_id = '" . (int)$languages_id . "' order by sort_order, cd.categories_name");
break; // we've found the deepest category the customer is in
}
}

} else {
$categories_query = tep_db_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 = '" . (int)$current_category_id . "' and c.categories_id = cd.categories_id and cd.language_id = '" . (int)$languages_id . "' order by sort_order, cd.categories_name");
}

$number_of_categories = tep_db_num_rows($categories_query);

$rows = 0;
while ($categories = tep_db_fetch_array($categories_query)) {
$rows++;
$cPath_new = tep_get_path($categories['categories_id']);
$width = (int)(100 / MAX_DISPLAY_CATEGORIES_PER_ROW) . '%';
echo ' <td align="center" class="smallText" width="' . $width . '" valign="top"><a href="' . tep_href_link(FILENAME_DEFAULT, $cPath_new) . '">' . tep_image(DIR_WS_IMAGES . $categories['categories_image'], $categories['categories_name'], SUBCATEGORY_IMAGE_WIDTH, SUBCATEGORY_IMAGE_HEIGHT) . '<br>' . $categories['categories_name'] . '</a></td>' . "\n";
if ((($rows / MAX_DISPLAY_CATEGORIES_PER_ROW) == floor($rows / MAX_DISPLAY_CATEGORIES_PER_ROW)) && ($rows != $number_of_categories)) {
echo ' </tr>' . "\n";
echo ' <tr>' . "\n";
}
}

// needed for the new products module shown below
$new_products_category_id = $current_category_id;

?>

PHP strpos() function

You may read strpos as String Position. The PHP strpos() function simply:

  • find position of first occurrence of a string
  • returns the position as an integer if found
  • return FALSE if not found

Let's see how the codes work.

strpos('_', $cPath)

  • If the $cPath variable contain "_", it will return TRUE.
  • If the $cPath variable do NOT contain "_", it will return FALSE.

Therefore:

if ( strpos('_', $cPath) ) {

// if $cPath variable contain "_", codes here will be executed
----- codes here -----

}

Let's review the value of $cPath that we learned before:

Source Link and cPath $cPath Value
Home Page http://osc.cz.cc/ empty
Hardware http://osc.cz.cc/index.php?cPath=1 1
CDROM Drives http://osc.cz.cc/index.php?cPath=1_17 1_17
Graphics Cards http://osc.cz.cc/index.php?cPath=1_4 1_4
Keyboards http://osc.cz.cc/index.php?cPath=1_8 1_8
Memory http://osc.cz.cc/index.php?cPath=1_16 1_16
Mice http://osc.cz.cc/index.php?cPath=1_9 1_9
Monitors http://osc.cz.cc/index.php?cPath=1_6 1_6
Printers http://osc.cz.cc/index.php?cPath=1_5 1_5
Speakers http://osc.cz.cc/index.php?cPath=1_7 1_7
Software http://osc.cz.cc/index.php?cPath=2 2
Action http://osc.cz.cc/index.php?cPath=2_19 2_19
Simulation http://osc.cz.cc/index.php?cPath=2_18 2_18
Strategy http://osc.cz.cc/index.php?cPath=2_20 2_20
DVD Movies http://osc.cz.cc/index.php?cPath=3 3
Action http://osc.cz.cc/index.php?cPath=3_10 3_10
Cartoons  http://osc.cz.cc/index.php?cPath=3_13 3_13
Comedy http://osc.cz.cc/index.php?cPath=3_12 3_12
Drama http://osc.cz.cc/index.php?cPath=3_15 3_15
Science Fiction http://osc.cz.cc/index.php?cPath=3_11 3_11
Thriller  http://osc.cz.cc/index.php?cPath=3_14 3_14

Since visitors are visiting the Parent Categories (Hardware, Software and DVD Movies), the $cPath values are 1, 2 and 3 respectively. In other words, the $cPath value will not contain the "_" of the default osCommerce shop.

Therefore we can ignore the codes in green fonts within the curly braces of if ( strpos('_', $cPath) ). Then, the above codes can be simplified as below:

<?php

require('includes/application_top.php');

$categories_query =
tep_db_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 = '" .
(int)$current_category_id . "' and c.categories_id = cd.categories_id and cd.language_id = '" .
(int)$languages_id . "' order by sort_order, cd.categories_name");

$number_of_categories = tep_db_num_rows($categories_query);

$rows = 0;
while ($categories = tep_db_fetch_array($categories_query)) {
$rows++;
$cPath_new = tep_get_path($categories['categories_id']);
$width = (int)(100 / MAX_DISPLAY_CATEGORIES_PER_ROW) . '%';
echo ' <td align="center" class="smallText" width="' . $width . '" valign="top"><a href="' . tep_href_link(FILENAME_DEFAULT, $cPath_new) . '">' . tep_image(DIR_WS_IMAGES . $categories['categories_image'], $categories['categories_name'], SUBCATEGORY_IMAGE_WIDTH, SUBCATEGORY_IMAGE_HEIGHT) . '<br>' . $categories['categories_name'] . '</a></td>' . "\n";
if ((($rows / MAX_DISPLAY_CATEGORIES_PER_ROW) == floor($rows / MAX_DISPLAY_CATEGORIES_PER_ROW)) && ($rows != $number_of_categories)) {
echo ' </tr>' . "\n";
echo ' <tr>' . "\n";
}
}

// needed for the new products module shown below
$new_products_category_id = $current_category_id;

?>

Moreover the codes needed for the new products module shown below will not be used to generate the Categories Content. This two lines can also be removed.

Therefore the final codes will becomes:

<?php

require('includes/application_top.php');

$categories_query =
tep_db_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 = '" .
(int)$current_category_id . "' and c.categories_id = cd.categories_id and cd.language_id = '" .
(int)$languages_id . "' order by sort_order, cd.categories_name");

$number_of_categories = tep_db_num_rows($categories_query);

$rows = 0;
while ($categories = tep_db_fetch_array($categories_query)) {
$rows++;
$cPath_new = tep_get_path($categories['categories_id']);
$width = (int)(100 / MAX_DISPLAY_CATEGORIES_PER_ROW) . '%';
echo ' <td align="center" class="smallText" width="' . $width . '" valign="top"><a href="' . tep_href_link(FILENAME_DEFAULT, $cPath_new) . '">' . tep_image(DIR_WS_IMAGES . $categories['categories_image'], $categories['categories_name'], SUBCATEGORY_IMAGE_WIDTH, SUBCATEGORY_IMAGE_HEIGHT) . '<br>' . $categories['categories_name'] . '</a></td>' . "\n";
if ((($rows / MAX_DISPLAY_CATEGORIES_PER_ROW) == floor($rows / MAX_DISPLAY_CATEGORIES_PER_ROW)) && ($rows != $number_of_categories)) {
echo ' </tr>' . "\n";
echo ' <tr>' . "\n";
}
}

?>

In next osCommerce file structure tutorial, I will start studying how the codes work.

osCommerce file structure example codesosc-navigate-through-categories-code-study-example-2-1.zip