Ok....so I have a nice looking calendar at http://www.bh-wbpastoralcharge.net/xindex.php

I want to title and link each date that has an event attached to it which are stored in a database.

I have called the function createCal() in the middle of my HTML document and it reports everything okay, except for calling the events from the database, I can't seem to get anything out of it. Do I have to do all the database queries, before the page loads (i.e. in the header)? My PHP code is below but you can all skip right to the big foreach loop at the end because that is where the action happens.

If anyone thinks this is more of an SQL question, let me know so I can post it there.

Code:
<?php

error_reporting('0'); //TURNS ERROR REPORTING OFF
ini_set('display_errors', '0'); //TURNS ERROR DISPLAY OFF


 //DECLARE MySQL ALL VARIABLES
 $mysql_dir = "localhost";
 $user      = "ajenkins";
 $pass      = "XXXXXX";
 $database  = "calendar";
 $table     = "entries";

 //CONNECT TO MySQL
 $link = @mysql_connect($mysql_dir, $user, $pass);

 if ($link == "FALSE") {
  echo "Calendar currently not available";
  exit;
 }

 //SELECT THE calendar DATABASE
 mysql_select_db($database);

function createCal() {

 //GET TODAY
 $date = mktime(0,0,0,date('n'), date('j'), date('Y'));

 //SET THE DAY MONTH AND YEAR VARIABLES TO BE USED IN THE PROGRAM
 $day = date('j', $date); //DAY WITHOUT LEADING ZEROES
 $month = date('n', $date); //MONTH WITHOUT LEADING ZEROES
 $year = date('Y', $date); //YEAR

 //CREATE A WEEKDAY ARRAY
 $weekday[0] = "Sun";
 $weekday[1] = "Mon";
 $weekday[2] = "Tue";
 $weekday[3] = "Wed";
 $weekday[4] = "Thu";
 $weekday[5] = "Fri";
 $weekday[6] = "Sat";

 //GET THE FIRST DAY OF THE MONTH DEFINED ABOVE
 $month_start = mktime(0,0,0,$month, 1, $year);

 //GET THE MONTH NAME
 $month_name = date('F', $month_start); //GET THE FULL MONTH NAME 

 //GET THE DAY OF THE WEEK FOR THE FIRST OF THE MONTH - THREE LETTERS
 $month_start_day = date('D', $month_start);

 //CREATE AN OFFSET TO CALCULATE WHICH COLUMN (Sun-Sat) THE FIRST DAY GOES IN
 switch($month_start_day) {
  case "Sun"; $offset = 0; break;
  case "Mon"; $offset = 1; break;
  case "Tue"; $offset = 3; break;
  case "Wed"; $offset = 4; break;
  case "Thu"; $offset = 5; break;
  case "Fri"; $offset = 6; break;
  case "Sat"; $offset = 7; break;
 }

 // determine how many days are in the last month.
 if($month == 1){
    $num_days_last = cal_days_in_month(0, 12, ($year-1));
 } 
 else {
    $num_days_last = cal_days_in_month(0, ($month-1), $year);
 }
 // determine how many days are in the current month.
 $num_days_current = cal_days_in_month(0, $month, $year);

 // Build an array for the current days
 // in the month
 for($i = 1; $i <= $num_days_current; $i++){
    $num_days_array[] = $i;
 }

 // Build an array for the number of days
 // in last month
 for($i = 1; $i <= $num_days_last; $i++){
    $num_days_last_array[] = $i;
 }

 if($offset > 0){
    $offset_correction = array_slice($num_days_last_array, -$offset+1, $offset);
    $new_count = array_merge($offset_correction, $num_days_array);
    $offset_count = count($offset_correction);
 }
 else {
    $offset_count = 0;
    $new_count = $num_days_array;
 }

 $current_num = count($new_count);

 if($current_num > 35){
   $num_weeks = 6;
   $outset = (42 - $current_num);
 } elseif($current_num < 35){
   $num_weeks = 5;
   $outset = (35 - $current_num);
 }

 if($current_num == 35){
   $num_weeks = 5;
   $outset = 0;
 }

 // Outset Correction
 for($i = 1; $i <= $outset; $i++){
   $new_count[] = $i;
 }

 // Now let's "chunk" the $all_days array
 // into weeks. Each week has 7 days
 // so we will array_chunk it into 7 days.
 $weeks = array_chunk($new_count, 7);

 //PRINT THE HEADER
 echo "<caption>$month_name $year</caption>";
 echo "<tr>\n";

 foreach ($weekday AS $wd) {
  echo "<th>$wd</th>\n";
 }

 echo "</tr>\n";

 $i = 0; 
 foreach($weeks AS $week){
        echo "<tr>\n";
        foreach($week as $d){
         if($i < $offset_count){
              $num_entries = numEntryQuery($date,($month-1),$year);
              $title = entryInfoQuery($date,($month-1),$year,$num_entries);
              echo "<td class=\"nonMonth\" title=\"$title\">$d</td>\n";
          }
          if(($i >= $offset_count) && ($i < ($num_weeks * 7) - $outset)){

              $num_entries = numEntryQuery($date,($month),$year);
              $title = entryInfoQuery($date,($month),$year,$num_entries);


            if($date == mktime(0,0,0,$month,$d,$year)){
                echo "<td class=\"today\" title=\"$title\">$d</td>\n";
            } else {
                echo "<td class=\"days\" title=\"$title\">$d</td>\n";
            }
         } elseif(($outset > 0)) {
              $num_entries = numEntryQuery($date,($month+1),$year);
              $title = entryInfoQuery($date,($month+1),$year,$num_entries);

             if(($i >= ($num_weeks * 7) - $outset)){
                echo "<td class=\"nonMonth\" title=\"$title\">$d</td>\n";
            }
         }
         $i++;
       }
       echo "</tr>\n";   
 } 


}

function numEntryQuery($date,$month,$year) {

 $num_query = mysql_query("SELECT COUNT(entryID) FROM $table WHERE date = $date AND month = $month AND year = $year");
 return $num_entries = mysql_result($num_query, 0, 0);

}

function entryInfoQuery($date,$month,$year,$num) {

 if ($num > 1) {
  //NOT SURE YET
 }
 else {
  return $query_result = mysql_query("SELECT title FROM $table WHERE date = $date AND month = $month AND year = $year");  
 }


}

?>