How to show Subcategories under a category

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
reachmou
Forum Newbie
Posts: 1
Joined: Tue Sep 20, 2011 5:50 am

How to show Subcategories under a category

Post 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.
weismana81
Forum Newbie
Posts: 20
Joined: Mon Feb 07, 2011 3:36 am

Re: How to show Subcategories under a category

Post 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:
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: How to show Subcategories under a category

Post 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.
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: How to show Subcategories under a category

Post 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?
Post Reply