Hi everyboby.

plz help me.

I created calendar using php and javascript.but the javasript function did not work.plz tell me what is the error.

<?php use_javascript('schedule') ?>
<?php use_javascript("editProfile") ?>
<?php use_javascript('unfinishedbussiness')?>
<?php use_javascript('scheduleload')?>

<script type="text/javascript" language="javascript">
function isAjax() {
 return isset($_SERVER['HTTP_X_REQUESTED_WITH']) &&
     $_SERVER ['HTTP_X_REQUESTED_WITH']  == 'XMLHttpRequest';
}

if(isAjax() && isset($_POST['month']))
{
	$month = $_POST['month'];
	$year = !isset($_POST['year']) ? date('Y', $current_time) : $_POST['year'];
	die(getCalendar($month,$year));
}
function getPrevMonth()
	{
		if(current_month == 1)
		{
			current_month = 12;
			current_year = current_year - 1;
		}
		else
		{
			current_month = current_month - 1;
		}
		params = 'month='+current_month+'&year='+current_year;
		new Ajax.Updater('calendar-node',window.location.pathname,{method:'post',parameters: params});
	}
		function getNextMonth()
		{
			if(current_month == 12)
			{
				current_month = 1;
				current_year = current_year + 1;
			}
			else
			{
				current_month = current_month + 1;
			}
			params = 'month='+current_month+'&year='+current_year;
			new Ajax.Updater('calendar-node',window.location.pathname,{method:'post',parameters: params});
		}
		</script>


<div id="calendar-node" >
<div style=" width: 320px;">
    
        <h1 class="title" style="float:left;">MyPlan</h1>&nbsp&nbsp;
        
    </div>
     <div id="unfinishedbussiness" >
    <div style="float:left"></div>
       
    <a href="<?php echo url_for("@make_note")?>">UnfinishedBussiness</a>
    </div>
          <div class="clear-both"></div>
          
       
         
   <?php   
   
   
   
   
     
      $month = date('m', time());
$year = date('Y', time());
$calendar = getCalendar($month,$year);

function getCalendar($month,$year)
{
	// Use the PHP time() function to find out the timestamp for the current time
	$current_time = time();
	
	// Get the first day of the month
	$month_start = mktime(0,0,0,$month, 1, $year); 
	
	// Get the name of the month
	$month_name = date('F', $month_start); 
	
	// Figure out which day of the week the month starts on.
	$first_day = date('D', $month_start);
	
	// Assign an offset to decide which number of day of the week the month starts on.
	switch($first_day)
	{
	case "Sun":
		$offset = 0;
		break;
	case "Mon":
		$offset = 1;
		break;
	case "Tue":
		$offset = 2;
		break;
	case "Wed":
		$offset = 3;
		break;
	case "Thu":
		$offset = 4;
		break;
	case "Fri":
		$offset = 5;
		break;
	case "Sat":
		$offset = 6;
		break;
	} 
	
	// determine how many days were in last month.
	//	Note: The cal_days_in_month() function returns the number of days in a month for the specified year and calendar.
	
	if($month == 1)
		$num_days_last = cal_days_in_month(CAL_GREGORIAN, 12, ($year -1));
	else
		$num_days_last = cal_days_in_month(CAL_GREGORIAN, ($month - 1), $year);
	
	// determine how many days are in the this month.
	$num_days_current = cal_days_in_month(CAL_GREGORIAN, $month, $year); 
	
	// Count through the days of the current month -- building an array
	for($i = 0; $i < $num_days_current; $i++)
	{
		$num_days_array[] = $i+1;
	} 
	        

	
	// Count through the days of last month -- building an array
	for($i = 0; $i < $num_days_last; $i++)
	{
		$num_days_last_array[] = '';
	}
	
	if($offset > 0){ 
		$offset_correction = array_slice($num_days_last_array, -$offset, $offset);
		$new_count = array_merge($offset_correction, $num_days_array);
		$offset_count = count($offset_correction);
	}
	else
	{ 
		$new_count = $num_days_array;
	}
	
	// How many days do we now have?
	$current_num = count($new_count); 
	
	// Our display is to be 35 cells so if we have less than that we need to dip into next month
	if($current_num > 35)
	{
		$num_weeks = 6;
		$outset = (42 - $current_num);
	}
	else if($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[] = '';
	}
	
	// Now let's "chunk" the $new_count array
	// into weeks. Each week has 7 days
	// so we will array_chunk it into 7 days.
	$weeks = array_chunk($new_count, 7);
	
	// Start the output buffer so we can output our calendar nicely
	ob_start();
	
	$last_month = $month == 1 ? 12 : $month - 1;
	$next_month = $month == 12 ? 1 : $month + 1;
	
	// Build the heading portion of the calendar table
	echo <<<EOS
	<table id="calendar">
	<tr>
		<td><a href="#" class="monthnav" onclick="getPrevMonth();return false;"> << Prev</a></td>
		<td colspan=5 class="month">$month_name $year</b></td>
		<td><a href="#" class="monthnav" onclick="getNextMonth();return false;">Next >></a></td>
	</tr>
	<tr class="daynames"> 
		<td>S</td><td>M</td><td>T</td><td>W</td><td>T</td><td>F</td><td>S</td>
	</tr>
EOS;
	
	foreach($weeks AS $week){
		echo '<tr class="week">'; 
		foreach($week as $day)
		{
			if($day == date('d', $current_time) && $month == date('m', $current_time) && $year == date('Y', $current_time))
				echo '<td class="today">'.$day.'</td>';
			else
				echo '<td class="days">'.$day.'</td>';
		}
		echo '</tr>';
	}
	
	foreach($scheduleDates as $scheduleDate){
	echo '<tr class="scheduleDate>';
	  foreach($scheduleDate as $day)
	  {
	   if($day==date('d',$current_time) && $month==date('m',$current_time) && $year==date('Y',$current_time))
	    echo' <td class="scheduleDate">'.$day.'</td>';
	}
	echo '</tr>';
	}
	foreach($noteDates as $noteDate){
	echo '<tr class="noteDate>';
	  foreach($noteDate as $day)
	  {
	   if($day==date('d',$current_time) && $month==date('m',$current_time) && $year==date('Y',$current_time))
	    echo '<td class="noteDate">'.$day.'</td>';
	}
	echo '</tr>';
	}
		
	echo '</table>';
	
	return ob_get_clean();
}

?>

Recommended Answers

All 3 Replies

I too would appreciate a reply to the above. Many thanks. :)

I'm unfamiliar with the php-function use_javascript('')
is it part of some libery? I don't see an include or require

also plaece use the code-tacs when posting code.
also indent (TAB) your functions, loops and conditions

In addition, your isAjax function is mixing javascript with PHP variables, that will not work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.