Home | Store | osCommerce Tutorials

osCommerce Navigate Through Categories - Categories Content Code Study

Part 2 - Review osCommerce code structure


Most of the codes have been removed in previous osCommerce Categories Content tutorial. Let's review the codes again.

In previous osCommerce file structure tutorial, I removed all un-used codes. Let's review the codes again:

<?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";
}
}

?>

At first glance, the codes seems still look complicated. Actually you should be very familiar with this code structure. Oh no... Yes.... we already learned this code structure in the MySQL Database Tutorial.

Let's me remove some codes. Then look only at the codes again:

$categories_query = tep_db_query("select c.categories_id, cd.categories_name,........

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

----- codes here simply used to display the contents -----

}

You learned that before. Aren't you? If no, please read the MySQL Database Tutorial again and then come back to this tutorial again.

Let's recall the codes structure:

  • The first line is simply select theMySQL database table
  • The second line is to fetch records into an array and store in the $categories variable. Then using while loop to loops through all the records one by one and display the data on the webpage.

Therefore the block of codes inside the while loop curly braces are simply used to display the data on the Categories Content.

Now we have the general idea of the code structure. Instead of studying of codes, which is very boring, we would rather to write the codes on our own. Let's start off with the following codes:

<?php

require('includes/application_top.php');

// The values of this two variables should come from application_top.php
$current_category_id = 1; // Hardware
$languages_id = 1; // English Language

$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");

$categories = tep_db_fetch_array($categories_query);

?>

The above codes is enough for us to play around.

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