0

What would be the cleanest and most efficient way to create a JSON tree representing a table from a MySQL query involving multiple JOINS ?

enter image description here

So far, the php array is created by this loop:

$rs = mysqli_query($connect, $query);
$arr = array();
while ($row = mysqli_fetch_array($rs, MYSQL_ASSOC)) {
    $arr[] = $row;
}

I could then do echo $arr[2]["sold"] and get "2 sodas"

4
  • 1
    possible duplicate of How to build a JSON array from mysql database Commented Aug 12, 2013 at 0:37
  • Can you clarify a bit on the structure of your data in PHP? Right now it looks like $queryResult['date']['employee']['sold'], which would mean it's already in a nested format. Does sold have an employeeId? and likewise does employee have a dateId? If that's the case I'd assume that is how you are performing the JOINs in the first place. Anyways, if the data from the query is flat, there needs to be some way for us to map each object to its parent. Commented Aug 12, 2013 at 0:46
  • I just updated the question... let me know if this isn't answering your questions. Commented Aug 12, 2013 at 1:05
  • I remmoved the multiple IDs stuff for sake of simplicity but yes... i will add them in the tree as this data structure is used to feed a UI Tree widget and when the user will click the node "2 sodas", i will want to know that it is the row XYZ of the SOLD table. Commented Aug 12, 2013 at 1:09

1 Answer 1

0

Well here is my own solution to my own question !... For the benefits of ALL !!! It is working but can anyone suggest a better faster approach ?

    $rs = mysqli_query($connect, $query);
$arr = array();
while ($row = mysqli_fetch_array($rs, MYSQL_ASSOC)) {

    $arr[] = $row;
}
$dateLevel = 0;
$employeeLevel = 0;
$soldLevel = 0;
$tree = array();
$count = count ($arr);
for ($i = 0; $i < $count; $i++){

    $A = $tree[$dateLevel-1];
    if ($A["text"] != $arr[$i]["date"]){
        $tree[$dateLevel]= array("text" => $arr[$i]["date"],  "expanded" => true, items => array());
        $dateLevel++;
        $employeeLevel = 0;
        $soldLevel = 0;
    }

    $A = $tree[$dateLevel-1];
    $B = $A["items"];
    $C = $B[$employeeLevel-1];
    if ($C["text"]  != $arr[$i]["employee"]){
        $tree[$dateLevel-1]["items"][$employeeLevel] = array ("text" => $arr[$i]["employee"],  "expanded" => true, items => array());
        $employeeLevel++;
        $soldLevel = 0;
    }

    $A = $tree[$dateLevel-1];
    $B = $A["items"];
    $C = $B[$employeeLevel-1];
    $D = $A["items"];
    if ($D["text"]  != $arr[$i]["sold"]){
        $tree[$dateLevel-1]["items"][$employeeLevel-1]["items"][$soldLevel] = array ("text" => $arr[$i]["sold"],  "expanded" => true, items => array());
        $soldLevel++;
    }
}

echo json_encode($tree);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.