-4

My Output must be like this

[
    {"Airtel":{"v": 50.00}},
    {"Hutch":{"v": 10.00}},
    {"Idea":{"v": 10.00}},
    {"TATA":{"v": 10.00}},
    {"Vodafone":{"v": 20.00}},
    {"Aircel":{"v": 15.00}}

]

Im using the data from Mysql so I cant put the data directly in the Code(Php). . . Im using the below code to get a data from the datbase

   <?php 
$con=mysql_connect("localhost","root","") or die("Failed to connect with database!!!!");
mysql_select_db("chart", $con); 

$sql=mysql_query("select * from googlechart"); 

$response = array();
$posts = array();



$i=0;
while($row=mysql_fetch_array($sql)) { 

$response[$i]['url']  = $row['weekly_task']; 
$response[$i]['title']= $row['percentage']; 
$data['posts'][$i] = $response[$i]; echo "\n";
$i=$i+1;
} 

$json_string = json_encode($data);

$file = 'json.aspx';
file_put_contents($file, $json_string);

?> 
4

2 回答 2

3

根据您想要的输出,构建正确的 php 数组:

$companyName = YOUR_DATA_FROM_SQL ; // $row[something..]
$percentage = YOUR_DATA_FROM_SQL ; // $row[something..]
$data[$companyName] = array('v' => $percentage) ;

// Then
json_encode($data) ;
于 2013-09-19T08:02:13.187 回答
0

可以试试这个,

你的循环从这里开始,

$response[$company] = array('v' => $percentage);
$data[$i] = $response[$company]; // creates array of tasks

json_encode($data);
于 2013-09-19T08:06:13.280 回答