Ice Data

<?php
$lakes = array(
"ME"=>"Lake Mendota",
"MO"=>"Lake Monona",
"WI"=>"Lake Wingra",
"FI"=>"Fish Lake",
"TR"=>"Trout Lake",
"TB"=>"Trout Bog",
"SP"=>"Sparkling Lake",
"CR"=>"Crystal Lake",
"CB"=>"Crystal Bog",
"BM"=>"Big Muskellunge Lake",
"AL"=>"Allequash Lake");

$lakeid = $_GET['lakeid'];

if(!$lakeid)$lakeid = "TR";

$lakename = $lakes[$lakeid];
$lakepage = strtolower(str_replace(" ", "-", $lakename));

$sqlstatement = 'SELECT icedata.lakename, icedata.start_year as icestart, icedata.end_year as iceend,
icedata.ice_on as ice_on, icedata.ice_off as ice_off, icedata.ice_duration as duration
FROM icedata
WHERE icedata.lakename = "'.$lakename.'"
ORDER BY icedata.start_year';

db_set_active('dbmaker');

$result = db_query($sqlstatement);

If (!$result) {
echo "No results for NTL lakes <br/>";
}
else {
echo '<a href="../researchsite/'.$lakepage.'"><img alt="Lake Icon" src="/sites/default/files/ntl/images/lakes/'.$lakeid.'Icon.jpg" style="width: 79px; height: 79px; margin: 10px; float: left;"/></a> <h2>Ice Data for '.$lakename.'</h2>';
echo '<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p>';

$chartdata[0] = array('Year', 'Days');
while($row = db_fetch_array($result)) { //db_fetch_object will not work in drupal 7
$returns[] = array($row[icestart], $row[iceend], $row[ice_on], $row[ice_off], $row[duration]);
if ($row[duration] > -1){
$chartdata[] = array((int)$row[icestart], (int)$row[duration]);
}
}
}
db_set_active('default'); //close the other database connection again

//calculate averages

$oldest = (int)$returns[0][0];
$today = (int)date('Y');
$numyears = $today - $oldest;
$numrecords = count($returns);

$sum3 = 0;
$sum10 = 0;
$sum50 = 0;
$divisor = 0;
if ($numyears >3){
for ($i = $numrecords; $i >= ($numrecords - 3); $i--){
if ($returns[$i][4] > -1){
$sum3 = $sum3 + (int)$returns[$i][4];
$divisor = $divisor + 1;
}
}
$average3 = $sum3 / $divisor;
}
$divisor = 0;
if ($numyears >10){
for ($i = $numrecords; $i >= ($numrecords - 10); $i--){
if ($returns[$i][4] > -1){
$sum10 = $sum10 + (int)$returns[$i][4];
$divisor = $divisor + 1;
}
}
$average10 = $sum10 / $divisor;
}
$divisor = 0;
if ($numyears >50){
for ($i = $numrecords; $i >= ($numrecords - 50); $i--){
if ($returns[$i][4] > -1){
$sum50 = $sum50 + (int)$returns[$i][4];
$divisor = $divisor + 1;
}
}
$average50 = $sum50 / $divisor;
}
?>

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var chartdata = <?php echo json_encode($chartdata);?>;
var data = google.visualization.arrayToDataTable(chartdata);

var options = {
'title': 'Ice Duration in Days',
'hAxis': {format: '0'},
};

var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>

<div id="chart_div" style="position:relative; left:-100; width: 800px; height: 400px"></div>

<?php
echo '<h3>Average Ice Duration</h3>';
echo '<table class="views-table cols-2"><tbody>';
echo '<tr class="odd views-row-first"><td>Last 3 years</td><td>'.$average3.'</td></tr>';
if ($average10 > 0){
echo '<tr class="even"><td>Last 10 years</td><td>'.$average10.'</td></tr>';
}
if ($average50 > 0){
echo '<tr class="odd"><td>Last 50 years</td><td>'.$average50.'</td></tr>';
}
echo '</tbody></table>';
echo '<p>&nbsp;</p>';
echo '<h3>Ice on and Ice off Dates</h3>';
$countrows = 1;
echo '<table class="views-table cols-4"><tr><th>Season</th><th>Ice-On Date</th><th>Ice-Off Date</th><th>Days</th></tr><tbody>';
foreach ($returns as $return) {
if (is_int($countrows / 2)) {
echo '<tr class="even"><td>'.$return[0].' - '.$return[1].'</td><td>'.$return[2].'</a></td><td>'.$return[3].'</td><td>'.$return[4].'</td></tr>';
$countrows = $countrows +1;
}else{
echo '<tr class="odd"><td>'.$return[0].' - '.$return[1].'</td><td>'.$return[2].'</a></td><td>'.$return[3].'</td><td>'.$return[4].'</td></tr>';
$countrows = $countrows +1;
}
}
echo "</tbody></table>";
?>