Home | Store | osCommerce Tutorials

osCommerce Navigate Through Categories - Categories Content Code Study

Part 5 - add URL link to Categories Images and Categories Name


In previous osCommerce tutorial, we already know how to loop through (navigate through) the Categories. We also learned how to display the Categories Content in a table with single column or multiple columns. In this part of osCommerce tutorial, we will add URL links to the categories image and categories name.

Firstly, let's recall what we learned up to now:

$categories['categories_id']

The $categories['categories_id'] will return the categories_id as below:

$categories['categories_id']
17
4
8
16
9
6
5
7

osCommerce tep_get_path() function

We also learned the tep_get_path() function before. Read the osCommerce tep_get_path() custom function tutorial if you wish.

The tep_get_path(parameter) function is used to append "cPath=" to the beginning of the parameter.

Therefore tep_get_path($categories['categories_id']) will append "cPath=" to the beginning of categories_id.

$categories['categories_id'] tep_get_path($categories['categories_id'])
17 cPath=17
4 cPath=4
8 cPath=8
16 cPath=16
9 cPath=9
6 cPath=6
5 cPath=5
7 cPath=7

FILENAME_DEFAULT

The FILENAME_DEFAULT is usually defined as "index.php" in catalog/includes/filenames.php.

Read the osCommerce tep_herf_link() custom function tutorial for more information of FILENAME_DEFAULT if you wish.

osCommerce tep_href_link() function

We also learned the tep_href_link() function before. The osCommerce tep_herf_link() custom function tutorial has explained in very details.

In summary, the osCommerce tep_href_link() function used to join "http://www.YourShopDomain.com/" or "http://YourShopDomain.com/" with the 1st parameter and 2nd parameter.

Therefore, tep_href_link (FILENAME_DEFAULT, tep_get_path($categories['categories_id'])) will be:

$categories
['categories_id']
tep_get_path
($categories['categories_id'])
tep_href_link(FILENAME_DEFAULT,
tep_get_path($categories['categories_id']))
17 cPath=17 http://www.YourShop.com/index.php?cPath=17
4 cPath=4 http://www.YourShop.com/index.php?cPath=4
8 cPath=8 http://www.YourShop.com/index.php?cPath=8
16 cPath=16 http://www.YourShop.com/index.php?cPath=16
9 cPath=9 http://www.YourShop.com/index.php?cPath=9
6 cPath=6 http://www.YourShop.com/index.php?cPath=6
5 cPath=5 http://www.YourShop.com/index.php?cPath=5
7 cPath=7 http://www.YourShop.com/index.php?cPath=7

Look! Actually we already learned a lot before.

Now we need to add the link tep_href_link(FILENAME_DEFAULT, tep_get_path($categories['categories_id'])) to the categories. Obviously, this can be done by:

<a href="' . tep_href_link(FILENAME_DEFAULT, tep_get_path($categories['categories_id'])) . '">' . xxx . '</a>'

Let's continue modifying the last codes.

Example 2-9

We already get all required knowledge, this should be not too difficult to add the categories link to both categories image and categories name. Here's the 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");

// Use below
//$categories = tep_db_fetch_array($categories_query);

// get the data
echo "<table border=1 cellpadding=8>";
echo "<tr>";

// We ned to know the total number of records of the output query
$number_of_categories = tep_db_num_rows($categories_query);

// We need a checking indicator
$rows = 0;

while ( $categories = tep_db_fetch_array($categories_query) ) {
// increase the indicator by one for each loop so that we can check the Categories position
$rows++;

// get the categories cPath and append "cPath=" to the beginning
// store the value in $cPath_new variable

$cPath_new = tep_get_path($categories['categories_id']);

echo "<td>" . '<a href="' . tep_href_link(FILENAME_DEFAULT, $cPath_new) . '">' .
tep_image(DIR_WS_IMAGES . $categories['categories_image'], $categories['categories_name']) .
"<br />" . $categories["categories_name"] . "</a>" . "</td>";

// Check current displaying Categories:
// if last item of the row or not
// Of course we also need to check if this is the last records

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

echo "</tr>";
echo "</table>";

?>

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

Save the file as osc-navigate-through-categories-code-study-example-2-9.php.

Upload the file to the osCommerce homepage.

Access the file with browser.

The output of the file should look like:

oscommerce navigate through categories

The categories links have been added to all categories images and categories names.