categories and parent categories

im trying to make my site show the current category and then the parent categories so it shows all of the parent categories back to the root. So to put it simply i want it to do this…

fig1
main cat > another cat > some other cat > another

i sort of have this on the code side through the url, as in it says

fig2
http://www.mysite.com/thecategorysection/thecat

(thing is it doesn’t show all of the cats in the url) its put to the php script for all .htaccess rather than code it to the .htaccess

the sql is…

id - parentid - name
1 - 0 - name
2 - 0 - anothername
3 - 1 - anothernothername
4 - 2 - something else

So thats pretty much it, i had a look around for tutorials on how to make it show fig1 on the page, there a few bits like the one on zend.com, but it won’t show anything. Does anyone have any code snippets or ideas where i can access tutorials that work, thanks in adavance

:)

Forgive me but HUH?

Maybe I am the only one, but I don’t really know what you are asking.

ok soz, erm you know on phpbb just above this article it shows the previous catagories
so…

PHP Help Community Forums Forum Index -> Beginners

well im trying to do this using the categories in the database.

There are tutorials for this but i can’t find one that actually works, they all seem to show nothing

We need more information… How and what information is the database storing. What have you tried so far? Do you have a sample of the code that you have tried?

Give us something to start with. Don’t just throw it out there and say “It’s broke… Fix it?”, give us something to fix.

It’s also impotant to note the OS, The type of Web Server, and database. Include version numbers where possible.

the mysql database is sotring information in a very simple structure. First is the catagory’s id then is the parentid then the name of the catagory

i’ve tried the tutorial on zend
http://www.zend.com/zend/tut/tutorial-f … c=0&view=1

it would show the tree structure but it wouldn’t show the path to the current catagory…

maincat > secondcat > third > Current catagory

System specs…

virtual linux server somewhere in the states
php 4.3.11
mysql 3.23.49

thanks

Okay, I think I know what you’re talkin about, and frankly, I’ve been working on the same thing (and got it working). Here’s what you (probably) want:

Level_1 Category -> Level_2 Category -> Level_3 Category

You can do something like this (as pseudo-code):

// GET ACCESSED CATEGORY
category = mysql_query(SELECT * FROM categories WHERE id = category[id])
// START PATH STRING
path = category[name]

// KEEP GETTING PARENT CATEGORIES UNTIL PARENT-ID EQUALS ZERO
// PARENT-ID = ZERO INDICATES TOPLEVEL CATEGORY
while (category[parentid] > 0) {
  category = mysql_query(SELECT * FROM categories WHERE id = category[parentid])
  // ADD NEW CATEGORY NAMES IN [I]FRONT[/I] OF THE PATH STRING
  path = category[name]." -> ".path
}

// DISPLAY BUILT UP PATH STRING
echo path

You can add the urls, extra info, etc, as you see fit.

Sponsor our Newsletter | Privacy Policy | Terms of Service