Home | Store | osCommerce Tutorials

osCommerce Main Categories Content File Structure

Part 4 - How the string of $categories_string is formed


In previous osCommerce tutorial, we learned the contents of the $categories_string that used to print out the osCommerce Main Categories. It's time to see how the contents of $categories_string is formed or coded.

Let's recall the main code structure of categories.php again:

$categories_string = '';
$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 (tep_not_null($cPath)) {

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

}

tep_show_category($first_element);

$info_box_contents = array();
$info_box_contents[] = array('text' => $categories_string);

new infoBox($info_box_contents);

 

The $categories_string is assigned an empty string at the beginning of the code.

$categories_string = '';

The the tep_show_category($first_element) function was firstly called after the codes gathering enough data. We mentioned before that the $first_element is the first item of the Categories tree.

tep_show_category($first_element);

Okay! let's take a look at the tep_show_category() function

Main Categories tep_show_category() Function

Let's skim over the main structure of tep_show_category() function.

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

// More codes here....
// More codes here...
// More codes here...

 

$categories_string .= '<br>';

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

}

The $counter is actually the $tree array that used to trace the Categories items one by one. We will explain this in more details in coming tutorials.

Note that the $tree, $categories_string and $cPath_array are global variables so that they can also be accessed in the main codes.

$tree Variable

The $tree variables already discussed and the values are obtained in Part 2 of this tutorial.

$categories_string Variable

The value of $categories_string variable is empty when the first time the tep_show_category() function being called in the main program of categories.php.

i.e. $categories_string ='';

Then it join (or concatenate) all required contents until all data are "loop through". The following code is the last content that the $categories_string will be joined:

$categories_string .= '<br>';

Take browsing the osCommerce homepage as an example:

When the tep_show_category() function is called for the first time, the value of $categories_string variable is empty, i.e.

$categories_string ='';

Then the tep_show_category() function run, gather all required content. It then end at:

$categories_string .= '<br>';

The value of $categories_string variable is now:

$categories_string =
<a href="http://osc.cz.cc/index.php?cPath=1">Hardware-&gt;</a>&nbsp;(6)<br>

If ($tree[$counter]['next_id'] != false) is true i.e. the contents is not the last item. It will call the tep_show_category() function again. The codes are:

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

The tep_show_category() function then run again. The value of $categories_string variable will be the following at the end:

$categories_string =
<a href="http://osc.cz.cc/index.php?cPath=1">Hardware-&gt;</a>&nbsp;(6)<br>
<a href="http://osc.cz.cc/index.php?cPath=2">Software-&gt;</a>&nbsp;(4)<br>

Now the ($tree[$counter]['next_id'] != false) is still true. Therefore It will call the tep_show_category() function once again.

The tep_show_category() function then run again. The value of $categories_string variable will be the following at the end:

$categories_string =
<a href="http://osc.cz.cc/index.php?cPath=1">Hardware-&gt;</a>&nbsp;(6)<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>

That's why "item" of the $categories_string we obtained in Part 3 of this osCommerce tutorial series are all end with "<br>".

$cPath_array Variable

The values of $cPath_array will depends on the cPath when browsing osCommerce webpages. We discussed a lot about cPath in previous tutorials.

The values of $cPath_array are shown below when browsing the Categories.

Browsing osCommerce Homepage

osCommerce cPath_array

The $cPath_array will be empty when browsing osCommerce homepage

Browsing osCommerce Hardware Parent Categories

osCommerce cPath_array

The $cPath_array is:

Array
(
[0] => 1
)

Array
(
[0] => 1
)

Array
(
[0] => 1
)

Array
(
[0] => 1
)

Array
(
[0] => 1
)

Array
(
[0] => 1
)

Array
(
[0] => 1
)

Array
(
[0] => 1
)

Array
(
[0] => 1
)

Array
(
[0] => 1
)

Array
(
[0] => 1
)

Browsing osCommerce Software Parent Categories

osCommerce cPath_array

The $cPath_array is:

Array
(
[0] => 2
)

Array
(
[0] => 2
)

Array
(
[0] => 2
)

Array
(
[0] => 2
)

Array
(
[0] => 2
)

Array
(
[0] => 2
)

Browsing osCommerce DVD Movies Parent Categories

osCommerce cPath_array

The $cPath_array is:

Array
(
[0] => 3
)

Array
(
[0] => 3
)

Array
(
[0] => 3
)

Array
(
[0] => 3
)

Array
(
[0] => 3
)

Array
(
[0] => 3
)

Array
(
[0] => 3
)

Array
(
[0] => 3
)

Array
(
[0] => 3
)

Browsing osCommerce CDROM Drives Sub-Categories

osCommerce cPath_array

The $cPath_array is:

Array
(
[0] => 1
[1] => 17
)

Array
(
[0] => 1
[1] => 17
)

Array
(
[0] => 1
[1] => 17
)

Array
(
[0] => 1
[1] => 17
)

Array
(
[0] => 1
[1] => 17
)

Array
(
[0] => 1
[1] => 17
)

Array
(
[0] => 1
[1] => 17
)

Array
(
[0] => 1
[1] => 17
)

Array
(
[0] => 1
[1] => 17
)

Array
(
[0] => 1
[1] => 17
)

Array
(
[0] => 1
[1] => 17
)

Browsing osCommerce Memory Sub-Categories

osCommerce cPath_array

The $cPath_array is:

Array
(
[0] => 1
[1] => 16
)

Array
(
[0] => 1
[1] => 16
)

Array
(
[0] => 1
[1] => 16
)

Array
(
[0] => 1
[1] => 16
)

Array
(
[0] => 1
[1] => 16
)

Array
(
[0] => 1
[1] => 16
)

Array
(
[0] => 1
[1] => 16
)

Array
(
[0] => 1
[1] => 16
)

Array
(
[0] => 1
[1] => 16
)

Array
(
[0] => 1
[1] => 16
)

Array
(
[0] => 1
[1] => 16
)

Browsing osCommerce Action Sub-Categories

osCommerce cPath_array

The $cPath_array is:

Array
(
[0] => 2
[1] => 19
)

Array
(
[0] => 2
[1] => 19
)

Array
(
[0] => 2
[1] => 19
)

Array
(
[0] => 2
[1] => 19
)

Array
(
[0] => 2
[1] => 19
)

Array
(
[0] => 2
[1] => 19
)

Browsing osCommerce Comedy Sub-Categories

osCommerce cPath_array

The $cPath_array is:

Array
(
[0] => 3
[1] => 12
)

Array
(
[0] => 3
[1] => 12
)

Array
(
[0] => 3
[1] => 12
)

Array
(
[0] => 3
[1] => 12
)

Array
(
[0] => 3
[1] => 12
)

Array
(
[0] => 3
[1] => 12
)

Array
(
[0] => 3
[1] => 12
)

Array
(
[0] => 3
[1] => 12
)

Browsing osCommerce Hardware - Graphics Card - Matrox G200 MMS Product

osCommerce cPath_array

The $cPath_array is:

Array
(
[0] => 1
[1] => 4
)

Array
(
[0] => 1
[1] => 4
)

Array
(
[0] => 1
[1] => 4
)

Array
(
[0] => 1
[1] => 4
)

Array
(
[0] => 1
[1] => 4
)

Array
(
[0] => 1
[1] => 4
)

Array
(
[0] => 1
[1] => 4
)

Array
(
[0] => 1
[1] => 4
)

Array
(
[0] => 1
[1] => 4
)

Array
(
[0] => 1
[1] => 4
)

Array
(
[0] => 1
[1] => 4
)

Now, we have the idea of the value of $cPath_array.

In the next osCommerce Main Categories tutorial, we will see how the tep_show_category() listing out all the elements of the osCommerce Main Categories.