Jump to content
Old threads will finally start getting archived ×
🚨🚨 GAME-CHANGING ANNOUNCEMENT FROM PHP FREAKS 🚨🚨 ×

Recommended Posts

I have gone brain dead.. :P  in the last day..

 

I've got a list of categories, that I need to figure out the hierarchy for.

 

They are in this format:

 

one

one::sub

one::sub::subsub

two

two::sub

two::sub::subsub

etc....

 

It's multiple levels deep. 

I need to be able to figure out what is the parent, child of each level so i can add it to the database. 

 

Any ideas on this?  I'm guessing using Explode, but I'm lost on how to do this.

 

Thanks

 

-Brad

 

Link to comment
https://forums.phpfreaks.com/topic/172932-exploding-a-list/
Share on other sites

It's quite easy in this format. Create a class named node which contains the information about the node:

- Node value

- Node parent

- The list of node children

 

Furthermore, create an assotiative array of such nodes ($nodes). Now, you take a single category, explode it against '::' and take a look at the last two elements. Let $cnt be the number of elements in the exploded category. Then $category[$elements-1] represents the category name, and $category[$elements-2] - the parent name. You create a new node object and fill in the fields:

 

- To node parent, you put $nodes[$category[$elements-2]] - so, the reference to the parent object.

- To node value, you put $category[$elements-1] - the category value.

- To node children, you put an empty array (actually, this can be done automatically with the constructor).

 

You still have two things to to: the parent must know that it has a new children, and the new node must be registered in $nodes. The first thing can be solved by taking the parent node and appending the new node to its list of children. The last thing - you simply put the new node into the array under the category name: $nodes[$category[$elements-1]] = $newNode;. That's it. If you remember the root node in a separate variable, you will get a fully functionable tree in the memory.

Link to comment
https://forums.phpfreaks.com/topic/172932-exploding-a-list/#findComment-911418
Share on other sites

@shamuraq 

 

I need to be able to populate a database (which I can do)

but I need to be able to find out the parent categories from the text file that I'm given.

 

So like before:

 

Cars

Cars::4 Door

Cars::4 Door::economy

Trucks

Trucks::4x4

Trucks::4x4::Leather

 

so I need to be able to parse out Cars as a parent, then 4 Door as a child of Cars, and Economy as a child of 4 Door.

 

so the database table would look something like this:

 

catid:int autoinc

parent:int default null

Name:varchar(100)

 

catid, parent, name

1, null, Cars

2, 1, 4 Door

3, 2, economy

4, null, Truck

5, 4, 4x4

6, 5, Leather

 

I'm having a problem with the logic of exploding the categories out into there parent categories.

I think once I sleep on it I will figure it out, but I need to get it done asap.  I know it's relatively easy.

 

 

 

-Brad

Link to comment
https://forums.phpfreaks.com/topic/172932-exploding-a-list/#findComment-911420
Share on other sites

@Zyx 

 

That sounds like a great idea except I'm not so good with classes, i'm trying to get away from procedural programming and do the oop but I've yet to figure out everything I need to do.

 

Do you think you could spare the time to put something together on this?  I'd appreciate it.

 

-Thanks Brad

 

 

Link to comment
https://forums.phpfreaks.com/topic/172932-exploding-a-list/#findComment-911421
Share on other sites

Then use an array instead of a class - all you need to save the data about a certain node somehow and objects are very convenient here. If you are using arrays, beware on assigning the nodes to something else. Objects are passed by reference, but arrays - by values, which we do not want here. You must either store the reference to other array or simply the name of the node and look for it in the $nodes array every time you need the data from this node.

Link to comment
https://forums.phpfreaks.com/topic/172932-exploding-a-list/#findComment-911449
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.