benhullah Posted March 13, 2018 Share Posted March 13, 2018 Hi guys, im working on a project for a client at work and they want a scheduler/calendar integrating into their web application. I have built the calendar however i cant get the first line to display correctly. it always starts within the first cell rather than the the correct one for the corresponding day (see below screenshot); here is the code that generates the above layout, am i missing something obvious or can somebody ppoint out where im going wrong as its driving me mad! $month=gmdate("m");} // Find today's month $day=gmdate("d"); // Finds today's date $year=gmdate("Y"); // Finds today's year $no_of_days=gmdate('t',gmmktime(0,0,0,$month,1,$year)); // This is to calculate number of days in a month $mn=gmdate('F',gmmktime(0,0,0,$month,1,$year)); // Month is calculated to display at the top of the calendar $yn=gmdate('Y',gmmktime(0,0,0,$month,1,$year)); // Year is calculated to display at the top of the calendar $j=gmdate('w',gmmktime(0,0,0,$month,1,$year)); // This will calculate the week day of the first day of the month for($k=0;$k<=$j;$k++){ $adj="<td width='117' height='117' align='left' valign='top'> </td>"; } /// top line showing name of the days of the week echo"<table align='center' valign='top' border='0' cellpadding='5' cellspacing='2'>\n" ."<tr><td align='center' valign='middle' colspan='7'>".$mn." ".$yn."</td></tr>\n" ."<tr><td colspan='7' height='10'></td></tr>\n" ."<tr><td align='center' valign='middle'><b>Sun</b></td><td align='center' valign='middle'><b>Mon</b></td><td align='center' valign='middle'><b>Tue</b></td><td align='center' valign='middle'><b>Wed</b></td><td align='center' valign='middle'><b>Thu</b></td><td align='center' valign='middle'><b>Fri</b></td><td align='center' valign='middle'><b>Sat</b></td></tr>\n" ////// End of the top line showing name of the days of the week////////// ."<tr>"; for($i=1;$i<=$no_of_days;$i++){ #if($i<10){$i="0".$i."";} $getevent_sql=@mysql_query("SELECT * FROM schedule WHERE date>='".gmmktime(0,0,0,$month,$i,$year)."' AND date<='".gmmktime(23,59,59,$month,$i,$year)."' ORDER BY date ASC, title ASC"); $getevent_num=@mysql_num_rows($getevent_sql); $getevent_sql=@mysql_query("SELECT * FROM schedule WHERE date>='".gmmktime(0,0,0,$month,$i,$year)."' AND date<='".gmmktime(23,59,59,$month,$i,$year)."' ORDER BY date ASC, title ASC LIMIT 0,2"); $calendardisplay_num=$getevent_num; echo "<td width='117' height='117' align='left' valign='top' class='"; if ($i==$day) { echo"caltoday"; } else { echo"calday"; } echo"'><i><a href='./schedule.php?view=day&day=$i&month=$month&year=$year'>$i</a></i><br />"; while($eventinfo=@mysql_fetch_array($getevent_sql)) { $id=$eventinfo['id']; $title=$eventinfo['title']; echo"<a href='./schedule.php?view=event&id=$id'>"; if (strlen(substr("".str_replace("’", "'", "".$title."")."", 0,15))>=15) { echo"".substr("".str_replace("’", "'", "".$title."")."", 0,15)."..."; } else { echo"".str_replace("’", "'", "".$title."").""; } echo"</a><br />"; } if($calendardisplay_num>2) { echo"<a href='./schedule.php?view=day&day=$i&month=$month&year=$year'><i><font class='smalltext'>...more</font></i></a>"; } echo"</td>"; // This will display the date inside the calendar cell $adj=''; $j++; if($j==7){ echo"</tr><tr>"; $j=0; } } echo"</tr><tr><td height='3' colspan='7'></td></tr></table>\n"; Quote Link to comment https://forums.phpfreaks.com/topic/306821-php-schedulercalender/ Share on other sites More sharing options...
requinix Posted March 13, 2018 Share Posted March 13, 2018 You have to put empty cells into the first row to fill up the space before the first day. If the month doesn't already start on Sunday, of course. You should also pad the last row of the table with empty cells if the month doesn't end on Saturday. Quote Link to comment https://forums.phpfreaks.com/topic/306821-php-schedulercalender/#findComment-1557118 Share on other sites More sharing options...
benhullah Posted March 13, 2018 Author Share Posted March 13, 2018 You have to put empty cells into the first row to fill up the space before the first day. If the month doesn't already start on Sunday, of course. You should also pad the last row of the table with empty cells if the month doesn't end on Saturday. yeah i tried that and i cant get the layout to stay correct. it either fills the page with empty cells apart from todays date or just one long table with no content! this is having my life i tell you! Quote Link to comment https://forums.phpfreaks.com/topic/306821-php-schedulercalender/#findComment-1557119 Share on other sites More sharing options...
requinix Posted March 13, 2018 Share Posted March 13, 2018 Ah, I skipped over that one line. for($k=0;$k "; }Couple problems: 1. If the month starts on Sunday then $j=0 and you should not be showing empty cells. So the for condition should be $k 2. You don't do anything with $adj. You can forget about $adj entirely by moving the loop to just after that first for weeks and outputting the empty cell immediately instead of putting it in a variable. Quote Link to comment https://forums.phpfreaks.com/topic/306821-php-schedulercalender/#findComment-1557120 Share on other sites More sharing options...
Barand Posted March 13, 2018 Share Posted March 13, 2018 Also, $adj will only ever be a single cell. You need something like for($k=0, $adj=''; $k<$j; $k++){ $adj .= "<td width='117' height='117' align='left' valign='top'> </td>"; } Quote Link to comment https://forums.phpfreaks.com/topic/306821-php-schedulercalender/#findComment-1557121 Share on other sites More sharing options...
benhullah Posted March 14, 2018 Author Share Posted March 14, 2018 (edited) Also, $adj will only ever be a single cell. You need something like for($k=0, $adj=''; $k<$j; $k++){ $adj .= "<td width='117' height='117' align='left' valign='top'> </td>"; } Ah, I skipped over that one line. for($k=0;$k<=$j;$k++){ $adj="<td width='117' height='117' align='left' valign='top'> </td>"; }Couple problems:1. If the month starts on Sunday then $j=0 and you should not be showing empty cells. So the for condition should be $k<$j. 2. You don't do anything with $adj. You can forget about $adj entirely by moving the loop to just after that first <tr> for weeks and outputting the empty cell immediately instead of putting it in a variable. OK, so taking that into account i have added the line of code after the first <tr> and its made no difference. I think im doing it wrong (not suprising!); <?php class Calendar { /** * calculate number of weeks in a particular month */ private function _weeksInMonth($month=null,$year=null){ if( null==($year) ) { $year = date("Y",time()); } if(null==($month)) { $month = date("m",time()); } // find number of days in this month $daysInMonths = $this->_daysInMonth($month,$year); $numOfweeks = ($daysInMonths%7==0?0:1) + intval($daysInMonths/7); $monthEndingDay= date('N',strtotime($year.'-'.$month.'-'.$daysInMonths)); $monthStartDay = date('N',strtotime($year.'-'.$month.'-01')); if($monthEndingDay<$monthStartDay){ $numOfweeks++; } return $numOfweeks; } /** * calculate number of days in a particular month */ private function _daysInMonth($month=null,$year=null){ if(null==($year)) $year = date("Y",time()); if(null==($month)) $month = date("m",time()); return date('t',strtotime($year.'-'.$month.'-01')); } } if(isset($prm) and $prm > 0){ $month=$prm+$chm;}else{ $month=gmdate("m");} // Find today's month $day=gmdate("d"); // Finds today's date $year=gmdate("Y"); // Finds today's year $no_of_days=gmdate('t',gmmktime(0,0,0,$month,1,$year)); // This is to calculate number of days in a month $mn=gmdate('F',gmmktime(0,0,0,$month,1,$year)); // Month is calculated to display at the top of the calendar $yn=gmdate('Y',gmmktime(0,0,0,$month,1,$year)); // Year is calculated to display at the top of the calendar $j=gmdate('w',gmmktime(0,0,0,$month,1,$year)); // This will calculate the week day of the first day of the month /// top line showing name of the days of the week echo"<table align='center' valign='top' border='0' cellpadding='5' cellspacing='2'>\n" ."<tr><td align='center' valign='middle' colspan='7'>".$mn." ".$yn."</td></tr>\n" ."<tr><td colspan='7' height='10'></td></tr>\n" ."<tr><td align='center' valign='middle'><b>Sun</b></td><td align='center' valign='middle'><b>Mon</b></td><td align='center' valign='middle'><b>Tue</b></td><td align='center' valign='middle'><b>Wed</b></td><td align='center' valign='middle'><b>Thu</b></td><td align='center' valign='middle'><b>Fri</b></td><td align='center' valign='middle'><b>Sat</b></td></tr>\n" ////// End of the top line showing name of the days of the week////////// ."<tr>"; # code suggested from phpfreaks.com # for($k=0, $adj=''; $k<$j; $k++){ $adj .= "<td width='117' height='117' align='left' valign='top'> </td>"; } ##################################### for($i=1;$i<=$no_of_days;$i++){ if($i<10){$i="0".$i."";} $getevent_sql=@mysql_query("SELECT * FROM schedule WHERE date>='".gmmktime(0,0,0,$month,$i,$year)."' AND date<='".gmmktime(23,59,59,$month,$i,$year)."' ORDER BY date ASC, title ASC"); $getevent_num=@mysql_num_rows($getevent_sql); $getevent_sql=@mysql_query("SELECT * FROM schedule WHERE date>='".gmmktime(0,0,0,$month,$i,$year)."' AND date<='".gmmktime(23,59,59,$month,$i,$year)."' ORDER BY date ASC, title ASC LIMIT 0,2"); $calendardisplay_num=$getevent_num; echo "<td width='117' height='117' align='left' valign='top' class='"; if ($i==$day) { echo"caltoday"; } else { echo"calday"; } echo"'><i><a href='./schedule.php?view=day&day=$i&month=$month&year=$year'>$i</a></i><br />"; while($eventinfo=@mysql_fetch_array($getevent_sql)) { $id=$eventinfo['id']; $title=$eventinfo['title']; echo"<a href='./schedule.php?view=event&id=$id'>"; if (strlen(substr("".str_replace("’", "'", "".$title."")."", 0,15))>=15) { echo"".substr("".str_replace("’", "'", "".$title."")."", 0,15)."..."; } else { echo"".str_replace("’", "'", "".$title."").""; } echo"</a><br />"; } if($calendardisplay_num>2) { echo"<a href='./schedule.php?view=day&day=$i&month=$month&year=$year'><i><font class='smalltext'>...more</font></i></a>"; } echo"</td>"; // This will display the date inside the calendar cell $adj=''; $j++; if($j==7){ echo"</tr><tr>"; $j=0; } } echo"</tr><tr><td height='3' colspan='7'></td></tr></table>\n"; ?> Edited March 14, 2018 by benhullah Quote Link to comment https://forums.phpfreaks.com/topic/306821-php-schedulercalender/#findComment-1557148 Share on other sites More sharing options...
Solution requinix Posted March 14, 2018 Solution Share Posted March 14, 2018 OK, so taking that into account i have added the line of code after the first and its made no difference. I think im doing it wrong (not suprising!);You still aren't actually using it. You set it, and you append to it, and you clear it, but you don't use it. 1 Quote Link to comment https://forums.phpfreaks.com/topic/306821-php-schedulercalender/#findComment-1557157 Share on other sites More sharing options...
benhullah Posted March 15, 2018 Author Share Posted March 15, 2018 You still aren't actually using it. You set it, and you append to it, and you clear it, but you don't use it. Well, wouldn't you know. I played about with a few locations in the code to place the $adj variable and BOOM! problem resolved and layout is now perfect! Thanks guys for ou help and guidance Quote Link to comment https://forums.phpfreaks.com/topic/306821-php-schedulercalender/#findComment-1557179 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.