简体   繁体   English

格式化MYSQL的PHP​​输出以获取morris.js图表​​数据

[英]Format PHP output from MYSQL for morris.js chart data

I have the following query for retrieving data in my MYSQL database: 我有以下查询用于检索我的MYSQL数据库中的数据:

SELECT a.PARNT_CIVSTATUS,count(a.PARNT_CIVSTATUS) from tbl_parnt a left join tbl_intrvw b 
on a.QN_NUMBR=b.QN_NUMBR left join tbl_barangay c on b.ZONE_NUM=c.BRGY_ZONE_NUM 
group by a.PARNT_CIVSTATUS

This is the output from phpmyadmin: 这是phpmyadmin的输出:

phpmyadmin输出

And this is the output using json_encode in PHP: 这是在PHP中使用json_encode的输出:

[
{"0":"L","PARNT_CIVSTATUS":"L","1":"14","count(a.PARNT_CIVSTATUS)":"14"},
{"0":"LSP","PARNT_CIVSTATUS":"LSP","1":"9","count(a.PARNT_CIVSTATUS)":"9"},
{"0":"M","PARNT_CIVSTATUS":"M","1":"4386","count(a.PARNT_CIVSTATUS)":"4386"},
{"0":"N","PARNT_CIVSTATUS":"N","1":"45","count(a.PARNT_CIVSTATUS)":"45"},
{"0":"NON","PARNT_CIVSTATUS":"NON","1":"1","count(a.PARNT_CIVSTATUS)":"1"},
{"0":"O","PARNT_CIVSTATUS":"O","1":"12","count(a.PARNT_CIVSTATUS)":"12"},
{"0":"S","PARNT_CIVSTATUS":"S","1":"681","count(a.PARNT_CIVSTATUS)":"681"},
{"0":"SGP","PARNT_CIVSTATUS":"SGP","1":"143","count(a.PARNT_CIVSTATUS)":"143"},
{"0":"SP","PARNT_CIVSTATUS":"SP","1":"148","count(a.PARNT_CIVSTATUS)":"148"},
{"0":"SPG","PARNT_CIVSTATUS":"SPG","1":"12","count(a.PARNT_CIVSTATUS)":"12"},
{"0":"W","PARNT_CIVSTATUS":"W","1":"239","count(a.PARNT_CIVSTATUS)":"239"},
{"0":"WGW","PARNT_CIVSTATUS":"WGW","1":"1","count(a.PARNT_CIVSTATUS)":"1"},
{"0":"WW","PARNT_CIVSTATUS":"WW","1":"2","count(a.PARNT_CIVSTATUS)":"2"}
]

I don't know how to format it like this: 我不知道如何格式化它:

{ y: 'L', a: 10 },
{ y: 'LSP', a: 75 },
{ y: 'M', a: 50 },
{ y: 'N', a: 75 },
{ y: 'SGP', a: 50 },
{ y: 'SP', a: 75 },
{ y: 'W', a: 57 }

It's for a morris.js bar chart data. 用于morris.js条形图数据。 How do I format it this way? 如何以这种方式格式化?

This will require modification in more than just your sql, but it is easily possible; 这不仅需要对sql进行修改,而且很容易实现。 firstly, using the AS keyword , you can create an alias for the columns which you reference: 首先,使用AS关键字 ,可以为您引用的列创建别名:

SELECT a.PARNT_CIVSTATUS AS 'y', count(a.PARNT_CIVSTATUS) AS 'a' 
FROM tbl_parnt a
LEFT JOIN tbl_intrvw b ON a.QN_NUMBR=b.QN_NUMBR 
LEFT JOIN tbl_barangay c ON b.ZONE_NUM=c.BRGY_ZONE_NUM 
GROUP BY a.PARNT_CIVSTATUS

The above, assuming I did things correctly, should give you this output . 假设我做的正确,上面的代码应该为您提供此输出

However, that still leaves the php side of things. 但是,这仍然留在php方面。 Given you haven't posted your php, I'm going to address this in the two ways I know how; 鉴于您尚未发布php,我将以我所知道的两种方式解决此问题; deprecated mysql_* functions, and PDO: 不推荐使用的mysql_*函数和PDO:

Firstly, the deprecated functions. 首先, 不推荐使用的功能。 If you're using these, I highly recommend against doing so, and changing to either PDO or mysqli; 如果您正在使用这些,我强烈建议您不要这样做,而应改为PDO或mysqli; anyway, to the code! 无论如何,要代码!

$arr = array();
while ($row = mysql_fetch_assoc($result)) {
    $arr[] = $row;
}

The above uses mysql_fetch_assoc() , and the example enclosed within that documentation page. 上面使用了mysql_fetch_assoc()和该文档页面中随附的示例。 It fetches an associative array of the results, assuming they are like the above SQLFiddle demo I provided, and shoves them into an existing array; 假定它们类似于我提供的上述SQLFiddle演示,它将获取结果的关联数组,并将其推入现有数组中; this array would of course then be used in json_encode to produce the desired result. 然后,当然可以在json_encode中使用此数组以产生所需的结果。

The second method I spoke, PDO, or php Data Objects, is one I stand by; 我讲的第二种方法是PDO或php数据对象,这是我支持的方法。 I'm not going to get into a complete demo here, as that's out of the scope of this answer, but will replicate the above code in PDO: 我不会在这里进行完整的演示,因为这超出了此答案的范围,但是将在PDO中复制以上代码:

$arr = $stmt->fetchAll(PDO::FETCH_ASSOC);

Here, I use fetchAll() to fetch every row, and use the constant PDO::FETCH_ASSOC to make sure it only gets the column names and their values; 在这里,我使用fetchAll()来获取每一行,并使用常量PDO::FETCH_ASSOC来确保它仅获取列名及其值。 otherwise, you get what you had above. 否则,您将获得以上的收益。

There is of course a third option, or even a fourth. 当然,有第三个选择,甚至第四个选择。 Mysqli, and some custom-made database abstraction layer. Mysqli,以及一些定制的数据库抽象层。 I'm not experienced in either, and thus I won't be providing any examples save php.net's mysqli quick start guide . 我没有这方面的经验,因此除了php.net的mysqli快速入门指南之外 ,我将不提供任何示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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