Jump to content

php calendar


andys2006

Recommended Posts

Hi there,

 

I'm currently building a calendar system for a company that I'm making a website for. I built the core of the calendar a few years ago, but I haven't looked at it in as long, I've forgotten where I was at with the programming of it, and I was wondering if you guys could point me in the right direction. Here is the code that builds the calendar:

 

<?php

    // get this month and this years as an int
    $thismonth = ( int ) date( "m" );
    $thisyear = date( "Y" );

    // find out the number of days in the month
    $numdaysinmonth = cal_days_in_month( CAL_GREGORIAN, $thismonth, $thisyear );

    // create a calendar object
    $jd = cal_to_jd( CAL_GREGORIAN, date( "m" ),date( 1 ), date( "Y" ) );

    // get the start day as an int (0 = Sunday, 1 = Monday, etc)
    $startday = jddayofweek( $jd , 0 );

    // get the month as a name
    $monthname = jdmonthname( $jd, 1 )

?>
<table>
    <tr>
        <td colspan="7"><div align="center"><strong><?= $monthname ?></strong></div></td>
    </tr>
    <tr>
        <td><strong>S</strong></td>
        <td><strong>M</strong></td>
        <td><strong>T</strong></td>
        <td><strong>W</strong></td>
        <td><strong>T</strong></td>
        <td><strong>F</strong></td>
        <td><strong>S</strong></td>
    </tr>
    <tr><?php

    // put render empty cells

    $emptycells = 0;

    for( $counter = 0; $counter <  $startday; $counter ++ ) {

        echo "\t\t<td>-</td>\n";
        $emptycells ++;

    }

    // renders the days

    $rowcounter = $emptycells;
    $numinrow = 7;
$initial = 1;
$getEventArray = mysql_fetch_array($getEventResult);

printf("%s - initial - %s", $dayVal, $initial);


    for( $counter = 1; $counter <= $numdaysinmonth; $counter ++ )
    {


        $rowcounter ++;

		if($counter)
		{
					$dayVal = (int) $getEventArray['startDay'];
					$dayQuery = "SELECT firstName FROM bookings WHERE startDay = '$dayVal'";
					$dayResult = mysql_query($dayQuery) or die ("Problem with query: " . mysql_query());
					$dayArray = mysql_fetch_array($dayResult) or die ("Problem with fetch: " . mysql_query());

        				if($dayVal == $counter)
					{
						$task = $dayArray['firstName'];

					}
					elseif($dayVal != $initial)
					{
						$task = "";
					}

			printf("\t\t<td>%s - %s</td>\n", $counter, $task);
			$initial ++;

			printf("Last DayVal = %s", $dayVal);
		}
		else
		{
			print("this is the else");
			echo "\t\t<td>$counter</td>\n";
		}


        	if( $rowcounter % $numinrow == 0 )
        	{

            echo "\t</tr>\n";

            if( $counter < $numdaysinmonth )
            {

                echo "\t<tr>\n";

            }

            $rowcounter = 0;

        }

    }

    // clean up
    $numcellsleft = $numinrow - $rowcounter;

    if( $numcellsleft != $numinrow ) {

        for( $counter = 0; $counter < $numcellsleft; $counter ++ ) {

            echo "\t\t<td>-</td>\n";
            $emptycells ++;

        }

    }

?>

You can see this working if you go to this url: http://andyseaton.co.uk/contact.php?section=holidaylets

 

basically, in the database, I've got a couple of records with a start day, start month, start year, and end month, end day and end year. What I want to be able to do is colour the cells of the table red if the date matches one of the booking days, and green if not. I'm just having trouble with this section:

 

if($counter)
		{
					$dayVal = (int) $getEventArray['startDay'];
					$dayQuery = "SELECT firstName FROM bookings WHERE startDay = '$dayVal'";
					$dayResult = mysql_query($dayQuery) or die ("Problem with query: " . mysql_query());
					$dayArray = mysql_fetch_array($dayResult) or die ("Problem with fetch: " . mysql_query());

        				if($dayVal == $counter)
					{
						$task = $dayArray['firstName'];

					}
					elseif($dayVal != $initial)
					{
						$task = "";
					}

			printf("\t\t<td>%s - %s</td>\n", $counter, $task);
			$initial ++;

			printf("Last DayVal = %s", $dayVal);
		}
		else
		{
			print("this is the else");
			echo "\t\t<td>$counter</td>\n";
		}

 

as what I can't get my head around is how to get the calendar, when it's building the table td cells, to check against an array which holds the starting days. Then if it encounters a day which matches the start day, it colours that cell red.

 

Anyway, I hope I am making sense, and if anyone can help, I would be very grateful.

 

Cheers

 

Andy

 

Mod edit: code tags are always appreciated

Link to comment
https://forums.phpfreaks.com/topic/135039-php-calendar/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.