So I am making a calender form for a client to choose a course to book… this then uploads to a link table between the course and the customer but I cant for the life of me get the calender into a calender like grid properly… my code is simple and sweet but i need to do this non object orientated (i have small java experience but nothing else)….
I have ben staring at this all day but to no avail!
(I am learning so im not going down the routes of protecting it etc… this is for self learning so just needs to work and help me understand where I am going wrong)
The code:
<?
if(@$_POST['month'])
{
$_SESSION['month'] = trim($_POST['month']);
}
elseif(!isset($_SESSION['month']))
{
$_SESSION['month'] = '3';
}
if(@$_POST['year'])
{
$_SESSION['year'] = trim($_POST['year']);
}
elseif(!isset($_SESSION['year']))
{
$_SESSION['year'] = '2012';
}
?>
<form name="sort" action="calender.php" method="post">
<select name="month">
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<select name="year">
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
<option value="2017">2017</option>
<option value="2018">2018</option>
<option value="2019">2019</option>
<option value="2020">2020</option>
<option value="2021">2021</option>
<option value="2022">2022</option>
<option value="2023">2023</option>
</select>
<input type="submit" value=" - !Go! - " />
</form><br />
<?php
$month = $_SESSION['month'];
$year = $_SESSION['year'];
$day = 1;
$nummonth = cal_days_in_month(CAL_GREGORIAN, $month, $year);
echo "<form name='choosedate' action='datechosen' method='get'>";
for($day = 1; $day <= $nummonth; $day++)
{
echo $day."/".$month."/".$year."<input type='radio' name='coursedate value='".$day."-".$month."-".$year."' /><br />";
}
echo "<input type='submit' value=' - Submit - ' /></form>";
?>
Thank you for any help you can give… any advice or even just a point in the right direction is much much much appreciated!
Here’s a function I threw together that should work with your current code. To use the function, simply call it by this:
Then you need to include the code below. I’ve commented in it to help.
The main concept is as DaveyBoy states, it’s just a X-by-X array. The only real trick is that days don’t always start on the first day of the week, so you need to print out some “blank” days of the previous month.
You could easily do this by creating a 7-by-X 2D array and just loop through that, but it will be the same concepts where you need to fill in the blank spaces.
Of course, there’s some hard coded values in there, which can be improved upon but for the sake of this tutorial, here it is.
Hope it helps!
Cheers!