こんな感じのJSONデータを作りたい
[{"名前":"AS","データ":["150","250","300"]},{"名前":"JS","データ":["175","180" ,"210"]},{"名前":"MS","データ":["100","75","200"]}]
ここに私が作成したスクリプトがあります
$c = mysql_query("SELECT distinct nama FROM tcoba ORDER BY nama ASC");
while($ca = mysql_fetch_array($c))
{
$cb[] = $ca['nama'];
}
$cc = array();
$cc = count($cb);
if(count($cb) > 1)
{
for($i=0;$i<$cc;$i++)
{
$b = mysql_query("SELECT distinct nama, jumlah FROM tcoba WHERE nama = '$cb[$i]'");
$rows = array();
while($ba = mysql_fetch_array($b))
{
$rows['name'] = $ba[0];
$rows['data'][] = $ba['jumlah'];
}
$result = array();
array_push($result,$rows);
print json_encode($result);
}
}
私のスクリプトの結果は
[{"名前":"AS","データ":["150","250","300"]}][{"名前":"JS","データ":["175","180 ","210"]}][{"名前":"MS","データ":["100","75","200"]}]
見せたいものとまだ一致していない...
編集:仕事
$result = array();
for($i=0;$i<$cc;$i++)
{
$b = mysql_query("SELECT distinct nama, jumlah FROM tcoba WHERE nama = '$cb[$i]'");
$rows = array();
while($ba = mysql_fetch_array($b))
{
$rows['name'] = $ba[0];
$rows['data'][] = $ba['jumlah'];
}
array_push($result,$rows);
}print json_encode($result);