akshay Posted March 8, 2010 Share Posted March 8, 2010 Hello. I'm trying to make an online booking calendar. I used following code for generating PHP calendar: (code works fine! u may skip reading this one) <?php $date=time(); $day=date('d',$date); $month=date('m',$date); $year=date('Y',$date); $first_day=mktime(0,0,0,$month,1,$year); $title=date('F',$first_day); $day_of_week=date('D',$first_day); switch($day_of_week) { case "Sun": $blank=0; break; case "Mon":$blank=1; break; case "Tue": $blank=2; break; case "Wed":$blank=3; break; case "Thu": $blank=4; break; case "Fri":$blank=5; break; case "Sat": $blank=6; break; } $days_in_month=cal_days_in_month(0,$month,$year); //0 is Calendar Number, Example: Gregorian..,etc.. php.net: $num = cal_days_in_month(CAL_GREGORIAN, 8, 2003); // 31 echo "<table border=1 width=294>"; echo "<tr><th colspan=7>$title - $year</th></tr>"; echo "<tr>"; $dz=array("S","M","T","W","T","F","S"); foreach($dz as $value) { print "<td width=42> $value </td>"; } echo "</tr>"; $day_count=1; echo "<tr>"; while ($blank>0) { echo "<td></td>"; $blank--; $day_count++; } $day_num=1; while ($day_num<=$days_in_month) { echo "<td> $day_num </td>"; $day_num++; $day_count++; if($day_count>7) { echo "</tr><TR>"; $day_count=1; } } while($day_count>1 && $day_count<7) { echo "<td> </td>"; $day_count++; } echo "</tr></table>"; ?> But, what about this: I want in the same calendar, if the dates are 1st,6st,7th,8th (Examplary) , i.e. one from $working_days array, those numbers should be printed in green color and hyperlinked to /book.php?day=(day of month number). But, this code prints all numbers in month as 1,6,7,8 and loops the calendar nonstop! Code: <?php $date=time(); $day=date('d',$date); $month=date('m',$date); $year=date('Y',$date); $first_day=mktime(0,0,0,$month,1,$year); $title=date('F',$first_day); $day_of_week=date('D',$first_day); switch($day_of_week) { case "Sun": $blank=0; break; case "Mon":$blank=1; break; case "Tue": $blank=2; break; case "Wed":$blank=3; break; case "Thu": $blank=4; break; case "Fri":$blank=5; break; case "Sat": $blank=6; break; } $days_in_month=cal_days_in_month(0,$month,$year); //0 is Calendar Number, Example: Gregorian..,etc.. php.net: $num = cal_days_in_month(CAL_GREGORIAN, 8, 2003); // 31 echo "<table border=1 width=294>"; echo "<tr><th colspan=7>$title - $year</th></tr>"; echo "<tr>"; $dz=array("S","M","T","W","T","F","S"); foreach($dz as $value) { print "<td width=42> $value </td>"; } echo "</tr>"; $day_count=1; echo "<tr>"; while ($blank>0) { echo "<td></td>"; $blank--; $day_count++; } $day_num=1; $working_days=array(1,6,7,; while ($day_num<=$days_in_month) { foreach($working_days as $wd) { if($day_num=$wd) { echo '<td><a href="/book.php?day='.$day_num.'"><font color="darkgreen"> '.$day_num.' </font></a></td>'; } //end if else { echo "<td> $day_num </td>"; } //end else } //end foreach $day_num++; $day_count++; if($day_count>7) { echo "</tr><TR>"; $day_count=1; } } while($day_count>1 && $day_count<7) { echo "<td> </td>"; $day_count++; } echo "</tr></table>"; ?> Please help. All i need to do is, if dates are from the array/already specified; print them in green with a hyperlink : /book.php?day=(that day of month, in numbers) Thank you Akshay Quote Link to comment https://forums.phpfreaks.com/topic/194481-booking-system-calendar/ Share on other sites More sharing options...
gwolgamott Posted March 8, 2010 Share Posted March 8, 2010 I used a foreach and some slight modification to your code to do that. Hopefully this helps you. What I did was the same as you, except I just created another variable used for printing the the calendar number. $day_num_t so before the foreach it is equal to day_num unless the foreach loop changes it. Your code gets stuck with reference I think (not really sure, maybe someone else here can shed light on that), not sure why so I just took the code for your calendar that worked and did it this method with using unset to reset the foreach reference to $value ($wd in your code). <?php //////////////////added code//////////////////////// //special colors $special = array(4,9); /////////////////////added code////////////////// $date=time(); $day=date('d',$date); $month=date('m',$date); $year=date('Y',$date); $first_day=mktime(0,0,0,$month,1,$year); $title=date('F',$first_day); $day_of_week=date('D',$first_day); switch($day_of_week) { case "Sun": $blank=0; break; case "Mon":$blank=1; break; case "Tue": $blank=2; break; case "Wed":$blank=3; break; case "Thu": $blank=4; break; case "Fri":$blank=5; break; case "Sat": $blank=6; break; } $days_in_month=cal_days_in_month(0,$month,$year); //0 is Calendar Number, Example: Gregorian..,etc.. php.net: $num = cal_days_in_month(CAL_GREGORIAN, 8, 2003); // 31 echo "<table border=1 width=294>"; echo "<tr><th colspan=7>$title - $year</th></tr>"; echo "<tr>"; $dz=array("S","M","T","W","T","F","S"); foreach($dz as $value) { print "<td width=42> $value </td>"; } echo "</tr>"; $day_count=1; echo "<tr>"; while ($blank>0) { echo "<td></td>"; $blank--; $day_count++; } $day_num=1; while ($day_num<=$days_in_month) { ///////////////////added code here ///////////////// $day_num_t = $day_num; foreach ($special as $value) { //echo $value."val<br>"; //echo $day_num."num<br>"; if($value == $day_num) { //echo "same<br><br>"; $day_num_t = '<a href="/book.php?day='.$day_num.'"><font color="darkgreen"> '.$day_num.' </font></a>'; } } unset($value); // break the reference with the last element ///////////added code here //////////////////////////// //////////////////CHANGED HERE TOO////////// echo "<td> $day_num_t </td>"; ////////////CHANGED HERE TOO/////////////// $day_num++; $day_count++; if($day_count>7) { echo "</tr><TR>"; $day_count=1; } } while($day_count>1 && $day_count<7) { echo "<td> </td>"; $day_count++; } echo "</tr></table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/194481-booking-system-calendar/#findComment-1023034 Share on other sites More sharing options...
sasa Posted March 8, 2010 Share Posted March 8, 2010 change while ($day_num<=$days_in_month) { foreach($working_days as $wd) { if($day_num=$wd) { echo '<td><a href="/book.php?day='.$day_num.'"><font color="darkgreen"> '.$day_num.' </font></a></td>'; } //end if else { echo "<td> $day_num </td>"; } //end else } //end foreach to while ($day_num<=$days_in_month) { //foreach($working_days as $wd) { if(in_array($day_num,$working_days)) { echo '<td><a href="/book.php?day='.$day_num.'"><font color="darkgreen"> '.$day_num.' </font></a></td>'; } //end if else { echo "<td> $day_num </td>"; } //end else //} //end foreach Quote Link to comment https://forums.phpfreaks.com/topic/194481-booking-system-calendar/#findComment-1023047 Share on other sites More sharing options...
akshay Posted March 9, 2010 Author Share Posted March 9, 2010 Thanks a lot to both of you!!!! Akshay Quote Link to comment https://forums.phpfreaks.com/topic/194481-booking-system-calendar/#findComment-1023343 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.