Page 1 of 1

Annual Data Summary - sunshine hours

Posted: Mon 16 May 2011 11:24 pm
by Graham64
I've been using David's excellent Annual data summary pages as a means to improve my almost non-existent PHP and Javascript skills. I wanted to add Sunshine hrs to the pages.

But there is a problem because my version of dayfile.txt before 01/01/2011 does not have the sunshine hours field.

My modified Javascript version deals with the fact that the early dayfile.txt is shorter than the 2011 file and just leaves blank fields for sunshine in 2010 (see http://poundstockpacket.org.uk/datasummary_gj.html)

However my modified PHP version produces an error of Notice: Undefined offset: 24 when it tries to display 2010 sunshine hrs. I assume that this is because my dayfile.txt in 2010 didn't have field 24, sunshine hours (see http://poundstockpacket.org.uk/datasummary_gj_01.php)

I have got around the problem by hardcoding a trap for the error (see http://poundstockpacket.org.uk/datasummary_gj_02.php) This looks for 2010 and sunshine hours

Code: Select all

// need to trap old 'layouts' that did not contain sunshine data
	if( $tableYear == '2010' && $whatdata == 'sunhours')
			{$data[$id] = '';}
	else
			{ $data[$id] = $buf_arr[$dayfilecol]; } 
I have two questions:
  • Why does the Javascript version not produce an error?
    Is there a better way of dealing with the problem in PHP without hardcoding?
Thanks
Graham

Re: Annual Data Summary - sunshine hours

Posted: Tue 17 May 2011 7:58 am
by mcrossley
Without looking at the full code, just working with your snippet...

Code: Select all

if ($dayfilecol >= count($buf_arr)) {
  $data[$id] = '';
} else {
  $data[$id] = $buf_arr[$dayfilecol];
}

Re: Annual Data Summary - sunshine hours

Posted: Tue 17 May 2011 8:05 am
by mcrossley
Oh, and your Javascript does produce an error "Uncaught TypeError: Cannot call method 'split' of undefined" at line 86 of jquery.js, but your browser may be set to ignore script errors - most are by default 'cause there are so many of them out there!

Re: Annual Data Summary - sunshine hours

Posted: Tue 17 May 2011 8:14 am
by steve
mcrossley wrote:if ($dayfilecol >= count($buf_arr)
This is what I do in Cumulus; just check that there are sufficient fields in the entry.

Re: Annual Data Summary - sunshine hours

Posted: Fri 20 May 2011 12:36 am
by Graham64
Mark/Steve
Thanks very much for the suggestion for the PHP fix - I have added the more general check for $dayfilecol as you both suggested. There is now no error.

Mark
Thanks for the pointing out the Javascript error, my browser didn't show it. I used Firebug to look at the problem - I think it was the same error as with the PHP version.
I have solved the problem by trapping the error using

Code: Select all

if(wd_data.length>=dayfilecol)
See http://poundstockpacket.org.uk/datasummary_gj.html

Thanks again to you both - this Forum is so useful. I'm learning a great deal, all I need now is more free time to program!

Graham