Jump to content

highlighting the week in...


jimfog

Recommended Posts

Below you will find the code for this calendar https://skydrive.live.com/redir.aspx?cid=be27434b2aac8130&resid=BE27434B2AAC8130!229&parid=BE27434B2AAC8130!140

function leftside_cal_alt($startmonth, $endmonth, $startyear) {	 global $cYear, $cMonth, $monthNames, $newdate,$days,$prevMonth,			 $nextMonth,$monday,$k,$monday,$sunday;			 $cmonth=date("m");	 $cYear=date("Y");	 $currentyear = "";	 $currentmonth = "";	 $currentday = "";		 for ($year = $startyear; $year < $startyear + 1; $year++) {		 for ($mon = $startmonth; $mon <= $endmonth; $mon++) {			 if ($mon != $currentmonth) {				 $month = mktime(0, 0, 0, $mon, 1, $year);				 if ($year != $currentyear) {					 ?>					 <table border="0"><?php echo'<a href="'.$_SERVER["PHP_SELF"]."?newdate=".$prevMonth .'"><img src="Images/prevmont_arrow.png" width="12" height="14" alt="προηγούμενος μήνας" /></a>';?>						 <tr>					    <td colspan="7"class="monthyea"><?php echo $monthNames[date("n", strtotime($newdate)) - 1] . ' ' . date("Y", strtotime($newdate)); ?></td>						 </tr>   <?php echo '<a href="' . $_SERVER["PHP_SELF"] . "?newdate=" . $nextMonth . '"><img src="Images/nextmont_arrow.png" width="12" height="14"  alt="επόμενος μήνας" /></a>';?>						 <tr>									   <td align="center" >ΔΕ</td>									    <td align="center" >ΤΡ</td>									    <td align="center" >ΤΕ</td>									    <td align="center" >ΠΕ</td>									    <td align="center" >ΠΑ</td>									    <td align="center" >ΣΑ</td>									   <td align="center">ΚΥ</td>								    </tr>				 					 <?php//////////////////week days					 $startdate = date("N", $month); // numeric value of day sat = 6, sunday = 7					 $totaldays_in_mon = date("t", $month);					 $prev_totaldays_in_mon = date("t", $month - 1);					 $prev_mon_residue = ($prev_totaldays_in_mon - $startdate) + 2;					 $datecount = 0;					 $colcout = 0;//αυτή είναι η στήλη					 $rowcount = 0;					 $next_month_residue = 0;					 for ($date = 1; $date < ($totaldays_in_mon + $startdate); $date++) {						 $colcout++;						 if ($colcout == 1) {							 ?>						    <!-- εδώ ...πρέπει να μπεί το class						    για την περίπτωση όπου υφίσταται month view -->	 <tr class="john" >									 <?php   								 }							  if ($date >= $startdate) {				   $datecount++;				   if ($colcout == 6 || $colcout == 7) {						 $cellclass = "weekend";				   } else {					   $cellclass = "weekdays";				   }									   if(isset ($_GET['week'])||isset ($_GET['weekview']))												   {					   if(($monday->format('j')) == $datecount)							 						   {echo '<tr>';						   for ($k =0; $k <7; $k++)							   if ((date("d") == ($datecount+$k)) && (date("n") == (date("n", strtotime($newdate)))))								  echo "<td class='current-day-highlited' align='center' valign='middle' height='20px'>" .($datecount+$k)."</td>\n";									 else								  echo "<td class='weekgreen' align='center' valign='middle' height='20px'>" .($datecount+$k). "</td>\n";						  						    echo '</tr>';								   }						  						    if(($datecount>=($monday->format('j')))&&($datecount<=($sunday->format('j'))))						    continue;						   						    if ((date("d") == $datecount) && (date("n") == (date("n", strtotime($newdate)))))						    echo "<td class='current-day-highlited' align='center' valign='middle' height='20px'>" .$datecount."</td>\n";						    else						    echo "<td class=".$cellclass.'id=dateid_'.$year.str_pad(date('m',$month),2,'0',STR_PAD_LEFT).str_pad($datecount,2,'0',STR_PAD_LEFT).'>'.$datecount."</td>";				 						   				   }				   elseif((date("d",strtotime($newdate))==$datecount)&&(date("d")==$datecount)&&(date("n")==(date("n", strtotime($newdate)))))				   echo "<td class='nav-cur-highlited' align='center' valign='middle' height='20px'>".$datecount."</td>\n";								   elseif((date("d") == $datecount)&& (date("n") == (date("n", strtotime($newdate)))))				   echo "<td class='current-day-highlited' align='center' valign='middle' height='20px'>" .$datecount. "</td>\n";				   elseif (date("d", strtotime($newdate)) == $datecount)				   echo "<td class='navigation-day-highlited' align='center' valign='middle' height='20px'>" .$datecount. "</td>\n";					 else	   				   {echo "<td class=".$cellclass.'id=dateid_' . $year . str_pad(date('m', $month), 2, '0', STR_PAD_LEFT)				    . str_pad($datecount, 2, '0', STR_PAD_LEFT).'>'.$datecount."</td>";}																												   									 ?>			  		   		    <?php						 } elseif ($colcout == 6 || $colcout == 7) {							 ?>									 <td class="mon_weekend_residue"><?php echo $prev_mon_residue; ?></td>									 <?php								 } else {									 ?>									 <td class="mon_weekdays_residue"><?php echo $prev_mon_residue; ?></td>								 <?php								 $prev_mon_residue++;							 }							 if ($colcout % 7 == 0) {								 $colcout = 0;								 $rowcount++;							   								 echo '</tr>';														 }						 }						 if ($colcout < 7 && $colcout != 0) {							 for ($emptycells = $colcout; $emptycells < 7; $emptycells++) {								 $next_month_residue++;								 if ($emptycells == 5 || $emptycells == 6) {									 echo '<td class="mon_weekend_residue">' .κ. $next_month_residue . '</td>';								 } else {									 echo '<td class="mon_weekdays_residue">' .κ. $next_month_residue . '</td>';								 }							 }							 echo '</tr>';							 $rowcount++;						 }					 }					 $currentmon = $mon;				 }				 ?> 			 </table>					 <?php				 }				 $currentyear = $year;			 }		 }

The problem, as seen in the photo is that when highlighting the week-in the end/beginning of the month-the calendar is ruined.For example, now that we are in the last week of the April the first days of May are gone-which otherwise ought to be shown there.Other similar problems arise also when i navigate from week to week which are not shown here. Bottom line:I am trying to figure out code to highlight the week without problems,not just the current but also the one the user goes by navigation in a future/past date. Thanks and sorry for the lengthy code above

Link to comment
Share on other sites

I think that the 'continue;' is causing to miss an important loop to identify the rowcount and colcount it is currently on , so as far as it knows its reached the total of number required, so it ends prematurely.
It probably this is the problem-continue is required though(for another reason) to break the loop.
Link to comment
Share on other sites

Anything?

Link to comment
Share on other sites

Why would the highlighting have anything to do with which days get displayed? Those are 2 completely separate tasks for the calendar. Your first step should be to get the calendar to always display the full range of days, switching to another month or whatever the current day or week is should not even come into play in the code that displays each day. You need to display all days no matter what, so the code to display the days shouldn't even really be in any if statements or whatever else, you always want to display the day. The continue is a problem because it's similar to an if statement, it's going to skip the rest of the code. Since that code displays the day, that's a problem. Look at this example:

<div class="calendar-container"> <?php// check for the date in the URL$m = isset($_GET['m']) ? intval($_GET['m']) : 0;$y = isset($_GET['y']) ? intval($_GET['y']) : 0;if (!$m) $m = date('n');if (!$y) $y = date('Y'); // get first and last days of month, and today$first = mktime(0, 0, 0, $m, 1, $y);$last = mktime(0, 0, 0, $m + 1, 1, $y) - 86400;$today = mktime(0, 0, 0);// previous month$prev_m = date('n', $first - 1);$prev_y = date('Y', $first - 1);$prev_text = date('F', $first - 1) . ' ' . $prev_y; // next month$next_m = date('n', $last + 86400);$next_y = date('Y', $last + 86400);$next_text = date('F', $last + 86400) . ' ' . $prev_y;?>   <div class="calendar-nav">    <div style="text-align: left;">	  < <a href="calendar.php?m=<?php echo $prev_m; ?>&y=<?php echo $prev_y; ?>"><?php echo $prev_text; ?></a>    </div>    <div style="text-align: center; font-weight: bold; font-size: 120%;">	  <?php echo date('F', $first) . ' ' . date('Y', $first); ?>    </div>    <div style="float: right; text-align: right;">	  <a href="calendar.php?m=<?php echo $next_m; ?>&y=<?php echo $next_y; ?>"><?php echo $next_text; ?></a> >    </div>  </div>  <table class="calendar-body">    <thead>	  <tr>	    <td>Sun</td>	    <td>Mon</td>	    <td>Tue</td>	    <td>Wed</td>	    <td>Thu</td>	    <td>Fri</td>	    <td>Sat</td>	  </tr>    </thead>    <tbody>   <?php  // figure out which day to start on  $day = date('w', $first);  if ($day != 0)  {    $cur = $first - (86400 * $day);  }  else  {    $cur = $first;  }   $done = false;  while (!$done)  {    $date = getdate($cur);    if ($date['wday'] == 0) // sunday    {	  echo '<tr>';    }    echo '<td class="calendar-day';    if ($date['mon'] != $m)	  echo ' calendar-disabled';    if ($cur == $today)	  echo ' calendar-today';    echo '">';     echo '<span class="calendar-date">' . $date['mday'] . '</span>';    if ($date['mon'] == $m)    {	  // get events for the current day and display them    }     echo '</td>';    if ($date['wday'] == 6) // saturday    {	  echo '</tr>';	  if ($cur == $last || $date['mon'] > $m)	    $done = true;    }    $cur += 86400;  }  ?>  </tbody></table></div>

Notice that inside that while loop that nearly the first thing it does is display the day. It always does that, there is no if statement, no continue, etc. There are if statements to determine which CSS class to put for that day, but it always displays the day regardless of anything else. It sounds like you're over-thinking the calendar, it really isn't that complex of a problem. Sorry about the inconsistent indenting, it never fails to amaze me that IPB can screw up something as basic as copying and pasting in their forum.

Link to comment
Share on other sites

Why would the highlighting have anything to do with which days get displayed?
I want to highlight the week as outlook does https://skydrive.live.com/redir.aspx?cid=be27434b2aac8130&resid=BE27434B2AAC8130!230&parid=BE27434B2AAC8130!140&authkey=!APJhbiKPcxzm1z8 Look in the picture, and i know that continue is the causes the problem in the loop. I will see what am I going to do
Link to comment
Share on other sites

I understand you want to add formatting. This is the part that adds CSS classes to each day:

    echo '<td class="calendar-day';    if ($date['mon'] != $m)		  echo ' calendar-disabled';    if ($cur == $today)		  echo ' calendar-today';    echo '">';

So you just need another if statement to check if it's the current week, and add a class if it is to do the highlighting. That's what the calendar-today class does, it puts a border and different background color for the current day.

Link to comment
Share on other sites

Don't target the td cells and apply class, target the tr and apply id ref, the id ref when used with any current classes will take precedence, it just a mater of using strtotime with 'last Monday' and 'next Sunday' to determine that the date falls between the two for the current row, if it does apply id ref to tr, if not echo normal '<tr>'.

Link to comment
Share on other sites

before proceeding and seeing as a whole this issue(of week highlighting) it is crucial i solve another problem(smaller one)that will help solving the highlighting one. For this smaller problem i am going to make another post-do not want mess things up.And then i will return again here.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...