0

[{"section_name": "Section A","data": [{"value": "2"},{"value": "0"}]}, {"section_name": "Section B","data": [{"value": "1"},{"value": "0"}]}]

In this I am retrieving "section_name","value" from database.

Here the concept is get values from database to display a bar chart, based on classes and sections.

But my template I had chosen, I have to get the values(no. of students in a class & section) for individual sections, as shown in below bar chart.

My PHP Code:

$result1 = mysql_query("select class_name as label from class ") or     die(mysql_error());

while($row1 = mysql_fetch_assoc($result1))
{
    $rows1[] = $row1;
}
$data1 = json_encode($rows1);

$result2 = mysql_query("select s.section_name as sectionname, 'data' as data from section s") or die(mysql_error());

$result3 = mysql_query("select (select count(*)as c from student_status ss where ss.section_id=s.section_id and ss.class_id=c.class_id) as value from section s, class c order by s.section_name asc ") or die(mysql_error());

if(mysql_num_rows($result2) > 1)
{
    while($row2 = mysql_fetch_assoc($result2))
    {
        $rows2[] = $row2;
    }
    $data2 = json_encode($rows2);

while($row3 = mysql_fetch_assoc($result3))
{
    $rows3[] = $row3;
}
$data3 = json_encode($rows3);

}

My JSON CODE:

"categories": [
        {
            "category": <? echo $data1 ?>/*[
            {
                "label": "1"
            },
            {
                "label": "TWO"
            },
            {
                "label": "3"
            }
            ]*/
        }
    ],
    "dataset": /*<? echo $data2 ?>*/[
        {
            "sectionname": "Section A",
            "data": [
                {
                    "value": "2" //class 1
                },
                {
                    "value": "1" //class 2
                }
            ]
        },
        {
            "sectionname": "Section B",
            "data": [
                {
                    "value": "" //class 1
                },
                {
                    "value": ""  //class2
                }
            ]
        }
    ]

I am using this json in ajax Hope you guys understand question. https://i.stack.imgur.com/mlfLJ.jpg

Machavity
  • 30,841
  • 27
  • 92
  • 100
R K Rao
  • 41
  • 6
  • You need to [stop using mysql_ functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) as they are removed in PHP 7 – Machavity Feb 23 '16 at 14:55
  • that's just plain nasty. You should **NOT** be embedding php values into json like that. You're highly likely to introduce invalid text causing json parse errors. In other words, **NEVER** manually build json, when you could just build a native php array data structure, then do a single `json_encode()` call on that array. Boom, done. – Marc B Feb 23 '16 at 14:57
  • And as for "accessing" the json - you don't. json is a transport/encoding format. It's literally just a plaintext-string. You decode the json into a native data structure, and access your data via that structure. – Marc B Feb 23 '16 at 14:58
  • Hello Marc B, It seems your words are very useful, I can't understand what you are saying, let me know briefly, regards. – R K Rao Feb 23 '16 at 15:07

1 Answers1

0

You can't act in your way. To produce a valid JSON string, you can write this code:

$data = array( array('category'=>$data1), array('section'=>$data2), array('category'=>$data3) );
$json = json_encode( $data );

Or — if you have more $dataN arrays — by this way:

$data = array();

// Your first MySQL query here
$data[] = array( 'category' => $rows1 );

// Your second MySQL query here
$data[] = array( 'section' => $rows2 );

// (...)

$json = json_encode( $data );

Please also note:

mysql_ syntax was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used.

(from PHP Official site)

fusion3k
  • 11,568
  • 4
  • 25
  • 47
  • But I make my application 2 years ago and now I am doing some modifications. Here I want to introduce my bar-chart, to attract some clients. Any way thanks for your answer. – R K Rao Feb 23 '16 at 15:14
  • @RKRao you have to compare 3rd query with ‘section’ array, or to group 2nd and 3rd queries – fusion3k Feb 23 '16 at 15:17
  • Great, your answer give me an idea to solve it. Soon I will post the answer. – R K Rao Feb 24 '16 at 11:06