Jump to content

Calander Help


ScottMac

Recommended Posts

Hi, I'm new here so I'm very sorry if this has been brought up before. I was looking back through some old work and found a calander I was working on in Php. I thought I would try fix it up as I really enjoyed working on it but I've come across a problem that I can't fix.

 

Basically, I want a calander to be shown one month at a time on the screen. But in order to choose which month is shown a drop box is present at the top so the user can just select a month and that month appears. Heres the code I've got so far: Any suggestions?

 

<?php

$monthNames = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

?>

 

<?php

if (!isset($_REQUEST["month"])) $_REQUEST["month"] = date("n");

if (!isset($_REQUEST["year"])) $_REQUEST["year"] = date("Y");

?>

 

<?php

$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;

}

?>

 

 

 

<html>

<head>

<title>Header</title>

 

<script type="text/javascript" src="ip.php"></script>

 

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

 

</head>

 

<body id="homepage">

 

 

<p><font size="12" face="blackadder ITC" font color ="#FFFFFF" ><Center>Leona Lewis</Center></p></font>

<p><font size="4" font color ="#FFFFFF" >Leona Lewis is an international superstar, topping the charts in 34 countries with the hit single Bleeding Love and producing 3 Number 1 hits in the UK. Leona Lewis is fast becoming one of the biggest superstars in the world.</p></font>

 

 

 

<center>

<tr>

<td align="center">

<table width="50%" border="0" cellpadding="2" cellspacing="2">

<tr align="center">

<td colspan="7" bgcolor="#6633CC" style="color:#FFFFFF"><strong><?php echo $monthNames[$cMonth-1].' '.$cYear; ?></strong></td>

</tr>

<tr>

<td align="center" bgcolor="#7D1B7E" style="color:#FFFFFF"><strong>S</strong></td>

<td align="center" bgcolor="#7D1B7E" style="color:#FFFFFF"><strong>M</strong></td>

<td align="center" bgcolor="#7D1B7E" style="color:#FFFFFF"><strong>T</strong></td>

<td align="center" bgcolor="#7D1B7E" style="color:#FFFFFF"><strong>W</strong></td>

<td align="center" bgcolor="#7D1B7E" style="color:#FFFFFF"><strong>T</strong></td>

<td align="center" bgcolor="#7D1B7E" style="color:#FFFFFF"><strong>F</strong></td>

<td align="center" bgcolor="#7D1B7E" style="color:#FFFFFF"><strong>S</strong></td>

</tr>

 

 

<?php

$timestamp = mktime(0,0,0,$cMonth,1,$cYear);

$maxday = date("t",$timestamp);

$thismonth = getdate ($timestamp);

$startday = $thismonth['wday'];

 

 

$newdate = date('Y-m-j',$timestamp);

$newdate = strtotime ( '-1 day' , strtotime ( $newdate ) ) ;

$newdate = date('Y-m-j',$newdate);

 

for ($i=0; $i<($maxday+$startday); $i++) {

if(($i % 7) == 0 ) echo "<tr>\n";

if($i < $startday) echo "<td></td>\n";

else {

$newdate = strtotime ( '+1 day' , strtotime ( $newdate ) ) ;

 

$newdate = date ( 'Y-m-j' , $newdate );

echo "<td align='center' valign='middle' style='height:20px'>";

 

echo "<a href='".$newdate."'>".($i - $startday + 1) ."</a>";

 

echo "</td>\n";

}

if(($i % 7) == 6 ) echo "</tr>\n";

}

?>

 

</table>

</td>

</tr>

</table>

</center>

Link to comment
https://forums.phpfreaks.com/topic/199463-calander-help/
Share on other sites

You can use ajax to update the calendar data on the screen without posting the form values for month and year and getting the value.

 

Just keep the calendar code in another file than the web site, and use ajax to load the calendar on load of the page and also on subsequent call. I hope that you know ajax already(see jquery.com) , using ajax is easy if you use the library there.

 

Link to comment
https://forums.phpfreaks.com/topic/199463-calander-help/#findComment-1048541
Share on other sites

ScottMac,

 

Here is the code to build your drop-down boxes.

 

Hope this helps...

 

( And yes, you should take the time to learn AJAX, instead of "just hiding / showing" your calander like my quick reply does...)

 

Scot L. Diddle, Richmond VA

 


<?php


Header("Cache-control: private, no-cache");
Header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
Header("Pragma: no-cache");


$monthNames = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

$past10Years = array('2000', '2001', '2002', '2003', '2004', '2005', '2006', '2007', '2009', '2009');
$currentYear = array('2010');
$next10Years = array('2011', '2012', '2013', '2014', '2015', '2016', '2017', '2018', '2019', '2020');

$years = array_merge($past10Years, $currentYear, $next10Years);

$processAsSubmit = FALSE; // Assume the user did not hit the submit button...

$showCalander    = FALSE; // We don't yet know which calander the user wants to display...

?>

<?php

if (isset($_POST['submit'])) {

if (!isset($_REQUEST["month"])) {
	$_REQUEST["month"] = date("n");
}

if (!isset($_REQUEST["year"])) {
	$_REQUEST["year"] = date("Y");
}

$processAsSubmit = TRUE; // Looks like the user hit the submit button...
$showCalander    = TRUE; // We now know which calander the user wants to display...
}

?>

<?php
$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;
}
?>



<html>
<head>
   <title>Header</title>

<script type="text/javascript" src="ip.php "></script>

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

</head>

<body id="homepage">


<p><font size="12" face="blackadder ITC" font color ="#FFFFFF" ><Center>Leona Lewis</Center></p></font>
<p><font size="4" font color ="#FFFFFF" >Leona Lewis is an international superstar, topping the charts in 34 countries with the hit single Bleeding Love and producing 3 Number 1 hits in the UK. Leona Lewis is fast becoming one of the biggest superstars in the world.</p></font>

<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">

<center>

<?php

echo "<select id='month' name='month'> \n";
echo "<option value='Select a month' selected='selected'>Select a month</option> \n";

foreach ($monthNames as $month) {

	echo "<option value='$month'>$month</option> \n";

}

echo "</select> \n";

echo "<br /> <br /> \n";

echo "<select id='year' name='year'> \n";
echo "<option value='Select a year' selected='selected'>Select a year</option> \n";

foreach ($years as $year) {
	echo "<option value='$year'>$year</option> \n";
}

echo "</select> \n";

echo "<br /> <br /> \n";

echo "<input type='submit' id='submit' name='submit' value='Submit'> \n"


?>

</form>

<?php

if (!$showCalander) { // We don't yet know which calander the user wants to display...
	echo "<div id=\"calanderDiv\" name=\"calanderDiv\" style=\"display: none;\"> \n";
}
else { // We do...
	echo "<div id=\"calanderDiv\" name=\"calanderDiv\" style=\"display: block;\"> \n";
}

?>

<table border="0">
<tr>
<td align="center">
<table width="50%" border="0" cellpadding="2" cellspacing="2">
<tr align="center">
<td colspan="7" bgcolor="#6633CC" style="color:#FFFFFF"><strong><?php echo $monthNames[$cMonth-1].' '.$cYear; ?></strong></td>
</tr>
<tr>
<td align="center" bgcolor="#7D1B7E" style="color:#FFFFFF"><strong>S</strong></td>
<td align="center" bgcolor="#7D1B7E" style="color:#FFFFFF"><strong>M</strong></td>
<td align="center" bgcolor="#7D1B7E" style="color:#FFFFFF"><strong>T</strong></td>
<td align="center" bgcolor="#7D1B7E" style="color:#FFFFFF"><strong>W</strong></td>
<td align="center" bgcolor="#7D1B7E" style="color:#FFFFFF"><strong>T</strong></td>
<td align="center" bgcolor="#7D1B7E" style="color:#FFFFFF"><strong>F</strong></td>
<td align="center" bgcolor="#7D1B7E" style="color:#FFFFFF"><strong>S</strong></td>
</tr>


<?php
$timestamp = mktime(0,0,0,$cMonth,1,$cYear);
$maxday = date("t",$timestamp);
$thismonth = getdate ($timestamp);
$startday = $thismonth['wday'];


$newdate = date('Y-m-j',$timestamp);
$newdate = strtotime ( '-1 day' , strtotime ( $newdate ) ) ;
$newdate = date('Y-m-j',$newdate);

for ($i=0; $i<($maxday+$startday); $i++) {
if(($i % 7) == 0 ) echo "<tr>\n";
if($i < $startday) echo "<td></td>\n";
else {
$newdate = strtotime ( '+1 day' , strtotime ( $newdate ) ) ;

$newdate = date ( 'Y-m-j' , $newdate );
echo "<td align='center' valign='middle' style='height:20px'>";

echo "<a href='".$newdate."'>".($i - $startday + 1) ."</a>";

echo "</td>\n";
}
if(($i % 7) == 6 ) echo "</tr>\n";
}
?>

</table>
</td>
</tr>
</table>
</center>
</div>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/199463-calander-help/#findComment-1048550
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.