PHP_GreenHorn Posted September 7, 2017 Share Posted September 7, 2017 Hi All, My concept: i'm trying to select age and salary from my database. i want to get the SUM of salaries of each age group. Then i want to convery it to JSON, so i can display it in a chart. Output: [{"label":"20","value":"null"},{"label":"30","value":"null"}] Thanks in advance. PHP_GreenHorn Quote Link to comment https://forums.phpfreaks.com/topic/304904-having-trouble-with-array-value-saying-null/ Share on other sites More sharing options...
Sepodati Posted September 7, 2017 Share Posted September 7, 2017 So what have you tried so far? What isn't working? Quote Link to comment https://forums.phpfreaks.com/topic/304904-having-trouble-with-array-value-saying-null/#findComment-1550927 Share on other sites More sharing options...
PHP_GreenHorn Posted September 7, 2017 Author Share Posted September 7, 2017 (edited) My PHP Code: //the SQL query to be executed $query = "SELECT age, SUM(consumer_income) FROM table GROUP BY age"; //storing the result of the executed query $result = $conn->query($query); //initialize the array to store the processed data $jsonArray = array(); //check if there is any data returned by the SQL Query if ($result) { //Converting the results into an associative array while($row = $result->fetch_assoc()) { $jsonArrayItem = array(); $jsonArrayItem['label'] = $row['age']; $jsonArrayItem['value'] = $row['SUM(consumer_income)']; //append the above created object into the main array. array_push($jsonArray, $jsonArrayItem); } } ----------------------------------------------------------------------------------------------- My output: [{"label":"13","value":"0"},{"label":"20","value":"0"},{"label":"30","value":"0"},{"label":"40","value":"0"},{"label":"70","value":"0"},{"label":"90","value":"0"},{"label":"100","value":"0"}] Edited September 7, 2017 by PHP_GreenHorn Quote Link to comment https://forums.phpfreaks.com/topic/304904-having-trouble-with-array-value-saying-null/#findComment-1550929 Share on other sites More sharing options...
ginerjm Posted September 7, 2017 Share Posted September 7, 2017 (edited) At first glance I'd question the format of your summary value. How about using an 'AS newname' clause on your SUM operation? That and the fact that you have it all in quotes thus making it a string for the index name Edited September 7, 2017 by ginerjm 1 Quote Link to comment https://forums.phpfreaks.com/topic/304904-having-trouble-with-array-value-saying-null/#findComment-1550930 Share on other sites More sharing options...
Solution Barand Posted September 7, 2017 Solution Share Posted September 7, 2017 My guess would be that there is something wrong with the data, What is the table structure? Can you show us some sample data? For example, are you storing income values with currency symbols? Don't store ages. It constantly needs updating; store the date of birth instead 1 Quote Link to comment https://forums.phpfreaks.com/topic/304904-having-trouble-with-array-value-saying-null/#findComment-1550931 Share on other sites More sharing options...
PHP_GreenHorn Posted September 8, 2017 Author Share Posted September 8, 2017 My guess would be that there is something wrong with the data, What is the table structure? Can you show us some sample data? For example, are you storing income values with currency symbols? Don't store ages. It constantly needs updating; store the date of birth instead Thanks. I got it working now. My table had ($) currency symbols inserted. Quote Link to comment https://forums.phpfreaks.com/topic/304904-having-trouble-with-array-value-saying-null/#findComment-1550984 Share on other sites More sharing options...
PHP_GreenHorn Posted September 8, 2017 Author Share Posted September 8, 2017 My guess would be that there is something wrong with the data, What is the table structure? Can you show us some sample data? For example, are you storing income values with currency symbols? Don't store ages. It constantly needs updating; store the date of birth in At first glance I'd question the format of your summary value. How about using an 'AS newname' clause on your SUM operation? That and the fact that you have it all in quotes thus making it a string for the index name Thanks. I got it working now. I changed my SUM operation to use the 'AS newname'. . Quote Link to comment https://forums.phpfreaks.com/topic/304904-having-trouble-with-array-value-saying-null/#findComment-1550985 Share on other sites More sharing options...
Barand Posted September 8, 2017 Share Posted September 8, 2017 Now you have got rid of the "$"s, change the column type to a numeric type (int or decimal) otherwise the values will not sort correctly should the need arise, Quote Link to comment https://forums.phpfreaks.com/topic/304904-having-trouble-with-array-value-saying-null/#findComment-1550986 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.