Code that calculates the number of dependent categories not

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Mulith
Forum Newbie
Posts: 1
Joined: Wed Sep 30, 2009 6:11 pm

Code that calculates the number of dependent categories not

Post by Mulith »

Hi all,

This is my first post and I'm a little bit new to PHP. Here is the problem I am experiencing:

I have included some php in one of my pages to do things:

Firstly to create an index of franchise categories from our database

and secondly to calculate the number of child categories for each main category.

The database is setup as follows:
id | name | parent_id | hompage

Main categories have a parent_id value of 1.

The problem is that the code works fine when calculating the number of dependent categories for the first main category it finds but then doesn't do the same for all the other main categories in the list. Here is the code:

Code: Select all

<? 
            $sql="SELECT * FROM franchise_categories WHERE homepage = 1 ORDER BY name ASC";
            $sql2="SELECT * FROM franchise_categories WHERE parent_id > 1 ORDER BY id ASC";
            $result = mysql_query($sql) or die(mysql_error());
            $result2 = mysql_query($sql2) or die(mysql_error());
            $count = 0;
            $column=1;
                   
                  while ($myrow = mysql_fetch_array($result))
                  {
                        $name = $myrow['name'];
                        $link = $myrow['id'];
                        $parent_id= $myrow['parent_id'];
                        $cat_counter=0;
                        if ($parent_id == 1){
                            
                            while ($myrow2 = mysql_fetch_array($result2)){
                            $temp=$myrow2['parent_id'];
                            if ($temp == $link) {
                            $cat_counter++; }}
                            
                            } else{
                            
                            $cat_counter = 0;}
                                                     
                        echo "<a href = 'View_Category.php?id=" . $link . "'>".$name."&nbsp;[".$cat_counter."]</a><br/>";
                        $count = $count + 1;
                        if (($count == 14) && ($column < 3))
                        {
                            echo "</td><td width=\"33%\">";
                            $count = 0;
                            $column++;
                        }
                  }
            ?>



Here is the live page with the error if you want to look:http://www.business4sale.co.uk/Franchises_test.php

Any help would be appreciated.

Thanx in advance.

Mulith
Post Reply