Home | Store | osCommerce Tutorials

osCommerce Navigate Through Categories - Categories Content Code Study

Part 3.2 - Get all records of osCommerce Categories data with while loop


This osCommerce tutorial shows how to get all the categories data from MySQL database by using the PHP while loop.

In previous osCommerce tutorial, we got the data of the first row of the array successfully. Let's review the codes of previous tutorial:

<?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);

// get the data
echo 'The categories_id is: ' . $categories ["categories_id"];
echo "<br />";
echo 'The categories_name is: ' . $categories["categories_name"];
echo "<br />";
echo tep_image(DIR_WS_IMAGES . $categories['categories_image'], $categories['categories_name']);

?>

The output of the above codes is:

oscommerce navigate through categories

In this osCommerce Categories Content tutorial, we will modify the codes so that it looks like the deafult layout of the osCommerce shop.

Example 2-4

Let's modify the above codes a bit. Like osCommerce shop, we will use a table to contain the query data. The codes are below:

<?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);

// get the data
echo "<table border=1 cellpadding=8>";
echo "<tr><td>categories_id</td><td>categories_name</td><td>categories_image</td></tr>";
echo "<tr><td>" . $categories ["categories_id"] . "</td><td>" . $categories["categories_name"] . "</td><td>" . tep_image(DIR_WS_IMAGES . $categories['categories_image'], $categories['categories_name']) . "</td></tr>";
echo "</table>";

?>

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

Save the file as osc-navigate-through-categories-code-study-example-2-4.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 layout looks better.

Example 2-5

In the above two examples, we only display the first record of the $categories array. It's time to loop through the $categories array and display the data one by one. As you learned before that it only requires to modify the codes a bit. The codes now are:

<?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><td>categories_id</td><td>categories_name</td><td>categories_image</td></tr>";

while ( $categories = tep_db_fetch_array($categories_query) ) {
echo "<tr><td>" . $categories ["categories_id"] . "</td><td>" . $categories["categories_name"] .
"</td><td>" . tep_image(DIR_WS_IMAGES . $categories['categories_image'],
$categories['categories_name']) . "</td></tr>";
}

echo "</table>";

?>

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

Save the file as osc-navigate-through-categories-code-study-example-2-5.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

Look! Once you knows how the osCommerce file structure, you can display the output in whatever format you like.

Example 2-6

Now, let's continue modifying the codes a bit so that the Categories layout looks like the default osCommerce shop.

<?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><td>Categories</td></tr>";

while ( $categories = tep_db_fetch_array($categories_query) ) {
echo "<tr><td>" . tep_image(DIR_WS_IMAGES . $categories['categories_image'],
$categories['categories_name']) . "<br />" . $categories["categories_name"] . "</td></tr>";
}

echo "</table>";

?>

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

Save the file as osc-navigate-through-categories-code-study-example-2-6.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

Most osCommerce shop owners would like to arrange the Categories in two columns or 3 columns. We will discuss this in next osCommerce File Structure tutorial.