Home | Store | osCommerce Tutorials

osCommerce Main Categories Content File Structure

Part 2 - Study the value of $tree array


Let's expand the codes of osCommerce Categories content in previous tutorial:

$tree = array();

$categories_query = tep_db_query("select c.categories_id, cd.categories_name, c.parent_id from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.parent_id = '0' and c.categories_id = cd.categories_id and cd.language_id='" . (int)$languages_id ."' order by sort_order, cd.categories_name");

while ($categories = tep_db_fetch_array($categories_query)) {

$tree[$categories['categories_id']] = array('name' => $categories['categories_name'],
'parent' => $categories['parent_id'],
'level' => 0,
'path' => $categories['categories_id'],
'next_id' => false);

}

As you can see that osCommerce put the data into the $tree array. Note that the $tree is now a 2-dimensional array. For example, the Hardware Categories Name can be retrieved by $tree[1]["name"].

The $tree array now is containing the data like the following:

Array
(
[1] => Array
(
[name] => Hardware
[parent] => 0
[level] => 0
[path] => 1
[next_id] =>
)

[2] => Array
(
[name] => Software
[parent] => 0
[level] => 0
[path] => 2
[next_id] =>
)

[3] => Array
(
[name] => DVD Movies
[parent] => 0
[level] => 0
[path] => 3
[next_id] =>
)

)

Let's continue to study the codes. The codes seems a bit more complicated now:

$tree = array();

$categories_query = tep_db_query("select c.categories_id, cd.categories_name, c.parent_id from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.parent_id = '0' and c.categories_id = cd.categories_id and cd.language_id='" . (int)$languages_id ."' order by sort_order, cd.categories_name");

while ($categories = tep_db_fetch_array($categories_query)) {

$tree[$categories['categories_id']] = array('name' => $categories['categories_name'],
'parent' => $categories['parent_id'],
'level' => 0,
'path' => $categories['categories_id'],
'next_id' => false);

if (isset($parent_id)) {
$tree[$parent_id]['next_id'] = $categories['categories_id'];
}

$parent_id = $categories['categories_id'];

if (!isset($first_element)) {
$first_element = $categories['categories_id'];
}

}

The $tree array is now containing the data like the following.

Note that the [next_id] of Hardware array is 2 and the [next_id] of Software array is 3. Actually the [next_id] control what is the next Categories that follow. Confused? Don't worry. We will explain this in more details in coming tutorials.

The $first_element is the first item of the Categories Tree, i.e. 1 (Hardware) of default osCommerce shop. In other words, this is the first item of the Categories tree.

Array
(
[1] => Array
(
[name] => Hardware
[parent] => 0
[level] => 0
[path] => 1
[next_id] => 2
)

[2] => Array
(
[name] => Software
[parent] => 0
[level] => 0
[path] => 2
[next_id] => 3
)

[3] => Array
(
[name] => DVD Movies
[parent] => 0
[level] => 0
[path] => 3
[next_id] =>
)

)

Now we have this array of data.

Let's continue to study the codes down. In order to learn the codes easier at this stage, some codes are omitted:

$tree = array();

$categories_query = tep_db_query("select c.categories_id, cd.categories_name, c.parent_id from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.parent_id = '0' and c.categories_id = cd.categories_id and cd.language_id='" . (int)$languages_id ."' order by sort_order, cd.categories_name");

while ($categories = tep_db_fetch_array($categories_query)) {

$tree[$categories['categories_id']] = array('name' => $categories['categories_name'],
'parent' => $categories['parent_id'],
'level' => 0,
'path' => $categories['categories_id'],
'next_id' => false);

if (isset($parent_id)) {
$tree[$parent_id]['next_id'] = $categories['categories_id'];
}

$parent_id = $categories['categories_id'];

if (!isset($first_element)) {
$first_element = $categories['categories_id'];
}

}

 

if (tep_not_null($cPath)) {

// Codes here will be executed
//when browsing Parent Categories, Sub-Categories and Products

}

As we learned before in other tutorial that the tep_not_null($cPath) will be true when the URL contains the cPath, i.e. browsing Parent Categories, Sub-categories and Products. Therefore the codes as shown above inside the curly braces of if (tep_not_null($cPath)):

  • will not be executed when browsing osCommerce homepage
  • will be executed when browsing Parent Categories, Sub-categories and Products.

Actually the codes that gather data in the main program end here. The next line of codes will call the tep_show_category() function that will start printing out the Main Categories Tree.

We will discuss the above codes inside the curly braces of if (tep_not_null($cPath)) later. However we can make a summary of the $tree array here.

Values of $tree Array

The value of $tree array depends on which links of osCommerce are browsing.

osCommerce homepage:

When browsing osCommerce homepage, the value of $tree is:

Array
(
[1] => Array
(
[name] => Hardware
[parent] => 0
[level] => 0
[path] => 1
[next_id] => 2
)

[2] => Array
(
[name] => Software
[parent] => 0
[level] => 0
[path] => 2
[next_id] => 3
)

[3] => Array
(
[name] => DVD Movies
[parent] => 0
[level] => 0
[path] => 3
[next_id] =>
)

)

The diagram illustrate how the $tree store the value of osCommerce Main Categories when browsing osCommerce homepage:

osCommerce Main Categories

osCommerce Parent Categories (Hardware):

The value of $tree is:

Array
(
[1] => Array
(
[name] => Hardware
[parent] => 0
[level] => 0
[path] => 1
[next_id] => 17
)

[2] => Array
(
[name] => Software
[parent] => 0
[level] => 0
[path] => 2
[next_id] => 3
)

[3] => Array
(
[name] => DVD Movies
[parent] => 0
[level] => 0
[path] => 3
[next_id] =>
)

[17] => Array
(
[name] => CDROM Drives
[parent] => 1
[level] => 1
[path] => 1_17
[next_id] => 4
)

[4] => Array
(
[name] => Graphics Cards
[parent] => 1
[level] => 1
[path] => 1_4
[next_id] => 8
)

[8] => Array
(
[name] => Keyboards
[parent] => 1
[level] => 1
[path] => 1_8
[next_id] => 16
)

[16] => Array
(
[name] => Memory
[parent] => 1
[level] => 1
[path] => 1_16
[next_id] => 9
)

[9] => Array
(
[name] => Mice
[parent] => 1
[level] => 1
[path] => 1_9
[next_id] => 6
)

[6] => Array
(
[name] => Monitors
[parent] => 1
[level] => 1
[path] => 1_6
[next_id] => 5
)

[5] => Array
(
[name] => Printers
[parent] => 1
[level] => 1
[path] => 1_5
[next_id] => 7
)

[7] => Array
(
[name] => Speakers
[parent] => 1
[level] => 1
[path] => 1_7
[next_id] => 2
)

)

The diagram illustrate how the $tree value store the value of osCommerce Main Categories when browsing osCommerce Hardware Parent Categories:

osCommerce Main Categories

Now we know how the $tree store the values of osCommerce Main Categories. The next tutorials shows how the tep_show_category() function use the $tree values to print out the osCommerce Main Categories.