Jump to content

adding a subcategory


scmeeker

Recommended Posts

I would like add subcategories to my main categories.

For example:

 

Paintings (would be a main category)

oil (sub-category)

acrylic  (sub-category)

watercolor  (sub-category)

 

When a user first comes to the webpage, it would list all the main categories.  Then, when the user clicks on a main category, the Sub-categories dynamically drop down, pushing down the other main categories (but still being able to view them).

 

I already have a separate sub-category table ready to pull from that match up with the corresponding  category.

 

This code, pulls from the category table and lists the main categories along the side and when clicked brings up all matching products according to the category.  But now I would like to take it one more step and have the sub-category list drop down under the main category while still bringing up the products under that category.  Then a user could choose to sort even further through the products by choosing the sub-category.

 

Hopefully I'm making some sense here. :)  Thanks for your help.

 

  //Set the selected category
$selected_cat = (isset($_GET["cat_id"])) ? $_GET["cat_id"] : false;

//show categories first
$result = mysql_query("SELECT cat_id, cat_title FROM category ORDER BY cat_id")
or die(mysql_error());

if (mysql_num_rows($result) < 1)
{
   $categoryList = "<p><em>Sorry, no categories to browse.</em></p>";
}
else
{
    //Display the categories
    while ($cats = mysql_fetch_array($result))
    {
        $categoryList .= "<a href=\"listtest5.php?cat_id={$cats['cat_id']}\">{$cats['cat_title']} <br /></a>\n";
        if ($cats['cat_id']==$selected_cat)
        {
            //get items
            $get_items_sql = mysql_query("SELECT id, image_upload_box, username, title, price, date FROM product WHERE cat_id = '{$selected_cat}' ORDER BY date $pages->limit")
		or die(mysql_error());;
            
            if (mysql_num_rows($get_items_sql) < 1)
            {
                $content = "<p><em>Sorry, no items in this category.</em></p>\n";
            }
            else
            {
                $content .= "<ul>\n";
                while ($items = mysql_fetch_array($get_items_sql))
                {
                    $item_url = "items3.php?id={$items['id']}=username={$items['username']}";
                    $item_title = stripslashes($items['title']);
                    $item_price = $items['price'];
                    $item_photo = $items['photo'];
                $item_username = $items['username'];
                    $item_date = $items['date'];
                    $content .= "";

 

Link to comment
https://forums.phpfreaks.com/topic/208072-adding-a-subcategory/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.