i am running PHP Version 5.2.9, i want to create a barchart from data in mysql. i got this sumple and tried running it. am having a bit if trouble with json_encode(). i have an array that has numbers and strings but the numbers get printed back as strings. how do i make sure the numbers are printed back as nunmbers. the code i have is below. I ddnt update because the server i want to to use also uses php 5.2.* (000webhost)

$query = mysql_query("SELECT month, wordpress, codeigniter, highcharts FROM project_requests");

$category = array();
$category['name'] = 'Month';

$series1 = array();
$series1['name'] = 'Wordpress';

$series2 = array();
$series2['name'] = 'CodeIgniter';

$series3 = array();
$series3['name'] = 'Highcharts';


while($row = mysql_fetch_assoc($query)) {
    $category['data'][] = $row['month'];
    $series1['data'][] = $row['wordpress'];
    $series2['data'][] = $row['codeigniter'];
    $series3['data'][] = $row['highcharts'];   
}

$result = array();
array_push($result,$category);
array_push($result,$series1);
array_push($result,$series2);
array_push($result,$series3);


print json_encode($result, JSON_NUMERIC_CHECK);
mysql_close($con);

the above code says json_encode requires 1 parameter and 2 are given

print json_encode($result, JSON_NUMERIC_CHECK);

, when i remove the second parameter to get

print json_encode($result);

, the graph i want renders but no bars as the numbers are returned as strings.

Member Avatar for diafol

CHeck out intval or cast variable with (int)

<?php

$arr = array("12","13","14");
echo json_encode($arr);

echo "<br />";

$arr = array_map("intval",$arr);
echo json_encode($arr);


?>

Was assuming int. You can do same with float. (float) and floatval

the suggestion does not work. my initial output with the second parameter removed from json_encode()

[{"name":"Month","data": ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]},{"name":"Wordpress","data":["4","5","6","2","5","7","2","1","6","7","3","4"]},{"name":"CodeIgniter","data":["5","2","3","6","7","1","2","6","6","4","6","3"]},{"name":"Highcharts","data":["7","8","9","6","7","10","9","7","6","9","8","4"]}]
The values in the square brackets are strings, i want them to be numeric. when i do what you suggested i only get an out put like

[1,1,1,1]
the output i desire is every month with a bar

Member Avatar for diafol

Because your intvalling the whole arrays not the value array in question

Member Avatar for diafol

Are you storing the wordpress, codeigniter... as varchar?

while($row = mysql_fetch_assoc($query)) {
    $category['data'][] = $row['month'];
    $series1['data'][] = intval($row['wordpress']);
    $series2['data'][] = intval($row['codeigniter']);
    $series3['data'][] = intval($row['highcharts']);   
}

Should work though.

thanx it works now

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.