Creating a PHP menu

Hi,

I have a sliding menu on this page:

http://www.freemanholland.com/cow/public_html/shop

If you look on the left you will see the sliding menu when clicking on a main link.

Now what i need to do is pull this out dynamically from a database. I have the following tables:

tbl_main_category
ID, name, date_added, deleted

tbl_category
ID, name, maincategory_ID, date_added, deleted

tbl_subcategory
ID, name, category_ID, date_added, deleted

So,

  1. Main Categories are “Men” and “Women”
  2. Categories are “Clothing”, “Shoes” etc…
  3. Sub categories are “Coats and Jackets”, “Knitwear” and so on…

Now i have the following methods in place:


class Category {

    public function selectAllMainCategories(){

        $query = "SELECT * FROM tbl_maincategory WHERE deleted = 0";

        $result = mysql_query($query);
        return $result;
    }

    public function selectAllCategories(){

        $query = "SELECT c.ID as TheID,
                         c.name as TheName,
                         c.maincategory_ID,
                         c.date_added,
                         m.name
                 FROM tbl_category c
                 INNER JOIN tbl_maincategory m 
                 ON c.maincategory_ID = m.ID
                 WHERE c.deleted = 0 AND m.deleted = 0
                 ORDER BY c.date_added DESC";
        $result = mysql_query($query);
        return $result;
    }

    public function selectAllSubCategories(){

        $query = "SELECT s.ID as TheID,
                         s.name as TheName,
                         s.category_ID,
                         s.date_added,
                         c.name
                 FROM tbl_subcategory s
                 INNER JOIN tbl_category c
                 ON s.category_ID = c.ID
                 WHERE s.deleted = 0 AND c.deleted = 0
                 ORDER BY s.date_added DESC";
        $result = mysql_query($query);
        return $result;
    }

    public function selectAllSubcategoriesByCategoryID($ID){

        if(is_numeric($ID)){

            $query = "SELECT s.ID as TheID,
                         s.name as TheName,
                         s.category_ID,
                         s.date_added,
                         c.name
                 FROM tbl_subcategory s
                 INNER JOIN tbl_category c
                 ON s.category_ID = ".mysql_real_escape_string($ID)."
                 WHERE s.deleted = 0 AND c.deleted = 0";

            $result = mysql_query($query);
            return $result;

        }else{
            die('ID needs to be numeric');
        }
    }  
}

How can i put this together to create the menu how i want…

I understand that these methods may need to change, so i appreciate any help whatsoever…

Thanks

Thanks, i didn’t see that :slight_smile:

It works perfectly now…

Thanks

me? no, sorry, i don’t do php

Ok. It turns out that the menu has been simplified even more. Now there are categories and subcategories.

If you take a look at this page:

http://www.freemanholland.com/cow/public_html/shop

I have nearly got it working… But for some reason each sub category name is repeated twice…

I tried doing this:


class Category {

    public function createMenu(){

        $result = self::selectAllCategories();
        $i=0;
        while($category = mysql_fetch_array($result)){
            ?>
            <h2 style="margin-top:0;">
                <span <?= $i == 3 ? 'style="color:#e34274; font-weight:bold;"' : ''?>><?=$category['name']?></span>
            </h2>

                <?
                $resultOfSubcategory = self::selectAllSubcategoriesByCategoryID($category['ID']);
                if(mysql_num_rows($resultOfSubcategory) > 0){
                    ?>
                    <ul>
                        <?
                        while($subcategory = mysql_fetch_array($resultOfSubcategory)){
                            $link = str_replace(' ', '-', $subcategory['TheName']);
                            echo "<li><a href=\\"".__SITE_PATH.str_replace(' ', '', $category['name']).'/'.$link."/\\">".$subcategory['TheName']."</a></li>";
                        }
                        ?>
                    </ul><br/>
                    <?
                }
            $i++;
        }
    }

    public function selectAllCategories(){

        $query = "SELECT * FROM tbl_category WHERE deleted = 0
            ORDER BY date_added DESC";
        $result = mysql_query($query);
        return $result;
    }

    public function selectAllSubcategoriesByCategoryID($ID){

        if(is_numeric($ID)){

            $query = "SELECT s.ID as TheID,
                         s.name as TheName,
                         s.category_ID,
                         s.date_added,
                         c.name
                 FROM tbl_subcategory s
                 INNER JOIN tbl_category c
                 ON s.category_ID = ".mysql_real_escape_string($ID)."
                 WHERE s.deleted = 0 AND c.deleted = 0";

            $result = mysql_query($query);
            return $result;

        }else{
            die('ID needs to be numeric');
        }
    }
}

Look at the createMenu() method. I almost got it working…

Can you see where i am going wrong? Why are the names being pulled out twice and showing duplication?

Thanks

Why don’t you give it a try, and when you get stuck, post your code here and we’ll try and help you :slight_smile:

Because you’re cross joining two tables (a join without a joining condition).
Since I don’t understand why you’d want to join the two tables anyway (you already have the category table data from your first query), I’ll give you the no-join solution:


    public function selectAllSubcategoriesByCategoryID($ID) {

        if(is_numeric($ID)){

            $query = "
               SELECT 
                   s.ID as TheID
                 , s.name as TheName
                 , s.category_ID
                 , s.date_added
                 , c.name
               FROM tbl_subcategory s
               WHERE s.category_ID = ".mysql_real_escape_string($ID)."
               AND s.deleted = 0
            ";

            $result = mysql_query($query);
            return $result;

        }else{
            die('ID needs to be numeric');
        }
    }

If you really need to join to the category table, then put the joining condition in the ON clause.

Ohh well the problem is i thought this was the best way to do it, and i’ve created the back-end admin section :(, i will have to keep this method in mind though :smiley:

What i will do is try to get it working with the methods i have used and let you guys know if i have any problems…

I will be in touch…

i hope you don’t mind me jumping in here and making a comment about your tables, but you did say “any help whatsoever”

presumably these categories are for products?

okay, here’s the problem: when you go to associate a given product with a category, do you link it to the subcategory? what if a particular category has no subcategories? would you link the product to the appropriate category?

i hope you can see where i’m going with this, because you will end up with products belonging either to a main category, or to a category, or to a subcategory

the result, when you want to search for products based on the category they’re in, is a UNION query with three different joins, which will be very clumsy

however, if you had just one table for all levels of categories, this complexity goes away

here’s an article which describes a hierarchical structure implemented in a single categories table: Categories and Subcategories

hope it helps :slight_smile: