简体   繁体   中英

convert sql result as nested structure javascript object in php

I want to create chart of account object with some parent & child relation. based on these sql results :

autoid  account_code    account_name         parentid   account_level
1       100-000         assets                  0                 1
2       200-000         liabilities             0                 1
3       110-000         cash                    1                 2
4       120-000         bank                    1                 2
5       110-001         petty cash              3                 3
6       120-001         bank no 1               4                 3
7       120-002         bank no 2               4                 3
8       210-000         Current liabilities     2                 2
9       210-001         Account Payable         8                 3

As you can see I have parentid column that correlate with autoid column. for example I want to convert these result to nested object :

[{
    account_code:'100-000',
    account_name:'assets',
    account_level:1,
    parentid:0,
    child:[{
        account_code:'110-000',
        account_name:'cash',
        account_level:2,
        parentid:1,
        child:[{
            account_code:'110-001',
            account_name:'petty cash',
            account_level:3,
            parentid:3,
            child:null
        }]
    }]
},
{
    account_code:'200-000',
    account_name:'liabilities',
    account_level:1,
    parentid:0,
    child:[{
        account_code:'210-000',
        account_name:'Current Liabilities',
        account_level:2,
        parentid:2,
        child:[{
            account_code:'210-001',
            account_name:'Account Payable',
            account_level:3,
            parentid:8,
            child:null
        }]
    }]
}]

I have options in mind to generate this

  1. generate & format js object in php.
  2. send 3 json for each level and combine these objects with lodash.

I don't know which solutions is perfect for these requirement.

Easy way to use jstree library. You can use next code:

PHP:

function getJsonDataForJstree($dbDataArr)
{
  $arrOfString = array();
  foreach($dbDataArr as $row){
      $parentId = $row == 0 ? '#' : "custom_elem_$row[parentid]";
      $arrOfString[] = '{ "id" : "custom_elem_$row[autoid]", "parent" : $parentId, "text" : "$row[account_name]" }';
  }
  $resJsonData = '['. implode(',', $arrOfString) .']';
  return $resJsonData;
}
$dbDataArr = getDataFromDataBase();
$jsonDataForJstree = getJsonDataForJstree($dbDataArr);

HTML:

    <head>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/themes/default/style.min.css" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/jstree.min.js"></script>
    </head>
    <body>
    <input type='hidden' id='jstree-json-data' value='<?php echo $jsonDataForJstree?>' />
    <div id='jstree-container'></div>
    </body>

Javascript/Jquery:

var jsTreeData = jQuery('#jstree-json-data').val();

$('#jstree-container').jstree({ 'core' : {
    'data' : jsTreeData 
} });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM