dubc07 Posted March 29, 2010 Share Posted March 29, 2010 Hello All, I have a calendar script that get the current days in month and outputs the days of month and then places a css style on the current day. Im trying to also place a different css on the days i have tasks or events on. I can run a while loop and grab the day which have events and echo them. however This creates duplicate days for $startday and dt. Can anyone help me figure out a different way of going about this so it only outputs one day not duplicates See Script below. <?php error_reporting(0); $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; } ?> <div class="cal_hld"><div class="cal_brd" id="calendar_div" style="width:207px; height:auto;"name="calendar_div"> <table width="200"> <tr> <td height="115" align="center"> <table width="100%" border="0" cellpadding="2" cellspacing="2"> <tr align="center"> <td colspan="7" bgcolor="#C4E1F3" style="color:#FFFFFF"><div style="width:190px; height:19px; float:left;"><strong></strong> <div style="width:19px; height:19px; float:left; margin-right:3px;"><a href="javascript:retrv(<?php echo "$prev_month , $prev_year"; ?>)"><img src="images/prev_mnt.png" width="19" height="19" border="0" /></a></div><div id="monthText" style="float:left; width:100px; font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#000; margin-left:22px;"><?php echo $monthNames[$cMonth-1].' '.$cYear; ?></div><div style="width:19px; height:19px; float:right; margin-left:15px;"><a href="javascript:retrv(<?php echo "$next_month , $next_year"; ?>)"><img src="images/next_mnt.png" width="19" height="19" border="0"/></a></div> </div></td> </tr> <tr class="days_st" style="padding-bottom:5px"> <td height="23" ><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 class="days_st" ><strong>S</strong></td> </tr> <?php $timestamp = mktime(0,0,0,$cMonth,1,$cYear); $maxday = date("t",$timestamp); $thismonth = getdate ($timestamp); $startday = $thismonth['wday']; $dy=date('d'); $mn=date('m'); // echo $mn; $strmn= preg_replace('/[\,]/', '', $cMonth); $resd=str_replace(",","",$cMonth); $yr=date('Y'); //echo "$resd $cYear"; for ($i=0; $i<($maxday+$startday); $i++) { $curday= $i - $startday + 1; if(($i % 7) == 0 ) echo "<tr>\n"; if($i < $startday) echo "<td></td>\n"; else if($curday == $dy && $mn == $strmn && $yr == $cYear){ echo "<td><a href=\"javascript:pulltask()\"><span class=\"blockss\">". ($i - $startday + 1) . "</span></a></td>\n"; }else{ //echo $cMonth; $realMonth=str_replace(",","",$cMonth); $t=mysql_query("SELECT * FROM table WHERE id='myid' AND month='$realMonth' AND yyear='$cYear'"); while($s=mysql_fetch_array($t)){ $dt=$s["dt"]; if($dt==($i - $startday + 1)){ echo "<td><a href=\"javascript:pulltask()\"><span class=\"blocksstsks\">". ($i - $startday + 1) . "</span></a></td>\n"; }else{ }} if($dt==($i - $startday + 1)){ }else{ //echo "<td><a href=\"javascript:pulltask()\"><span class=\"blocksstsks\">". ($i - $startday + 1) ."</span></a></td>\n"; echo "<td><a href=\"javascript:pulltask()\"><span class=\"blocker\">". ($i - $startday + 1) . "</span></a></td>\n"; } } if(($i % 7) == 6 ) echo "</tr>\n"; } ?> </table></td> </tr> </table> </div></div> ; Link to comment https://forums.phpfreaks.com/topic/196874-php-calendar-problem/ Share on other sites More sharing options...
andrewgauger Posted March 29, 2010 Share Posted March 29, 2010 while($s=mysql_fetch_array($t)){ $dt=$s["dt"]; if($dt==($i - $startday + 1)){ echo "<td><a href=\"javascript:pulltask()\"><span class=\"blocksstsks\">". ($i - $startday + 1) . "</span></a></td>\n"; }else{ }} if($dt==($i - $startday + 1)){ }else{ //echo "<td><a href=\"javascript:pulltask()\"><span class=\"blocksstsks\">". ($i - $startday + 1) ."</span></a></td>\n"; echo "<td><a href=\"javascript:pulltask()\"><span class=\"blocker\">". ($i - $startday + 1) . "</span></a></td>\n"; } The second conditional " if($dt==($i - $startday + 1)){" only executes after the while loop has completed, is this the desired behavior? Why are you checking the same conditional twice? If all you want to do is stylize the css then what I would do is create your complex conditional set a $style variable, and at the end of the conditional print the $style and$date as html instead of having 3 echo statements inside of conditions. That would be easier to debug. Link to comment https://forums.phpfreaks.com/topic/196874-php-calendar-problem/#findComment-1033578 Share on other sites More sharing options...
dubc07 Posted March 29, 2010 Author Share Posted March 29, 2010 Here is the updated code with the style set in it. It will style one of the days but. if i rap the rest within the while loop it will still echo the days how ever many times there are rows on the db. <?php for ($i=0; $i<($maxday+$startday); $i++) { $curday= $i - $startday + 1; if(($i % 7) == 0 ) echo "<tr>\n"; if($i < $startday) echo "<td></td>\n"; else if($curday == $dy && $mn == $strmn && $yr == $cYear){ echo "<td><a href=\"javascript:pulltask()\"><span class=\"blockss\">". ($i - $startday + 1) . "</span></a></td>\n"; }else{ //echo $cMonth; $realMonth=str_replace(",","",$cMonth); $t=mysql_query("SELECT * FROM table WHERE id='$id' AND month='$realMonth' AND year='$cYear'"); while($s=mysql_fetch_array($t)){ $dt=$s["dt"]; if($dt==($i - $startday + 1)){ $style='blocksstsks'; }else{ $style='blocker'; } } //echo "<td><a href=\"javascript:pulltask()\"><span class=\"blocksstsks\">". ($i - $startday + 1) ."</span></a></td>\n"; echo "<td><a href=\"javascript:pulltask()\"><span class=\"$style\">". ($i - $startday + 1) . "</span></a></td>\n"; } if(($i % 7) == 6 ) echo "</tr>\n"; } ?> any help is good Thanks Link to comment https://forums.phpfreaks.com/topic/196874-php-calendar-problem/#findComment-1033612 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.