Jump to content

looping through one-dimensional array


Go to solution Solved by Psycho,

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/288565-looping-through-one-dimensional-array/
Share on other sites

 

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

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, ,
  • Solution

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
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.