I am designing an app with codeigniter and generating dynamically my arrays and pushing them to my views. Although it seems straitforward I had some problems though.
In my Model function I generate my arrays like this :
foreach ($Q->result_array() as $row)
{
$data[]=array(
'label'=>$row['students'],
'data'=>$row['marks']
);
}
Then I return $data
In my view I use json_decode like shown here
<script>
data = <?php echo json_encode($lang);?>;
data=trim("label", '"');
//alert(data);
$.plot($("#graph4"), data,
{
series: {
pie: {
show: true,
radius: 1,
label: {
show: true,
radius: 3/4,
formatter: function(label, series){
return '<div style="font-size:8pt;text-align:center;padding:2px;color:white;">'+label+'<br/>'+Math.round(series.percent)+'%</div>';
},
background: {
opacity: 0.5,
color: '#000'
}
}
},
legend: {
show: false
}
});
</script>
But the json generated is not compatible with flotcharts library, instead of having
[{label:"Mark",data:1},{label:"Jenny",data:1}]
I have
[{"label":"Mark","data":"1"},{"label":"Jenny","data":"1"}]
How can I remove the double quotes so that it can be only applied to labels and not the data
Any help would be appreciated,
Thanks
UPDATE :
For those who will face this problem I solved the problem with regular expression
echo preg_replace('/"([^"]+)"\s*:\s*/', '$1:',json_encode($lang));
You could construct it manually rather than with json functions. I've used something like this in the past, which you can use inside the foreach of your Model:
$data .= '{label:'.$row["students"].',data:'.$row["marks"].'},';
and after the loop:
$data = "[{$data}]";