Raissam Posted March 10, 2014 Share Posted March 10, 2014 Hi guys, Im trying to use the library for graphics Rgraph to show some data from mysql My exit from mysql is year | amount | type 2000 | 3 | open 2001 | 1 | open 2010 | 1 | close 2010 | 2 | open 2011 | 1 | open Rgraph need the data like this to show right [[3],[1],[1],[1,2],[1]]. At the end the graph will stay that way: | | ~1~ | _3_ | _1_ | _2_ | _1_ ------------------------------------------ 2000 | 2001 | 2010 | 2011 Can u please help to format this on php.. tks Quote Link to comment https://forums.phpfreaks.com/topic/286860-select-for-stacked-bars-graph/ Share on other sites More sharing options...
Psycho Posted March 10, 2014 Share Posted March 10, 2014 Try $query = "SELECT year, amount FROM table_name ORDER BY year, amount"; $result = mysql_query($query); $data = array(); while($row = mysql_fetch_assoc($result)) { $data[$row['year']][] = $row['amount']; } //Post process the data foreach($data as &$year) { $year = '[' . implode(',', $year) . ']'; } $finalData = '[' . implode(',', $data) . ']'; Quote Link to comment https://forums.phpfreaks.com/topic/286860-select-for-stacked-bars-graph/#findComment-1472064 Share on other sites More sharing options...
Barand Posted March 10, 2014 Share Posted March 10, 2014 I'd order by year, type to get close value before the open value Quote Link to comment https://forums.phpfreaks.com/topic/286860-select-for-stacked-bars-graph/#findComment-1472070 Share on other sites More sharing options...
Solution Raissam Posted March 11, 2014 Author Solution Share Posted March 11, 2014 (edited) Thanks Psycho, works perfectly! Edited March 11, 2014 by Raissam Quote Link to comment https://forums.phpfreaks.com/topic/286860-select-for-stacked-bars-graph/#findComment-1472208 Share on other sites More sharing options...
Raissam Posted March 13, 2014 Author Share Posted March 13, 2014 Good afternoon,I put the solution presented there and ran up to the test, but there was another problem, that is when there is no data, so I talked with the developer Rgraph and he suggested that I enter a 0 which is blank.At the end should have the following string [[0,1,0,1],[0,1,0,3],[0,1,0,4],[0,1,1,5],[1,2,0,1],[1,0,1,0]].I tried changing the suggested but did not succeed code follows below:My exit from mysql isyear | amount | type2000 | 1 | orange2000 | 1 | banana2001 | 1 | orange2001 | 3 | banana2003 | 1 | orange2003 | 4 | banana2005 | 1 | orange2005 | 1 | pear2005 | 5 | banana2010 | 1 | apple2010 | 2 | orange2010 | 1 | banana2011 | 1 | apple2011 | 1 | pear[apple,orange,pear,banana]Rgraph need the data like this to show right [[0,1,0,1],[0,1,0,3],[0,1,0,4],[0,1,1,5],[1,2,0,1],[1,0,1,0]]. These were changes I made to work but it did not work while($row = mysql_fetch_assoc($result)) { $data[$row['year2']][] = $row['amount']; $data[][$row['amount']] = $row['amount']; } foreach($data as &$year) { $year2 = $row['year2']; foreach($year as $key=>$year_2) { $amount = $row['amount']; if(!empty($year2)) { $year = implode(',', $year); } else{ $year = '[' . 0 . ']' ; } } } $finalData = '[' . implode(',', $data) . ']'; I know it's annoying to keep asking but I am no expert on the subject and I'm trying to do something that my boss wants and I have no skill for it. TKSS!! Quote Link to comment https://forums.phpfreaks.com/topic/286860-select-for-stacked-bars-graph/#findComment-1472484 Share on other sites More sharing options...
Barand Posted March 13, 2014 Share Posted March 13, 2014 try something like this $datakeys = array('apple','orange','pear','banana'); $sql = "SELECT year, type, amount FROM mytable ORDER BY year"; $res = $mysqli->query($sql); $curryear = 0; $data = array(); while (list($year, $type, $amount) = $res->fetch_row()) { if ($year != $curryear) { $data[$year] = array_fill_keys($datakeys, 0); $curryear = $year; } $data[$year][$type] = $amount; } Quote Link to comment https://forums.phpfreaks.com/topic/286860-select-for-stacked-bars-graph/#findComment-1472496 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.