2

I'm using Google Orgchart in my project. In that I'm returning JSON OBJECT from PHP file.

Problem

My Problem is when I hardcode the value, It works fine. When I return data from PHP file. It did not work. I guess the data format which is returning from PHP file is not correct. File below.

$result = mysql_query("SELECT * FROM emp"); 
    while($row = mysql_fetch_array( $result )) {
        $arr1 = array(
            'v' => $row['name'],
            'f' => $row['name']+'<div style="color:red; font-style:italic">President</div>',
            '' => $row['rep'],
            '' => $row['des'],
        );
        array_push($dataarray, $arr1);
    }

echo json_encode($dataarray);

which returns object like below

enter image description here

How it Should be

My hardcorded JSON OBJECT below

   [
      [{v:'Prabhkar', f:'Prabhkar<div style="color:red; font-style:italic">President</div>'},
       '', 'The President'],
      [{v:'Raguram', f:'Raguram<div style="color:red; font-style:italic">GM</div>'},
       'Prabhkar', 'GM']
    ]

Console Screenshot below:

enter image description here

Do I need to create a one more array in PHP file. How I suppose to change the PHP array according to above screenshot. sorry for my english. Thank you.

2
  • Show the JSON output you get echo json_encode($dataarray);.. Also, what's stored in $dataarray initially? Commented Apr 18, 2017 at 6:28
  • @ObjectManipulator, I have parsed data from $dataarray and shown result in screenshot res. Commented Apr 18, 2017 at 6:30

3 Answers 3

2

You need to wrap 'v' and 'f' in a array, then push other values to parent array.

$result = mysql_query("SELECT * FROM emp"); 
    while($row = mysql_fetch_array( $result )) {
        $arr1 = array(
            array(
                'v' => $row['name'],
                'f' => $row['name'] . '<div style="color:red; font-style:italic">President</div>'
            ),
            $row['rep'],
            $row['des']
        );
        array_push($dataarray, $arr1);
    }

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

Comments

1

In your hard coded array the first key has an array inside so you must change your code like this

$result = mysql_query("SELECT * FROM emp"); 
$dataarray = [];
while($row = mysql_fetch_array( $result )) {
    $arr1 = array(
        array(
              'v'=> $row['name'], 
              'f' => $row['name'].'<div style="color:red; font-style:italic">President</div>',),
        $row['rep'],
        $row['des'],
    );
    array_push($dataarray, $arr1);
}

echo json_encode($dataarray);

Comments

1

Your internal structure is wrong. Your internal structure is an array, with the first being a map, followed by two values. Your current implementation is an array, with only a map.

$result = mysql_query("SELECT * FROM emp"); 
    while($row = mysql_fetch_array( $result )) {
        $arr1 = array(
            array(
                'v' => $row['name'],
                'f' => $row['name'] . '<div style="color:red; font-style:italic">President</div>',
            ),
            $row['rep'],
            $row['des']);
        array_push($dataarray, $arr1);
    }

echo json_encode($dataarray);

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.