Home | Store | osCommerce Tutorials

osCommerce Navigate Through Categories - Categories Content Code Study

Part 6 - study original codes of osCommerce Categories


We have been playing around with osCommerce Categories Content and have learned a lot. As we mentioned before that this is the best way to learn writting codes.

Now this is time to study how osCommerce original codes work. At this moment, this should be rather easy to understand the osCommerce codes since we have been playing around the codes for some times. The method used by osCommerce may be a bit different from ours.

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

?>

Look! The codes are almost the same as ours. Let's check the difference:

$width = (int)(100 / MAX_DISPLAY_CATEGORIES_PER_ROW) . '%';

The $width is simply the width (%) set to each column. For example:

  • If there is only one column, the width of the column is 100%, i.e. <td width="100%">
  • If there are two columns, the width of each column is 50%, i.e. <td width="50%">
  • If there are three columns, the width of each column is 33%, i.e. <td width="33%">

The major difference should be the following code:

if ((($rows / MAX_DISPLAY_CATEGORIES_PER_ROW) == floor($rows /
MAX_DISPLAY_CATEGORIES_PER_ROW
)) && ($rows != $number_of_categories)) {
echo ' </tr>' . "\n";
echo ' <tr>' . "\n";
}

The code is a bit difference from ours. It seems that our code is more short, clean and easy. Here's our code again:

if ( (is_int($rows/MAX_DISPLAY_CATEGORIES_PER_ROW)) && ($rows != $number_of_categories) ) {
echo "</tr>";
echo "<tr>";
}

We don't know why osCommerce is using so complicated code.

Let's explain how the osCommerce code work. We only explain the first condition as the second condition is same as ours and explained before.

The floor() function will returns the next lowest integer value by rounding down value if necessary. For example:

  • floor (2.5) will return 2
  • floor (0.6) will return 0
  • floor (1) will return 1

Let's navigate through the Hardware Categories and everything will be clear.

$rows $rows /
MAX_DISPLAY_CATEGORIES_PER_ROW
floor($rows /
MAX_DISPLAY_CATEGORIES_PER_ROW)
Result
1 0.33 0 not equal
2 0.66 0 not equal
3 1 1 EQUAL
4 1.33 1 not equal
5 1.66 1 not equal
6 2 2 EQUAL
7 2.33 2 not equal
8 2.66 2 not equal

NOTE:

The value of MAX_DISPLAY_CATEGORIES_PER_ROW is 3 in this example.

The result is compare $rows / MAX_DISPLAY_CATEGORIES_PER_ROW with floor($rows /
MAX_DISPLAY_CATEGORIES_PER_ROW)

As you can see that the concept is exactly the same. The only difference is that we use a more straight forward idea.

This is the end of osCommerce file structure or code structure of Categories Content. Don't be scared by the long and complicated codes. Always breaking it down into some shorter codes and study. You will find out that the codes are so simple.