Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7785475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:15:43+00:00 2026-06-01T20:15:43+00:00

I have a table with data stored hierarchicaly using adjacency list like in the

  • 0

I have a table with data stored hierarchicaly using adjacency list like in the following example

id     account parent
1      root    null
2      a1      1
3      b1      1
4      a2      2
5      a3      4
6      a4      2
7      b2      3

And a table where I keep values for these accounts

id_account    value
2             10
2             10
4             20
5             30
6             10 

I made a function which returns all child account given a parent account:

function getChildrenAccount($parent_id)
  {
    $query = "SELECT id, account FROM accounts WHERE parent='{$parent_id}' ORDER BY account";
    $result = mysql_query($query) or die(mysql_error());
    while($r[]=mysql_fetch_assoc($result));
    return $r;
  }

What I’m trying to do is a function which returns not only children accounts but also the sum of all values including children of every result. For example

getChildrenAccount(4)

Would return an array with the following syntax

array(1) { 
  [0]=> array(3) { 
     ["id"]=> 5
     ["account"]=> "a3"
     ["sum"]=> 50 //a2 + a3
}

And

getChildrenAccount(2)
array(2) { 
  [0]=> array(3) { 
     ["id"]=> 4
     ["account"]=> "a2"
     ["sum"]=> 70 //a1 + a2 + a3
  [1]=> array(3) { 
     ["id"]=> 6
     ["account"]=> "a4"
     ["sum"]=> 30 //a1 + a4
}

I think I have to use some kind of recursion in my while statement but I’m a little bit confused. Can you help me please?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T20:15:44+00:00Added an answer on June 1, 2026 at 8:15 pm

    I found a function which gets results as needed without recursive queries.

    $nodeList = array();
    $tree     = array();
    $query = mysql_query("SELECT A.guid, A.name, A.parent_guid, SUM( S.value_num ) /100 AS suma FROM accounts AS A
        LEFT JOIN splits AS S ON S.account_guid = A.guid GROUP BY A.guid ORDER BY A.name");
    while($row = mysql_fetch_assoc($query))
      $nodeList[$row['guid']] = array_merge($row, array('children' => array()));
    mysql_free_result($query);
    
    foreach ($nodeList as $nodeId => &$node) {
      if (!$node['parent_guid'] || !array_key_exists($node['parent_guid'], $nodeList))
        $tree[] = &$node;
      else 
        $nodeList[$node['parent_guid']]['children'][] = &$node;
    }
    unset($node);
    unset($nodeList);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have data stored using tinymce with placeholders before each table Editor view when
How to update table data with data stored in session? I have following code
I have table in MySQL where the data are stored like: table_1 +-------+-------+ |field_0|field_1|
Greetings, I have data stored on mysql with delimiter , in 1 table. I
Say I have some data stored in an audit table, where triggers on the
I have a table Email with field contents where the data are stored as
I have a MySQL table that contains datetime data stored in it. I need
i have a table array dynamically generated from a data query and stored in
I have a table that contains some sensitive data that I would like to
I have a mysql table field set as time type which stores data in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.