Home | Store | osCommerce Tutorials

osCommerce Main Categories Content File Structure

Part 1 - First glance at Main Categories Content code structure


The code structure of osCommerce Main Categories Content is quite complicated and congested. This is better to break the long codes into seperate parts and study. Let's begin to study the following part of the codes of osCommerce Categories content.

$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)) {

// Original osCommerce codes here

}

At first glance, the above codes simply selects records from both:

  • table "categories", and
  • table "categories_description"
Same as previous osCommerce tutorials, this is a good idea to study the structure and contents of table categories and table categories_description:

osCommerce table categories and categories_description

We learned this line of code in many previous tutorial. It simply selects records from both table "categories" and table "categories_description".

Actually the first line is quite self-explantory:

  • Select field"categories_id" and field"parent_id" from Table"categories".
  • Select field"categories_name" from Table"categories_description".
  • The criteria is that:
    c.parent_id = '0' and c.categories_id = cd.categories_id and cd.language_id='" . (int)$languages_id ."'
  • The result is order by sort_order, cd.categories_name");

The second line is to fetch the records in an array stored in the $categories variable.

Same as previous osCommerce tutorial, let's play around with the codes to get a better idea of the codes and fetch data. The following codes will loop through the $categories and get the record one by one.

$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)) {

// get the data
echo 'The categories_id is: ' . $categories ["categories_id"];
echo "<br />";
echo 'The categories_name is: ' . $categories["categories_name"];
echo "<br />";
echo 'The parent_id is: ' . $categories["parent_id"];
echo "<br /><br />";

}

Save categories.php and upload the file.

The output should be:

The categories_id is: 1
The categories_name is: Hardware
The parent_id is: 0

The categories_id is: 2
The categories_name is: Software
The parent_id is: 0

The categories_id is: 3
The categories_name is: DVD Movies
The parent_id is: 0

Let's get the data in array output:

$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)) {

// get the data in array
echo '<pre>';
print_r($categories);
echo '</pre>';

}

Save categories.php and upload the file.

The output should be:

Array
(
[categories_id] => 1
[categories_name] => Hardware
[parent_id] => 0
)

Array
(
[categories_id] => 2
[categories_name] => Software
[parent_id] => 0
)

Array
(
[categories_id] => 3
[categories_name] => DVD Movies
[parent_id] => 0
)

The query result can be illustrated in the following picture:

osCommerce main categories query