Page 1 of 1

How to show Subcategories under a category

Posted: Tue Sep 20, 2011 6:01 am
by reachmou
Hello,

I am complete newbie in programming so my question might sound like an elementary one though i am stuck badly so any help would be much appreciated.

I need to fetch and show subcategories under a category and link them to a page.

Pasting my code snippet below

Code: Select all

$sql = mysql_query("SELECT id , title , nest_under FROM photo_galleries WHERE active = '1'");
while($result = mysql_fetch_assoc($sql)) {

if ($result[nest_under] > 0){
echo '<a href="gallery.php?gid='.$result['id'].'">'.$result['title'].'</a>'. "&nbsp;" . "|" . "&nbsp;";
} 
 }
Right now it shows all subcategories from every category, what i want is this :

Cat A
Cat B - subcatB_b
Cat C - subcatC_c

so e.g. when i click on Cat C it should only show subcatC_c where as its showing 'subcatB_b' and 'subcatC_c' which is wrong. , lemme know if anyone can help.

Re: How to show Subcategories under a category

Posted: Tue Sep 20, 2011 2:22 pm
by weismana81
I'm a relative newbie myself so I'm not sure if this is your issue, but it looks like you missed single quotes in the array in your if statement...?

You have

Code: Select all

if ($result[nest_under] > 0){
I'm thinking this should be

Code: Select all

if ($result['nest_under'] > 0){
I'm not positive on that, but give it a shot and see what happens :wink:

Re: How to show Subcategories under a category

Posted: Tue Sep 20, 2011 5:57 pm
by califdon
reachmou wrote:I need to fetch and show subcategories under a category and link them to a page.

Pasting my code snippet below

Code: Select all

$sql = mysql_query("SELECT id , title , nest_under FROM photo_galleries WHERE active = '1'");
while($result = mysql_fetch_assoc($sql)) {

if ($result[nest_under] > 0){
echo '<a href="gallery.php?gid='.$result['id'].'">'.$result['title'].'</a>'. "&nbsp;" . "|" . "&nbsp;";
} 
 }
Right now it shows all subcategories from every category, what i want is this :

Cat A
Cat B - subcatB_b
Cat C - subcatC_c

so e.g. when i click on Cat C it should only show subcatC_c where as its showing 'subcatB_b' and 'subcatC_c' which is wrong. , lemme know if anyone can help.
I'm not clear on what you want to display. Your example shows Cat A, etc., and subcatB_b, etc., but those are not in your code. What field in the table is the category and what field is the subcategory? Will there ever be more than one subcategory within a category? Next, you must use an ORDER BY clause in your query or you can't be sure that the results will even be in the correct sequence (databases don't guarantee ANY particular order of rows, you must specify that in your query).

So you need to be clear about what you want to see, and what is in your database.

And yes, you need quotes around the array index $result['nest_under'], just like in the other places you specify an array index.

Re: How to show Subcategories under a category

Posted: Tue Sep 20, 2011 11:05 pm
by flying_circus
I'm with Don, a little lost. So I thought maybe I might offer some free advice, and it may help you solve the problem another way.

When I create a category structure in a database, each category is assigned an id, a parent id, and a name

Id, Parent, Name
1, 0, "Cat A"
2, 1, "Cat B"
3, 2, "Cat C"
4, 0, "Cat D"

In this case, we assume that those categories who have a parent category id of 0, are in the menu root.

Through a series of queries, I can rebuild the path. Cat C is a child category of Cat B, which is a child category of Cat A. Cat A and Cat D are siblings in the menu root.

[text]Cat A
Cat B
Cat C
Cat D[/text]
Does that help at all?