stevieontario Posted May 17, 2014 Share Posted May 17, 2014 hello freaks, I have a four-element array, which is the result of a mysql query. The array is like below. (This is of course just a snippet to show the structure; the hours of course go up to 24, then repeat; there are about ten days' worth of data.) Array([dayname] => day1[hour] => 1[widtype] => type1[Output] => 20)Array([dayname] => day1[hour] => 2[widtype] => type1[Output] => 9)Array([dayname] => day1[hour] => 1[widtype] => type2[Output] => 450)Array([dayname] => day1[hour] => 2[widtype] => type2[Output] => 650) I want to loop through this data and output each hour's data in a separate line, like below (I included the headings just for clarity): Day Hour Type1_total Type2_total day1 1 20 450 day1 2 9 650 ... and can't seem to make this happen. Here's the code I've written: $prevday = ''; while ( $row = mysql_fetch_assoc($result)) { extract($row); $currentday = $row['dayname']; $currenthour = $row['hour']; $currentwid = $row['widtype']; while($currentday !== $prevday){ for($currenthour = 1; $currenthour <=24; $currenthour++){ if($row['widtype'] == 'type1'){ $type1_total = $row['Output'];} if($row['fuel'] == 'type2'){$type2_total = $row['Output'];} print "<pre>"; echo "$currentday, $currenthour, $type1_total, $type2_total"; print "</pre>"; } $prevday = $currentday; } } ... and here's what it outputs: day1, 1, 20, ,day1, 2, 20, , Clearly I have written this loop wrong but have banged my head against it for a while and wonder if it's just not possible to do what I want. Any suggestions would be hugely appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/288565-looping-through-one-dimensional-array/ Share on other sites More sharing options...
Ch0cu3r Posted May 17, 2014 Share Posted May 17, 2014 I want to loop through this data and output each hour's data in a separate line, like below (I included the headings just for clarity): Day Hour Type1_total Type2_total day1 1 20 450 day1 2 9 650 Not very clear from where I'm sitting. Can you post a better example Quote Link to comment https://forums.phpfreaks.com/topic/288565-looping-through-one-dimensional-array/#findComment-1479871 Share on other sites More sharing options...
stevieontario Posted May 17, 2014 Author Share Posted May 17, 2014 sorry, that was a cut-and-paste from a spreadsheet which didn't work out. Here's the raw output: day1, 1, 20, 450 day2, 2, 9, 650 Quote Link to comment https://forums.phpfreaks.com/topic/288565-looping-through-one-dimensional-array/#findComment-1479877 Share on other sites More sharing options...
stevieontario Posted May 17, 2014 Author Share Posted May 17, 2014 whoops, my most recent reply was not the raw output, but what I WANT to be the raw output. So, what I want is this: day1, 1, 20, 450 day2, 2, 9, 650 ... and what I'm actually getting is this: day1, 1, 20, ,day1, 2, 20, , Quote Link to comment https://forums.phpfreaks.com/topic/288565-looping-through-one-dimensional-array/#findComment-1479879 Share on other sites More sharing options...
Barand Posted May 18, 2014 Share Posted May 18, 2014 What does your original db table look like and what is your query? Quote Link to comment https://forums.phpfreaks.com/topic/288565-looping-through-one-dimensional-array/#findComment-1479908 Share on other sites More sharing options...
Solution Psycho Posted May 18, 2014 Solution Share Posted May 18, 2014 You didn't show the query you used so this may not work out-of-the-box. But, you should be able to change your query to do the calculations for you. Give this a try SELECT dayname, hour, SUM(IF(widtype='type1', Output, 0)) AS type1_total, SUM(IF(widtype='type2', Output, 0)) AS type2_total FROM table GROUP BY dayname, hour Quote Link to comment https://forums.phpfreaks.com/topic/288565-looping-through-one-dimensional-array/#findComment-1479910 Share on other sites More sharing options...
stevieontario Posted May 18, 2014 Author Share Posted May 18, 2014 Psycho -- thanks, that worked perfectly. For some reason none of my attempts to solve this on the query side involved the if statement. thanks very much! Quote Link to comment https://forums.phpfreaks.com/topic/288565-looping-through-one-dimensional-array/#findComment-1479915 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.