Home | Store | osCommerce Tutorials

osCommerce Main Content Vs $category_depth and $cPath (1)

The value of cPath control what contents of osCommerce shop will be display. The following video shows how the value of $cPath and $category_depth play in osCommerce online shop.

The following PHP block of codes (line 15 - 32) at the top of osCommerce homepage (index.php) determine what the contents of the Main Content (Body text) will be displayed.

// the following cPath references come from application_top.php
$category_depth = 'top';

if (isset($cPath) && tep_not_null($cPath)) {
$categories_products_query = tep_db_query("select count(*) as total from " .
TABLE_PRODUCTS_TO_CATEGORIES . " where categories_id = '" . (int)$current_category_id . "'");
$cateqories_products = tep_db_fetch_array($categories_products_query);

if ($cateqories_products['total'] > 0) {
$category_depth = 'products'; // display products
} else {
$category_parent_query = tep_db_query("select count(*) as total from " .
TABLE_CATEGORIES . " where parent_id = '" . (int)$current_category_id . "'");
$category_parent = tep_db_fetch_array($category_parent_query);

if ($category_parent['total'] > 0) {
$category_depth = 'nested'; // navigate through the categories
} else {
$category_depth = 'products'; // category has no products, but display the 'no products' msg
}
}
}

Let's break down the PHP codes as below in a more simple way and study.

// the following cPath references come from application_top.php
$category_depth = 'top';

if (isset($cPath) && tep_not_null($cPath)) {
----- codes here -----
}

$category_depth variable

The default value of $category_depth is top.

As we learned from previous tutorial that if the value of $category_depth is top, the default homepage content will be displayed in the Main Content section.

$cPath variable

If the value of $cPath variable is set (isset). And. The custom function tep_not_null() return True (i.e. $cPath has value).Then the codes enclosed will be executed.

What is cPath?

Hove the mouse pointer over the links under the Categories Box, and the value of cPath can be seen in the status bar. In fact the value of cPath can also be seen in the URL address field.

The picture below shows the cPath value when visiting Parent Categories (e.g. Hardware, Software and DVD Movies) of osCommerce shop:

osCommerce cPath value

The picture below shows the cPath value when visiting osCommerce homepage (index.php):

osCommerce homepage cPath value

Here's the complete cPath values of default osCommerce shop:

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

The following diagram shows the value of $category_depth and $cPath when browsing osCommerce homepage:

osCommerce $category_depth and $cPath

Codes Study

Let's recall the codes again:

if (isset($cPath) && tep_not_null($cPath)) {
----- codes here -----
}

When you studying the osCommerce codes, you will always come across the PHP build-in isset() function and osCommerce tep_not_null() function. Let's study them one by one.

isset ($cPath)

The PHP isset() function will determine if a variable is set and is not NULL.

Note:

  • Returns TRUE if the variable exists and has value other than NULL.
  • Remember that an empty string is not null.
  • The result may sometimes expect differently with PHP version 4 and 5.

Example:

if isset ($cPath) {

// if the $cPath variable is set and is not null, the codes here will be executed.
----- codes here -----

}

Actually you may remove some codes of the osCommerce homepage and modify as below:

if (isset ($cPath)) {

echo 'The value of $cPath variable is set and is not NULL.';

} else {

echo 'The value of $cPath variable is NOT set and is NULL.';

}

Upload the file and overwrite osCommerce homepage (index.php). Open a browser and test with osCommerce homepage, parent categories and sub-categories.

Result:

We tested with PHP4. We noticed that the value of $cPath variable is always set and is not null. See the above table for the values of cPath for different links. Therefore the isset ($cPath) is always true.

tep_not_null($cPath)

The custom osCommerce PHP tep_not_null () function will determine if the variable has value.

Note:

  • Returns TRUE if the variable exists and has value.
  • Returns FALSE if the variable is empty.

Example:

if tep_not_null ($cPath) {

// if the $cPath variable has value other than empty, the codes here will be executed.
----- codes here -----

}

Remove some codes of the osCommerce homepage and modify as below:

if (tep_not_null ($cPath)) {

echo '$cPath has value now. The value is: ' . $cPath;

} else {

echo 'The value of $cPath variable is empty.';

}

Upload the file and overwrite osCommerce homepage (index.php). Open a browser and test with osCommerce homepage, parent categories and sub-categories.

Result:

  • The custom osCommerce PHP tep_not_null () function will return FALSE when visiting homepage. Because the value of $cPath is empty.
  • The custom osCommerce PHP tep_not_null () function will return TRUE when visiting parent categories and sub-categories. Because $cPath has values (e.g. 1, 1_17, 1_14, etc...). See the above table for values of cPath for different links.

Now, we can combine the two functions.

isset($cPath) && tep_not_null($cPath)

if (isset($cPath) && tep_not_null($cPath)) {

// If isset($cPath) is true AND tep_not_null($cPath) is true, the codes here will be executed.
----- codes here -----

}

The "&&" operator means AND.

Therefore (isset($cPath) && tep_not_null($cPath)) is TRUE if:

  • isset($cPath) is true AND tep_not_null($cPath) is also true.

Result:

  • It will return FALSE when visiting homepage. Because the value of $cPath is empty.
    Therefore tep_not_null($cPath) is false.
  • It will return TRUE when visiting parent categories and sub-categories. Because $cPath has values (e.g. 1, 1_17, 1_14, etc...). See the above table for values of cPath for different links.