Jump to content

Recommended Posts

Hi

 

Ive got the following code which produces a calendar, however I want to add different events to different dates. So how would I put an event in todays date for example?

 

<?php
error_reporting('0');
ini_set('display_errors', '0');

//gather variables from the user and break them down for this script

if(!isset($_REQUEST['date'])) {
$date = mktime(0,0,0,date('m'),date('d'), date('Y'));
} else {
$date =$_REQUEST['date'];

}

$day = date('d', $date);
$month = date('m', $date);
$year = date('Y', $date);

//Get the first day of the month
$month_start = mktime(0,0,0,$month, 1, $year);

//Get the friendly month name
$month_name = date('F', $month_start);

//Find first day of the week the month starts
$month_start_day = date('D', $month_start);

switch($month_start_day){

case "Mon": $offset = 0; break;
case "Tue": $offset = 1; break;
case "Wed": $offset = 2; break;
case "Thu": $offset = 3; break;
case "Fri": $offset = 4; break;
case "Sat": $offset = 5; break;
case "Sun": $offset = 6; break;
}

//determine how many days are in the previous month
if($month == 1) {
$num_days_last = cal_days_in_month(0, 12, ($year - 1));
} else {
$num_days_last = cal_days_in_month(1, ($month - 1), $year);
}

//determine how many days are in the current month
$num_days_current = cal_days_in_month(0, $month, $year);

//Build an array of the days
for($i = 1; $i <= $num_days_current; $i++) {
$num_days_array[] = $i;
}

//build an array from the last months days
for($i = 1; $i <= $num_days_last; $i++) {
$num_days_last_array[] = $i;
}

//check the offset for day of the week, none is needed if month starts on Sunday
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;
}

//count how many total days we have with these two arrays merged
$current_num = count($new_count);

//With 5 HTML table rows with 7 table data entries we will need a total
//of 35 TDs.  We will need to see from a third array how many days we need

if($current_num > 35 ) {
$num_weeks = 6;
$outset = (42 - $current_num);
} elseif($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[] = $i;
}

//separate the $new_count array into 7 day weeks
$weeks = array_chunk($new_count, 7);

$previous_link = "<a href=\"".$_SERVER['PHP_SELF']."?date=";
if($month == 1) {
$previous_link .= mktime(0,0,0,12,$day,($year - 1));
} else {
$previous_link .= mktime(0,0,0,($month - 1), $day, $year);
}
$previous_link .= "\"><< Prev</a>";

$next_link = "<a href=\"".$_SERVER['PHP_SELF']."?date=";
if($month == 12) {
$next_link .= mktime(0,0,0,1,$day,($year + 1));
} else {
$next_link .= mktime(0,0,0,($month + 1), $day, $year);
}
$next_link .= "\">Next >></a>";



?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />

<link rel="stylesheet" type="text/css" href="stylesheets/stylesheet.css">



</head>

<boby>
<div id="wrapper">
<div id="navigation">
		<div id="menuh-container">

		<div id="menuh">


		</div> 	<!-- end the menuh-container div -->  
		</div><!-- end the menuh div --> 

</div><!-- end navigation --> 

<div id="content">
<?php
echo "<table border=\"1\" cellpadding=\"2\" cellspacing=\"0\" width=\"999\" class=\"calendar\">\n".
		"<tr>\n".
		"<td colspan=\"7\">".
		"<table align=\"center\">".
		"<tr>".
		"<td colspan=\"2\" width=\"150\" align=\"left\">$previous_link</td>\n".
		"<td colspan=\"3\" width=\"200\" align=\"center\"><strong>$month_name $year</strong></td>\n".
		"<td colspan=\"2\" width=\"150\" align=\"right\"$next_link</td>\n".
		"</tr>\n".
		"</table>\n".
		"</td>\n".
		"<tr>\n".
		"<td><strong>Monday</strong></td><td><strong>Tuesday</strong></td><td><strong>Wednesday</strong></td><td><strong>Thursday</strong></td><td><strong>Friday</strong></td><td><strong>Saturday</strong></td><td><strong>Sunday</strong></td>\n".
		"</tr>\n";

//Break each key of the array into a week and create a new table row for each week
//with the days of the week in the table data

$i = 0;


foreach($weeks AS $week) {

	echo "<tr>\n";

	/*
		while ($row = mysql_fetch_array($selectquery)) {

					$party_host_id = $row['id'];
					$party_host_name = $row['hostname'];
					$party_calendar_entry = strtotime($row['partydate']);


			if($party_calendar_entry == $date) {

				$host_link = "<a href=\"party_sign_up.php?id=".$party_host_id."\">".$party_host_name."</a>";
				//echo "td class=\"days\">$host_link</td>\n";
			}//if $party_calendar_entry=$date	


		}//while*/	
			foreach($week as $d) {

				if($i < $offset_count) {
					$day_link = "<a href=\"".$_SERVER['PHP_SELF']."?date=".mktime(0,0,0,($month - 1),$d,$year)."\">$d</a>";
					echo "<td class=\"nonmonthdays\">$day_link.$host_link</td>\n";
				}

				if(($i >= $offset_count) && ($i < ($num_weeks * 7) - $outset)) {
					//if date = first day of next month?
					if($date == mktime(0,0,0,$month, $d, $year)) {
						echo "<td class=\"today\">$d</td>\n";
					} else {
						echo "<td class=\"days\"><a href=\"".$_SERVER['PHP_SELF']."?date=".mktime(0,0,0,$month,$d,$year)."\">$d</a></td>\n";
					}

				} elseif($i >= ($num_weeks * 7) - $outset) {
					$day_link = "<a href=\"".$_SERVER['PHP_SELF']."?date=".mktime(0,0,0,$month + 1,$d,$year)."\">$d</a>";
					echo "<td class=\"nonmonthdays\">$day_link.$host_link</td>\n";
				}

			}//foreach $week as $d


			$i++;




		echo "</tr>\n";

}//foreach $weeks as $week

echo '<tr><td colspan="7" class="days"> </td></tr>';
echo '</table>';




?>




</div><!--end content-->
</div><!--end wrapper-->
</body>

</html>

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/90078-calendar/
Share on other sites

I don't think you can do a GROUP BY Date in mysql and have multi events show up as sub arrays of the bigger array of your fetch.  So you will need to query out all your event data and then recreate the array in php like

<?php
while($row = mysql_fetch_assoc($result)){
    $count = count($data[$row['Date']);
    $data[$row['Date'][$count]['Name'] = $row['Date'];
    $data[$row['Date'][$count]['Start_time'] = $row['Start_time'];
    #etc.
}
?>

and then in your calendar table product have a way to reproduce a date the same as the array keys and say

<?php
$date = "2/2/2008";
foreach($data[$date] as $key=> $value){
   echo "Name: ".$value['Name']."<br />";
   echo "Start Time: ".$value['Start_time'];
}
?>

 

Get the idea.  I don't think mysql will let you get that sort of sort off a GROUP BY ORDER BY query so you will need to do this or find a mysql alternative to the first code snippete

Link to comment
https://forums.phpfreaks.com/topic/90078-calendar/#findComment-461860
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.