473,385 Members | 1,796 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Caculating Nestled Arrays from Mysql DB

Some nestled calculation.
How did club freedom did the calculation, or how will the database structure looks like?

Please include sample code for me, i`m still learning.

The example is -:

Joseph gave birth to John, James, and Johnson
John gave birth to Peter, Matter, and Potter
James gave birth to Juliana, Justin, and Jane
Johnson gave birth to Jak, Jake and Jacob

Continuously like that,
Peter the son of John also gave birth to another 3 children
And the 3 children also keep giving birth to 3 children each, so
How can I calculate the Total descendants of Joseph?
How can I calculate the total descendants of John or any of the grand children?

Because each of the children also start having grand children, while Joseph grand descendants increases.

I want to use MySql/PHP
The concept is Like http://www.disneytreasures.biz/ or ClubFreedom

All I want to do is to know who bring who?
Apr 10 '08 #1
2 1230
nathj
938 Expert 512MB
Hi,

If I understand this correctly it is quite tricky but not impossible.

Stick with me on this and I think we muight have astructure outlined by the end.

It shold be possible to do this iwth two tables, assumuing that you only want parent to child relationships. (I understand from your examples this not a biological thing but the parent child terms hold good)

table: tbl_person
ID
first_name
last_name
dob

table: tbl_relationship
ID
parentID
childID

If john is the very first person in or he jpins of his own accord he has no parent so he exists in tble_person. John introduces James. James now exists in tbl_person and a new record is added to tbl_relationship with John's ID listed under parentId and James' ID listed under childID.

If Mark joins because of James, James is a child and a parent. so our data looks like:

tbl_person
1, John, Johnson, 01/01/1900
2, James, Jameson, 02/02/1902
3, Mark, Markson, 03/03/1903

tbl_relationship
1, 1, 2
2, 2, 3

With this structure it is possible to travers the data for any given person finding out their direct children and then grandchildren or great grandchildren.

I hope this makes sense for you.

Cheers
nathj
Apr 18 '08 #2
Atli
5,058 Expert 4TB
Hi.

To get the total number of descendants, you would probably need to use a recursive function.

Consider this:
Expand|Select|Wrap|Line Numbers
  1. function count_descendants($parentID) {
  2.   $count = 0;
  3.  
  4.   // Query for all children
  5.   $_sql = "SELECT child_id FROM relationsTable WHERE parent_id = {$parentID}";
  6.   $_result = mysql_query($_sql)
  7.  
  8.   while($_row = mysql_fetch_assoc($_result)) {
  9.     // Add for this child
  10.     $count++; 
  11.  
  12.     // Call this function again for this child
  13.     $count += count_descendants($_row['child_id']);
  14.   }
  15.  
  16.   return $count;
  17. }
  18.  
Apr 18 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: James | last post by:
Hi, I have the following code: <PRE> <?php $username = "####t"; $password = "####"; $hostname = "####"; mysql_connect($hostname, $username, $password) or die("Unable to connect to
12
by: James | last post by:
Hi, Have posted before, but will simplify problem here. For original post go to http://forums.devshed.com/t80025/s.html I have setup 2 arrays like so in my one page script: $carers =...
3
by: Stephen Preston | last post by:
I can't get an $array to be used as a select query on a mySQL database. I have a 'parts list' which displays on a page (works fine) and is a form. At the end of each row is a input type='text'...
16
by: Ian Davies | last post by:
Hello Needing help with a suitable solution. I have extracted records into a table under three columns 'category', 'comment' and share (the category column also holds the index no of the record...
4
by: mantrid | last post by:
Im using arrays generated from my records displayed in a table on my site to update the corresponding records in a mysql database ie on the web page col1 col2 col3 1 2 2 1 ...
2
by: assgar | last post by:
Hi Developemnt on win2003 server. Final server will be linux Apache,Mysql and PHP is being used. I use 2 scripts(form and process). The form displays multiple dynamic rows with chechboxs,...
3
by: wombat | last post by:
I have a question about using arrays in Perl, involving connecting to MySQL databases. In the past I've used PHP to access databases and I've been playing around now with Perl. In PHP to access...
15
by: Maarten | last post by:
I've found some answers to my problem on this forum, but not exactly the answer I was looking for. Sorry if I've missed something. This is my situation: I am trying to make an insertion into an...
3
by: poolboi | last post by:
hi guys, i dunno if this post should be in Perl or MySQL but i'm using Perl DBI to do manupilations now in MySQL i've got problem trying to input 2 arrays of data into 2 fields at the same...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.