Concerning parent/child array relationship - php

This is my database structure:
I would like to loop through all the child elements (those who's parent_id is set to the data_id within the same table).
Therefore those with "parent_id" as "0" are parents, and those with parent_id with a number in it is a child.
I would like help creating a function that will generate tables, with the table itself as the parent, and all the rows within it would be children of that parent. There is only one layer of depth in this project (one parent and one child layer always. ).
Can anyone help, or would want a more detailed description?
Thank you, and looking forward to a reply.

From my perspective, as when I look at your database, I assume that your parent will always be added before your child is (If I am wrong, please correct me). And because you said that you only have 1 parent and 1 child, I believe this is how your database would look like:
1st - parent
2nd - child of 1st
3rd - parent
4th - child of 3rd
If that's the case, one single loop can help you out with this pseudo code
//get the data from the database
//run through the loop
//check if the parent_id is 0
// if it is, create an element (a table) to be the container with the id as cited in your data_id
// if is is not, create an element (a row). Then append this element to the table with the same id as this parent_id

Related

How to list most recently updated parent upper in the query?

I have pic related parent/child tables. For every one row in the parent table, there can be like 100 or 200 child rows in the child table. When I upgrade the child table; I also give it's parent row's tID number to child's row under thread ID column. So, there isn't more than one parent row to each child row.
If I had a column in the parent table like "LastChildTimestamp" could I upgrade related row with the last child timestamp? If so can you show me an example?
What I want to achieve is to keep the most recently updated parents upper when I list out the parents and I want to make it easy. So if you have a better solution or method I'm open to it too.
There are two different approaches you could use to solve this problem.
Create the parent.LastChildTimestamp field, then use a database trigger to update it.
See: http://www.techonthenet.com/mysql/triggers/after_update.php
In particular, you would set up after insert and after update triggers in the child table that would update the associated parent row.
Alternately, you could instead simply aggregate the data from the child rows when you need to read it. Your query would looks something like:
SELECT MAX(TIMESTAMP)
FROM child
WHERE child.threadID = (thread ID)

Logic for DB with Ternary tree structure

Currently I need to build a structure for a MLM company. The structure they wanted is something like a ternary tree structure, which have one parent node links to 3 child node.
But for the top most node (root), it can have more than 3 child nodes while all other node will stick to the limit of three. Currently my idea for the structure was storing one ancestor parent node (the parent of the parent node) and also 3 child node as a row of record in DB. The method of keeping track is to store the depth and also node number of the user currently in. The node number will start from 1, left to right on new depth.
But the problem arise when i tried to insert, as I have no optimize solution to identify the root user from other user when inserting to available node (unused node number) as I only keeping track the nodes of depth one, I will need to loop the query to select the next depth level to get the list of empty node (and the loop goes on if no available node found). Currently every info is stored in a single table, 1 to 1 relation. I'd like to know if there is any other effective way of doing this ?
Currently coding is done in php and MySql.

Building tree structure in a database-how to fill in the tree one child node at a time?

I'm building a tree in a MySQL database using an adjacency list so each row has a node_id and a parent_id.
The tree will be filled with data over time and I need to fill it in a certain way:
Each node has a max of three child nodes
Fill in each node in the current level one child at a time horizontally
After all nodes in the current level have one child, return to the beginning of the current level and fill in child node 2 for each node, then repeat with the last child
After the last node in the current level has three children, start filling in the next level in the same way
So basically I just need help with the algorithm that will determine the parent_id of the node I am inserting. I'd rather not have to traverse the tree with every insert as it will get quite large, but that's fine if it's the only way. I'm building the logic in PHP but pseudocode is fine. Thanks!

checking value in n-depth tree?

I have two entities, post and category which is a 1:n relationship.
I have a reference table with two columns, post_id,category_id
The categories table has an id column, a status column and a parent_id column
If a category is a child of another category (n-depth) then it's parent_id is not null.
If a category is online it's status is 1, otherwise it is 0.
What I need to do is find out if a post is visible.
This requires:
Foreach category joined to the post trace up it's tree to the root node (till a category has parent_id == null), if any of those categories have status 0 then that path is considered offline.
If any path is online then the post is considered visible, otherwise it is hidden.
The only way I can think of doing this (as semi-pseudo code) is:
function visible(category_ids){
categories = //select * from categories where id in(category_ids)
online = false
foreach(categories as category){
if(category.status == 0)
continue;
children = //select id from categories where parent_id = category.id
if(children)
online = visible(children)
}
return online
}
categories = //select c.id from categories c join posts_categories pc on pc.category_id = c.id where pc.post_id = post.id
post.online = visible(categories)
But that could end up being a lot of sql queries, is there a better way?
If nested sets are not an option, I know about the following:
If the data is ordered so that children of a parent always follow after it's parent, you can solve this with one database-query over all data by skipping hidden nodes in the output.
This works equally with a sorted nested set, too, the principle has been outlined in this answer however the algorithms about getting the depth do not work and I would suggest a recursive iterator that is able to remove hidden items.
Also if the data is not ordered, you can create a tree structure from the (unsorted) query of all rows like outlined in the answer to Nested array. Third level is disappearing. No recursion needed and you get a structure you can easily output then, I should have covered that for <ul>/<li> html style output in another answer, too.
Answer to How can I convert a series of parent-child relationships into a hierarchical tree?
Answer to How to obtain a nested HTML list from object's array recordset?
A classic database vs memory tradeoff. What you are doing is building a tree with leafs in it. To build the tree you need recursive loop the leafs. Coming from a database there are 2 scenarios:
Build the tree recursive with a query for each leaf. You hold 1 tree in memory. That is what you are doing.
Get a flat structure from the database, and build the tree recursive in memory. You hold a flat tree and the real tree in memory. That is your alternative way.
What is better depends on a lot of things: your hardware (disk access vs memory), the size of the tree to name two.

"self-referencing" table and arrays

MySQL table:
categoryID
categoryName
categoryParent
Every category has ONE parent category, though it can be NULL, which I treat as the root-category.
I want to get all categories from the table, store it in an array and print it in a way, that shows the nesting.
Example:
ID name parent
1 a NULL
2 b NULL
3 c NULL
4 b1 2
5 d NULL
6 b2 2
HTML:
a
b
-b1
-b2
c
d
Later I'll try to make it draggable with jQuery so the user can choose the parent/child category by him-/herself.
Can I do all this with one single table or do I need an external junction table?
Your table structure is fine.
You'll render the nesting when you deal with the results; perhaps you loop through each result with no parent and append to the DOM, then loop through each result with a parent appending to existing elements. That only works for a two-level tree, but you get the idea.
Yes you can, just select all the rows from table. And using php, you can generate a good html table using a for loop. When you see an element with a parent, just process the table string you are constructing in your php script. From there using JQuery you can change the orders and parent child relationship of the table.
You can nest arrays and have a structure like (Array of (me, childrenArray)).
That way you use the "me" for indexing.

Categories