Skip to content
Advertisement

Get the ancestral data from database dynamically

I would like to get the ancestral or child data when I pass the value in a function.

This is my data from MySQL table.

enter image description here

If I pass the value of Document2 in my function i want to get the ancestral data, for example, if I pass Document2 it will get the Book2 then the Book2 will get the Document1 and Document1 will get the Book1 and so on, then same with the Profile1 it will get the Document0 dynamically.

Here’s my code.

JavaScript

The problem with my code is it will not dynamically get ancestral data if the passed value in the function has more than 2 ancestral.

Advertisement

Answer

DBFIDDLE

  • I did choose to separate the recursive parts with a -.
JavaScript

output:

b
Document0-Profile1-Document2
Book1-Document1-Book2-Document2
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement