Jump to content

Include Checkbox in Simple PHP Calendar Script


Gazzahbay

Recommended Posts

Hi Team

 

Using a simple PHP Calendar, but wish to add a checkbox with the value being the full date that will end up being added to MySQL DB.

 

Full Code is below (as you can see I have a WHILE inside a FOR as just a test to date) So am getting a Multiple Loop...

 

<?php

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

 

$cDay = $_REQUEST["day"];

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

}

?>

<!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=UTF-8" />

<title>Availability Calendar</title>

</head>

<body>

<table width="100%">

<tr align="center">

<td bgcolor="#999999" style="color:#FFFFFF"><table width="100%" border="0" cellspacing="0" cellpadding="0">

<tr>

<td width="50%" align="left"><a href="<?php echo $_SERVER["PHP_SELF"] . "?month=". $prev_month . "&year=" . $prev_year; ?>" style="color:#FFFFFF">Previous</a></td>

<td width="50%" align="right"><a href="<?php echo $_SERVER["PHP_SELF"] . "?month=". $next_month . "&year=" . $next_year; ?>" style="color:#FFFFFF">Next</a></td>

</tr>

</table></td>

</tr>

<tr>

<td align="center"><table width="100%" border="1" cellpadding="5" cellspacing="0">

<tr align="center">

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

</tr>

<tr>

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

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

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

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

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

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

<td align="center" bgcolor="#999999" 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'];

 

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

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

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

else echo "<td align='center' valign='middle' height='20px'>". ($i - $startday + 1) . "<br />";

 

/************** START TESTING ***************/

 

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

$endTime = mktime(0,0,0,$cMonth,$maxday,$cYear);

while($thisTime <= $endTime)

{

$thisDate = date('d-m-Y', $thisTime);

echo "<input name='selectdate' type='checkbox' value='".$thisDate."'/>";

 

$thisTime = strtotime('+1 day', $thisTime); // increment for loop

}

 

/************** END TESTING ***************/

 

 

echo "</td>";

 

 

 

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

}

 

 

?>

</body>

</html>

 

Any assistance would be much appreciated....

 

Cheers

Link to comment
Share on other sites

Could you provide a little more information as to where you're stuck? Based on a cursory look, I didn't see a <form> tag in the above code. If you want the visitor to submit a value to PHP by clicking a check box, you'll need a <form> tag...and a way to submit the form.

 

Side note: displaying the raw value of PHP_SELF to the screen makes your script vulnerable to XSS attacks. You could change lines like this

<td width="50%" align="left"><a href="<?php echo $_SERVER["PHP_SELF"] . "?month=". $prev_month . "&year=" . $prev_year; ?>" style="color:#FFFFFF">Previous</a></td>

...to this

<td width="50%" align="left"><a href="<?php echo "?month=". $prev_month . "&year=" . $prev_year; ?>" style="color:#FFFFFF">Previous</a></td>

 

Link to comment
Share on other sites

or you could do this:

/* Get the current page */
$phpSelf = filter_input(INPUT_SERVER, 'PHP_SELF', FILTER_SANITIZE_URL);

$path_parts = pathinfo($phpSelf);

$basename = $path_parts['basename']; // Use this variable for action='':
$pageName = ucfirst($path_parts['filename']);


<td width="50%" align="left"><a href="<?php echo $basename . "?month=". $prev_month . "&year=" . $prev_year; ?>" style="color:#FFFFFF">Previous</a></td>
Link to comment
Share on other sites

Thanks Guys

Yep, haven't got to the point of making it a form as yet (thats stage 2!!!)

This stage is to merely try and get the checkboxes displaying correctly with the correct full date value IE d-m-Y.

This bit here is what I'm trying to get working... (Works now, but zillions of checkboxes appear!)

echo "<input name='selectdate' type='checkbox' value='".$thisDate."'/>";

If you use the code as it stands, each calendar day has a full months worth of checkboxes in the one day! iE if Jan, 31 x 31 check boxes appear.... (WHILE inside a FOR) Like paste in say http://phpfiddle.org

Cheers

Edited by Gazzahbay
Link to comment
Share on other sites

Like if I change 

else echo "<td align='center' valign='middle' height='20px'>". ($i - $startday + 1) . "<br />";

/************** START TESTING ***************/

$thisTime = mktime(0,0,0,$cMonth,1,$cYear);
$endTime = mktime(0,0,0,$cMonth,$maxday,$cYear);
while($thisTime <= $endTime)
{
$thisDate = date('d-m-Y', $thisTime);
echo "<input name='selectdate' type='checkbox' value='".$thisDate."'/>";

$thisTime = strtotime('+1 day', $thisTime); // increment for loop
}

/************** END TESTING ***************/


echo "</td>";

TO

 else echo "<td align='center' valign='middle' height='20px'>". ($i - $startday + 1) . "<br /><input name='selectdate' type='checkbox' value='". ($i - $startday + 1) . "'/></td>";

Works fine - except value of checkbox is just the day number  EG Just "5" BUT want the value to be EG 5-1-2016 (for starters) - eventually going to MySQL Date Format (0000-00-00) once I get this stage working..

Edited by Gazzahbay
Link to comment
Share on other sites

 

 

Make the date format Y-m-d and NOT d-m-Y

 

Date format is irrelevant until I go to stage 2, which is to include more code for entering info into DB...

 

If you go to http://new.herveybayqld.com.au - you will see what I mean by "zillions of checkboxes"

 

it is supposed to look like this - http://new.herveybayqld.com.au/test.php  - But the value, say the checkbox on the 5th Feb 16, is just "5" instead 2016-02-05

 

So, am trying to change the "5" to a full date EG "2016-02-05"

 

Cheers

Edited by Gazzahbay
Link to comment
Share on other sites

I'm getting closer...

 

Changed the FOR statement to include the extra expression...  Got rid of the WHILE statement...

<?php 
$timestamp = mktime(0,0,0,$cMonth,1,$cYear);
$maxday = date("t",$timestamp);
$thismonth = getdate ($timestamp);
$startday = $thismonth['wday'];
			
$thisTime = mktime(0,0,0,$cMonth,1,$cYear);
$endTime = mktime(0,0,0,$cMonth,$maxday,$cYear);
			
for ($i=0; $i<($maxday+$startday); $i++, $thisTime <= $endTime) {
				
	if(($i % 7) == 0 ) echo "<tr>";
			
	if($i < $startday) echo "<td></td>";
			
	else echo "<td align='center' valign='middle' height='20px'>". ($i - $startday + 1) . "<br />";
		
		$thisDate = date('Y-m-d', $thisTime);
		echo "<input name='Days[]' type='checkbox' value='".$thisDate."'";
	
		if ($selectSelectDate == $thisDate){?> checked="checked" <?php } ?> />
	
	<?php
			
		$thisTime = strtotime('+1 day', $thisTime); // increment for loop			
		echo "</td>";
		if(($i % 7) == 6 ) echo "</tr>";
		}
	?>

Now what happens is that the date in value is starting from the first TD in the table, not the first TD with a date.  So if 4 empty TD's before the start of the month, then date is out by four days...

Link to comment
Share on other sites

I've got it to the point of mostly working...  A bit of CSS to hide unwanted checkboxes

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

			
			for ($i=0; $i<($maxday+$startday); $i++, $thisTime <= $endTime) {
				
			if(($i % 7) == 0 ) echo "<tr>";
			
			if($i < $startday) echo "<td  align='center' valign='middle' height='20px'> </td>";
			
			else echo "<td align='center' valign='middle' height='20px'>". ($i - $startday + 1) . "<br />";
		
			$thisDate = date('Y-m-d', $thisTime);
			
			echo "<input name='Days[]' type='checkbox' value='".$thisDate."'";
			
			if ($selectSelectDate == $thisDate){?> checked="checked" <?php } ?> />
			
			<?php
			
			
			$thisTime = strtotime('+1 day', $thisTime); // increment for loop			
			
			echo "</td>";
					
			if(($i % 7) == 6 ) echo "</tr>";
			}
			?>
Link to comment
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.