PHP Source Code


<html>
<head>
<title>Rutgers Sports Calendar</title>
<style type="text/css">
a:link {color: #000000; text-decoration: underline; }
a:active {color: #000000; text-decoration: underline; }
a:visited {color: #000000; text-decoration: underline; }
a:hover {color: #000000; text-decoration: underline; }

table a:link, table a:visited, table a:active, table a:hover {
color: #FFFFFF;
       text-decoration: none;
}
h2 {
    font-family: Verdana;
    font-size: 15px;
}
h3 {
    font-family: Verdana;
    font-size: 12px;
    font-weight: normal;
}
body {
    font-family: Verdana;
    font-size: 12px;
    table-layout: fixed;
}
table {
    font-family: Verdana;
    font-size: 10px;
}
</style>
</head>
<body onLoad="document.getElementById('loader').style.display='none';">
<div id='loader'>
Fetching events from external websites, please wait...
</div>

<?php
ob_flush();
flush();

echo "<center>";
echo "<img src=\"rutgers.jpg\" height=100>";
echo "<p><h3><b>Live Rutgers Sports Calendar</b><br />";
echo "30 days worth of events automatically extracted from www.scarletknights.com.<br />";
echo "Hover mouse over event image for information<p>";
echo "By <a href=\"http://winlab.rutgers.edu/~snelson/\">Sam Nelson</a> | <a href=\"source.html\">View script source code</a></h3><p>";

/*******************************************************************
  Extact content from HTML
  Each of these data points is stored in an array that is returned:
    finalData[0] = a dataPoint;
    finalData[1] = another dataPoint;
    etc.
*******************************************************************/
function getContents($in_url) {

    // store all data in the following array
    $finalData = array();

    // get HTML data
    $sportsData = file_get_contents($in_url);

    // keep getting data until the end of the page
    while (true) {

        // get next <tr> ... </tr> block
        $start = strpos($sportsData, "<tr>");
        if ($start === false) {
            break;
        }
        $end = strpos($sportsData, "</tr>");
        $trBlock = substr($sportsData, $start, $end);

        // extact data from block
        $dataPoint = getDataPoint($trBlock);

        // add data to main array if valid
        if ($dataPoint['valid'] == true) {
            array_push($finalData, $dataPoint);
        }

        // get rid of the block
        $sportsData = substr($sportsData, $end+4, strlen($sportsData));
    }

    // return the data array
    return $finalData;

}

/******************************************
  Get data out of a <tr>...</tr> block
******************************************/
function getDataPoint($trBlock) {
    $dataPoint = array();
    $trArray = explode("\n",strip_tags($trBlock));
    if (count($trArray) < 8 || trim($trArray[6]) == "DATE") {
        $dataPoint['valid'] = false;
        return $dataPoint;
    } else {
        $dataPoint['valid'] = true;
    }

    $offset = 0;
    if (preg_match('/divider/',$trBlock)) {
        $offset = 1;
    }

    $dataPoint['date'] = trim($trArray[3+$offset]);
    $dataPoint['sport'] = trim($trArray[4+$offset]);
    $dataPoint['opp'] = trim($trArray[5+$offset]);
    $dataPoint['location'] = trim($trArray[6+$offset]);
    $dataPoint['time'] = trim($trArray[7+$offset]);
    return $dataPoint;
}

/**********************************************************
  Write all sports on tempDay through the echo command
**********************************************************/
function echoSportsToday($sportsData,  $tempDay) {

    for ($i = 0; $i < count($sportsData); $i++) {
        $dataPoint = $sportsData[$i];
        if ($dataPoint['date'] == $tempDay) {
	    $mouseOver = $dataPoint['sport'] . " ";
            $mouseOver = $mouseOver . "vs. " . $dataPoint['opp'] . " ";
            $mouseOver = $mouseOver . "at " . $dataPoint['location'] . " ";
            $mouseOver = $mouseOver . "at " . $dataPoint['time'];
            $sport = $dataPoint['sport'];            
            $img = "default.jpg";
            if ($sport == "Baseball") {
                $img = "baseball.jpg";
            } elseif ($sport == "Men's Basketball") {
                $img = "mbball.jpg";
            } elseif ($sport == "Women's Basketball") {
                $img = "wbball.jpg";
            } elseif ($sport == "Football") {
                $img = "football.jpg";
            } elseif ($sport == "Women's Tennis") {
                $img = "wtennis.jpg";
            } elseif ($sport == "Men's Tennis") {
                $img = "mtennis.jpg";
            } elseif ($sport == "Softball") {
                $img = "softball.jpg";
            } elseif ($sport == "Swimming and Diving") {
                $img = "swimming.jpg";
            } elseif ($sport == "Women's Golf") {
                $img = "golf.jpg";
            } elseif ($sport == "Men's Golf") {
                $img = "golf.jpg";
            } elseif ($sport == "Men's Lacrosse") {
                $img = "lacrosse.jpg";
            } elseif ($sport == "Women's Lacrosse") {
                $img = "lacrosse.jpg";
            } elseif ($sport == "Men's Track") {
                $img = "track.jpg";
            } elseif ($sport == "Women's Track") {
                $img = "track.jpg";
            } elseif ($sport == "Gymnastics") {
                $img = "gym.jpg";
            } elseif ($sport == "Wrestling") {
                $img = "wrestling.jpg";
            }
            echo "<img src=\"" . $img . "\" title=\"" . $mouseOver . "\" width=20 height=20>";
        }
    }

}

$sportsData = getContents('http://www.scarletknights.com/schedule/sched-30.asp');

// Days of the week
$dayNames = array(0 => "S", 1 => "M", 2 => "T", 3 =>"W", 4 => "T", 5 => "F", 6 => "S");

// Determine the first and last day of the current month
if (array_key_exists('month',$_GET) && array_key_exists('year',$_GET)) {
    $month = $_GET['month'];
    $year = $_GET['year'];
    $today = getdate(mktime(0,0,0,$month,1,$year));
    $realMonth = false;
} else {
    $today = getdate();
    $realMonth = true;
}
$first_day = getdate(mktime(0,0,0,$today['mon'],1,$today['year']));
if ($today['mon'] != 11)
    $last_day = getdate(mktime(0,0,0,$today['mon']+1,0,$today['year']));
else
    $last_day = getdate(mktime(0,0,0,0,0,$today['year']+1));

// Print out the calendar
echo "<table border = 1 cellpadding = 5>\n";
echo "<tr>\n";
echo "<td bgcolor=#D21034 colspan=\"7\" align=\"center\"><b><font color=#FFFFFF>" . $today['month'] . " " . $today['year'] . "</font></b></td>\n";
echo "</tr>\n";
echo "<tr height=20>\n";
foreach ($dayNames as $dn) {
    echo "<td width=80><b>" . $dn . "</b></td>\n";
}
echo "</tr>\n";
$dayWereOn = 0;
$breakout = false;
for ($week = 0; $week < 6; $week++) {
    echo "<tr height=80>\n";
    for ($dayofweek = 0; $dayofweek < 7; $dayofweek++) {
        if ($dayWereOn == 0 && $dayofweek == $first_day['wday'])
            $dayWereOn = 1;
        if ($dayWereOn == $today['mday'] && $realMonth)
            echo "<td width=80 valign=top bgcolor=#D21034>";
        elseif ($dayWereOn <= 0)
            echo "<td width=80 valign=top bgcolor=#D8D8D8>";
        else
            echo "<td width=80 valign=top>";
        if ($dayWereOn > 0) {
            echo $dayWereOn;
            echo "<center>";
            $tempDay = date("F j",mktime(0,0,0,$today['mon'],$dayWereOn,$today['year']));
            echoSportsToday($sportsData, $tempDay);
            $dayWereOn++;
        } else {
            echo " ";
        }
        echo "</td>\n";
        if ($dayWereOn == $last_day['mday'] + 1) {
            $dayWereOn = -1;
            $breakout = true;
        }
    }
    echo "</tr>\n";
    if ($breakout == true)
        break;
}

// Create bottom of calendar
$prevMonth = $today['mon']-1;
$prevYear = $today['year'];
if ($prevMonth == 0) {
    $prevMonth = 12;
    $prevYear--;
}
$nextMonth = $today['mon']+1;
$nextYear = $today['year'];
if ($nextMonth == 13) {
    $nextMonth = 1;
    $nextYear++;
}
echo "<tr>\n";
echo "<td colspan=2 bgcolor=#D21034>";
echo "<center><b><a href=\"main.php?month=" . $prevMonth . "&year=" . $prevYear . "\">Go To Previous Month</a></b></center>";
echo "</td>\n<td colspan=3 bgcolor=#D21034>";
echo "<center><b><a href=\"main.php\">Go To Today: " . date("m/d/y") . "</a></b></center>";
echo "</td>\n<td colspan=2 bgcolor=#D21034>";
echo "<center><b><a href=\"main.php?month=" . $nextMonth . "&year=" . $nextYear . "\">Go To Next Month</a></b></center>";
echo "</td>";
echo "</tr>\n";

echo "</table><p>\n";
?>

</center>
</body>
</html>