Jump to content


Made a Calendar... Now Im stuck...


  • Please log in to reply
5 replies to this topic

#1 _*mYth_*

_*mYth_*
  • Guests

Posted 04 August 2004 - 06:41 PM

Heres my calendar, I done it all myself with no help. Im new at php and MySQL so it was harder then you think.

The problem is i cant seem to find a way to highlight a day if a event is on it. Maybe you can help?

My idea was this but it doesnt work..

if ($d == $eventDay) {
$f = "eventDay";
}

<?php
#############
# Variables #
#############

    $today = date('j'); // Whats todays date ie. 11
    $daysInMonth = date('t'); // How many days in this month ie. 31
    $year = date('Y'); // Todays year ie. 2004
    $thisMonth = date('F'); // Todays Month ie. January
    $monthNum = date('m'); // Todays Month ie. 02
    $todayDay = date('l'); // Todays day ie. Monday

    $host = 'localhost';
    $user = 'myth';
    $pass = '******';
    $db = 'sdhs';
    $table = 'events';

#############
# Day start #
#############

    // This Month
	$dateString = mktime(0,0,0,$monthNum,1,$year); //The date string we need for some info

	$dayStart = date('w',$dateString);  //The number of the 1st day of the week

#############
# Day Array #
#############

   // Format days to shorthand
   $days = array(
    'Sunday' => 'Sun',
    'Monday' => 'Mon',
    'Tuesday' => 'Tue',
 	 'Wednesday' => 'Wed',
 	 'Thursday' => 'Thu',
 	 'Friday' => 'Fri',
 	 'Saturday' => 'Sat',);

##################################
# Days in the Month w/ Leap year #
##################################

    if(date("L")){
    $otherMonths = array(
	'01' => '31',
	'02' => '28',
	'03' => '31',
	'04' => '30',
	'05' => '31',
	'06' => '30',
	'07' => '31',
	'08' => '31',
	'09' => '30',
	'10' => '31',
	'11' => '30',
	'12' => '31',);
    } else {
    $otherMonths = array(
	'01' => '31',
	'02' => '29',
	'03' => '31',
	'04' => '30',
	'05' => '31',
	'06' => '30',
	'07' => '31',
	'08' => '31',
	'09' => '30',
	'10' => '31',
	'11' => '30',
	'12' => '31',);
    }
    $daysInMonth = $otherMonths[$monthNum]; // How many days in this month ie. 31

###############
# Month Array #
###############

   // Format integar months to a name
   $monthFormat = array(
    '01' => 'January',
    '02' => 'Febuary',
 	 '03' => 'March',
 	 '04' => 'April',
 	 '05' => 'May',
    '06' => 'June',
    '07' => 'July',
    '08' => 'August',
 	 '09' => 'September',
 	 '10' => 'October',
 	 '11' => 'November',
 	 '12' => 'December',);

###########################
# Css Variables / Options #
###########################

    $calWidth = '350'; // Use size in Pixels, leave the px out
	$dayWidth = '50'; // Use size in Pixels, leave the px out
	$dayAlign = 'center'; // Use "center", "left", "right", or leave blank
	$dayFill = 'TDayFill'; // Class name to use to fill in the days with dates
	$dateEmpty = 'TDayEmpty'; // Class name to use to fill in the empty days without dates

	$dateWidth = '50'; // Use size in Pixels, leave the px out
	$dateFill = 'TDateFill'; // Class name to use to fill in the date with dates
	$dateAlign = 'center'; // Use "center", "left", "right", or leave blank
	$dateText = 'TDateText'; // Class name to use to stylize in the dates

	$tableHeader = 'TTableHeader';
    $dayText = 'TDayText';
    $monthAlign = 'center';
    $monthText = 'TMonthText';

    $calFill = 'TCalFill';

#########
# Start #
#########

// Open the outside table
print "<table width=\"$calWidth\" class=\"$calFill\"><tr><td>";

################
# Table Header #
################

   echo '
  <table width="'.$calWidth.'" class="'.$tableHeader.'">
   <tr>
     <td colspan="7"><div align="'.$monthAlign.'" class="'.$monthText.'">' . $monthFormat[$monthNum] . '</div></td>
   </tr>
  <tr>
    <td width="'.$dayWidth.'" class="'.$dayFill.'"><div align="'.$dayAlign.'" class="'.$dayText.'">' . $days['Sunday'] . '</div></td>
    <td width="'.$dayWidth.'" class="'.$dayFill.'"><div align="'.$dayAlign.'" class="'.$dayText.'">' . $days['Monday'] . '</div></td>
    <td width="'.$dayWidth.'" class="'.$dayFill.'"><div align="'.$dayAlign.'" class="'.$dayText.'">' . $days['Tuesday'] . '</div></td>
    <td width="'.$dayWidth.'" class="'.$dayFill.'"><div align="'.$dayAlign.'" class="'.$dayText.'">' . $days['Wednesday'] . '</div></td>
    <td width="'.$dayWidth.'" class="'.$dayFill.'"><div align="'.$dayAlign.'" class="'.$dayText.'">' . $days['Thursday'] . '</div></td>
    <td width="'.$dayWidth.'" class="'.$dayFill.'"><div align="'.$dayAlign.'" class="'.$dayText.'">' . $days['Friday'] . '</div></td>
    <td width="'.$dayWidth.'" class="'.$dayFill.'"><div align="'.$dayAlign.'" class="'.$dayText.'">' . $days['Saturday'] . '</div></td>
  </tr>
   ';

############
# Pre-Loop #
############

   echo '<tr>';

###################
# Loop Empty days #
###################

  $dayStart = $dayStart + 1;
  for( $i = 1; $i < $dayStart; $i++ ) {
       echo '<td class="'.$dayEmpty.'" width="'.$dayWidth.'"><div align="'.$dayAlign.'" class="dateText"></div></td>';
	}

###############################
# Connect to MySQL / Query it #
###############################

$link = mysql_connect("$host", "$user", "$pass")
  or die ("Could not connect to MySQL");

mysql_select_db ("$db")
  or die ("Could not select database");

$query = "SELECT * FROM $table WHERE year = $year AND month = $monthNum";
$result = mysql_query ($query)
  or die ("Query failed");

while ($row = mysql_fetch_assoc($result)){
        $eventDay = $row['day'];

#################
# Loop the rest #
#################

  $currentPosition = $dayStart; // Current column position

  for ($i = $currentPosition; $d < $daysInMonth; $i++) {

     $d = $d + 1;

        // Change the text class if its today or not
        if ($d == $today) {
        ($t = "TnowText") && $f = ("TnowFill");
        } else {
        ($t = "TdateText") && $f = ("TdateFill");
        }

     echo "<td width=\"$dateWidth\" class=\"$f\">" . "<div align=\"$dateAlign\" class=\"$t\">" . substr($d+100,1) . "</div>" . "</td>";

      if ($i == 07 || $i == 14 || $i == 21 || $i == 28) {
     	 echo "</tr><tr>\n";
      }
   }
}
#############################
# Fill the rest of the days #
#############################

$a = 35 - $daysInMonth - $dayStart + 1;
for ($e = 0; $e < $a; $e++) {
  echo "<td width=\"$dateWidth\" class=\"emptyDay\">&nbsp;</td>";
}

#######
# End #
#######
// Close the inside table
print "</tr></table>";

// Close the outside table
print "</td></tr></table>";
?>


#2 _*Dabu_*

_*Dabu_*
  • Guests

Posted 05 August 2004 - 11:01 AM

I am not going to write it but in your database or whatever you are using add a collum that has the parameter "event" set it to eithre 0 or 1. Then use an if statement to see if it is 0 or 1 and change the color depending on it. like


if($day == "1") {
echo "With Color";
} else {
echo "Without Color";
}

#3 _*Jay_*

_*Jay_*
  • Guests

Posted 05 August 2004 - 12:27 PM

you could also do a count of the records for that date, if bigger than 0, then highlight with link

#4 _*mYth_*

_*mYth_*
  • Guests

Posted 05 August 2004 - 01:59 PM

Maybe you were misleaded in what was in events... Maybe this will help?

+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int(11)      |      | PRI | NULL    | auto_increment |
| name  | varchar(255) |      |     |         |                |
| text  | longtext     |      |     |         |                |
| month | varchar(255) |      |     |         |                |
| day   | varchar(255) |      |     |         |                |
| year  | varchar(255) |      |     |         |                |
| link  | varchar(255) |      |     |         |                |
+-------+--------------+------+-----+---------+----------------+
7 rows in set (0.01 sec)

Basicly if any $d == $row['day'] then use color else use color. Right?

#5 _*Dabu_*

_*Dabu_*
  • Guests

Posted 06 August 2004 - 08:30 AM

<?
$day = "1";
$year = "2004";
$currentmonth = "Augest";
$leapyear = "0"; //set 1 if yes - 0 if no

$jan = "31";

if ($leapyear == '1') {
$feb = "29";
} else {
$feb = "28";
}

$march = "31";
$apr = "30"
$may = "31";
$june = "30";
$july = "31";
$aug = "31";
$sept = "30";
$oct = "31";
$nov = "30";
$dec = "31";

$month = $aug; //put month's variable here

while ($day !> $month)

$query = mysql_query("SELECT * FROM TABLENAME WHERE day='$day' AND month='$currentmonth' AND year='$year'") or die("Mysql Error: " . mysql_error());
$querynum = mysql_num_rows($query);

if($querynum > 0) {
//echo with link
} else {
//echo without link
}

$day++;
}

?>


Now just change the stuff that needs to be changed and fix any errors because i havent tested it and that should do the trick. If not then im not rewriteing it because I don't have the tiem sorry :-\

#6 _*mYth_*

_*mYth_*
  • Guests

Posted 09 August 2004 - 09:32 PM

Ive tried it, and it still doesnt work.

Im going to try to explain the situation to see if maybe anyone has an idea. Ok i want to make a event calendar. I have a calendar. I want it to query the DB, find the table, show only events that the year and month matches.

Hold that thought. I have a loop, the loop makes the day, if day == the event day make the class for that day something.

Heres the part I need help on.

$link = mysql_connect("$host", "$user", "$pass") or die ("Could not connect to MySQL");
mysql_select_db ("$db") or die ("Could not select database");
$query = "SELECT * FROM $table WHERE year = $year AND month = $monthNum";
$result = mysql_query ($query) or die ("Query failed");

$currentPosition = $dayStart; // Current column position

  for ($i = $currentPosition; $d < $daysInMonth; $i++) {

  $d = $d + 1;

    if ($d == $today) {
    ($t = "nowText") && ($f = "nowFill");
    } else {
    ($t = "dateText") && ($f = "dateFill");
    }

     while ($row = mysql_fetch_assoc ($result))
     {
          if ($d == $row[day])
          {
               $row[day];
          }
     }

    echo "<td width=\"$dateWidth\" class=\"$f\">" . "<div align=\"$dateAlign\" class=\"$t\">" . substr($d+100,1) . "</div>" . "</td>\n";

    if ($i == 7 || $i == 14 || $i == 21 || $i == 28 || $i == 35) {
    echo "</tr><tr>\n";
    } // end of if

  } // end of for

If anyone has some ideas, please i would like to hear them.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users