Jump to content

Populating calendar with dates from array


AdRock

Recommended Posts

I haven't used php for about a year now so i'm a bit rusty  ;)

 

I have a calendar and in the end i want to add links to any dates in the calendar that has a date in the database.

 

The way i thought the most efficient way of doing it was to query the database, get a list of dates in an array, then when displaying the calendar days, if that date is in the array add a link otherwise just display the day.

 

I've been testing with a static array of dates but can't get the days highlighted if they are in the array

 

This is the bit i'm having trouble with

<?php 
$timestamp = mktime(0,0,0,$cMonth,1,$cYear);
$maxday    = date("t",$timestamp);
$thismonth = getdate ($timestamp);
$startday  = $thismonth['wday'];

  for ($i=0; $i<($maxday+$startday); $i++) {
if(($i % 7) == 0 ) echo "<tr>\n";
if($i < $startday) echo "<td></td>\n";
else {
                //need to check if the date displayed in the calendar is in the array
                
	//$today = mktime(0,0,0,date("m"),date("d"),date("Y"));
	$now = date("Y-m-d", $thismonth);
	if(in_array($now,$dateMonthYearArr)) {
		echo "<td align='center' valign='middle' style='height:20px'><a href='#'>". ($i - $startday + 1) . "</a></td>\n";
	}
	else {
		echo "<td align='center' valign='middle' style='height:20px'>". ($i - $startday + 1) . "</td>\n";
	}
}
if(($i % 7) == 6 ) echo "</tr>\n";
  } 

?>

<?php
  $monthNames = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
if (!isset($_REQUEST["month"])) $_REQUEST["month"] = date("n");
if (!isset($_REQUEST["year"]))  $_REQUEST["year"]  = date("Y");

  $cMonth = $_REQUEST["month"];
  $cYear  = $_REQUEST["year"];
                
  $prev_year = $cYear;
  $next_year = $cYear;

	$prev_month = $cMonth-1;
	$next_month = $cMonth+1;

	if ($prev_month == 0 ) {
    $prev_month = 12;
    $prev_year = $cYear - 1;
  }
	if ($next_month == 13 ) {
    $next_month = 1;
    $next_year = $cYear + 1;
  }
  
  //get the dates between 2 dates
  $fromDate = '03/01/2010';
$toDate = '03/31/2010';

$dateMonthYearArr = array();
$fromDateTS = strtotime($fromDate);
$toDateTS = strtotime($toDate);

for ($currentDateTS = $fromDateTS; $currentDateTS <= $toDateTS; $currentDateTS += (60 * 60 * 24)) {
// use date() and $currentDateTS to format the dates in between
$currentDateStr = date("Y-m-d",$currentDateTS);
$dateMonthYearArr[] = $currentDateStr;
//print $currentDateStr."<br />";
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
    <title>fdghhdfhdfh</title>

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <meta name="description" content="" />
    <meta name="keywords" content="" />


<style type="text/css">
button.left {
border:none;
height:10px;
width:28px;
background: url('images/layout/previous.png') no-repeat;

}
button.right {

border:none;
height:10px;
width:28px;
background: url('images/layout/next.png') no-repeat;
}
</style>

<script type="text/javascript">
function loadXMLDoc(url)
{
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.open("GET",url,false);
xmlhttp.send(null);
document.getElementById('calendar_div').innerHTML=xmlhttp.responseText;
}
</script>
</head>

<body>
   <div id="calendar_div">
    <table width="200">
        <tr align="center">
          <td bgcolor="#999999" style="color:#FFFFFF">
            	<table width="100%" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                      <td width="50%" align="left">  
					<div>
						<button class="left" type="button" onclick="loadXMLDoc('<?php echo "php/calendar.php?month=". $prev_month . "&year=" . $prev_year; ?>')"> </button>
					</div>
				  </td>
                      <td width="50%" align="right">
					<div>
						<button class="right" type="button" onclick="loadXMLDoc('<?php echo "php/calendar.php?month=". $next_month . "&year=" . $next_year; ?>')"> </button>
				  </div>  </td>
                    </tr>
              </table>
          </td>
        </tr>
        <tr>
            <td align="center">
              <table width="100%" border="0" cellpadding="2" cellspacing="2">
                <tr align="center">
                  <td colspan="7" bgcolor="#999999" style="color:#FFFFFF"><strong><?php echo $monthNames[$cMonth-1].' '.$cYear; ?></strong></td>
                </tr>
                <tr>
                  <td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>S</strong></td>
                  <td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>M</strong></td>
                  <td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>T</strong></td>
                  <td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>W</strong></td>
                  <td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>T</strong></td>
                  <td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>F</strong></td>
                  <td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>S</strong></td>
                </tr>

                <?php 
                	$timestamp = mktime(0,0,0,$cMonth,1,$cYear);
                	$maxday    = date("t",$timestamp);
                	$thismonth = getdate ($timestamp);
                	$startday  = $thismonth['wday'];

                  for ($i=0; $i<($maxday+$startday); $i++) {
                    if(($i % 7) == 0 ) echo "<tr>\n";
                    if($i < $startday) echo "<td></td>\n";
                    else {
					//$today = mktime(0,0,0,date("m"),date("d"),date("Y"));
					$now = date("Y-m-d", $thismonth);
					if(in_array($now,$dateMonthYearArr)) {
						echo "<td align='center' valign='middle' style='height:20px'><a href='#'>". ($i - $startday + 1) . "</a></td>\n";
					}
					else {
						echo "<td align='center' valign='middle' style='height:20px'>". ($i - $startday + 1) . "</td>\n";
					}
				}
                    if(($i % 7) == 6 ) echo "</tr>\n";
                  } 

                 ?>
              </table>
            </td>
        </tr>
    </table>
</div>


</body>
</html>

 

 

 

Have you checked to see if you have garbage going in? You should put in echo statement in your loop that is creating each of the dates separated by a line break so that you can ensure that the date you are checking for is actually being produced. If it is then echo the spot in the array right before u check the array to ensure that it is stored correctly in the array. Third you must echo the result of the in_array() function to ensure that the correct result is going into the if statement. If all these are straight then let us know. If not then you've found your problem.

If you were developing and debugging this on a system with error_reporting set to E_ALL and display_errors set to ON in your master php.ini, you would already know why it is not working. In the following line -

 

$now = date("Y-m-d", $thismonth);

 

$thismonth is not a Unix Timestamp that date() expects, it is an array that getdate() returns.

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.