hello to you all !
this is my first post here, and I am developing my first PHP/mysql project. So please be indulgent
I have been trying to adapt a script found in Learning PHP5 from OReilly, for displaying a monthly calendar, with 2 comboboxes and a Display button, which allow to select & display a monthly calendar by setting a month and a year.
i added the code that displays a checkbox for every day in the month, so that the user can select multiple dates, then i would like to save these dates to a database.
my code for detecting the checked days is not working.
if someone would have a look and give a suggestion or better a solution to my problem, i would be extremely grateful !
the line that generates the checkbox is :
input_radiocheck('checkbox','mycheckbox[]', $day, $day);
the code that tries to detect the checked boxes is in the process_form() function
<?php
// Use the form helper functions defined in Chapter 6
require 'formhelpers.php';
$months = array(1 => 'January', 2 => 'February', 3 => 'March', 4 => 'April',
5 => 'May', 6 => 'June', 7 => 'July', 8 => 'August',
9 => 'September', 10 => 'October', 11 => 'November',
12 => 'December');
$years = array();
for ($year = date('Y') - 1, $max_year = date('Y') + 5; $year < $max_year; $year++) {
$years[$year] = $year;
}
if ($_POST['_submit_check']) {
if ($errors = validate_form()) {
show_form($errors);
} else {
show_form();
process_form();
}
} else {
// When nothing is submitted, show the form and then
// a calendar for the current month
show_form();
show_calendar(date('n'), date('Y'));
}
function validate_form() {
global $months, $years;
$errors = array();
if (! array_key_exists($_POST['month'], $months)) {
$errors[] = 'Select a valid month.';
}
if (! array_key_exists($_POST['year'], $years)) {
$errors[] = 'Select a valid year.';
}
return $errors;
}
function show_form($errors = '') {
global $months, $years, $this_year;
// If the form is submitted, get defaults from submitted variables
if ($_POST['_submit_check']) {
$defaults = $_POST;
} else {
// Otherwise, set our own defaults: the current month and year
$defaults = array('year' => date('Y'),
'month' => date('n'));
}
if ($errors) {
print 'You need to correct the following errors: <ul><li>';
print implode('</li><li>',$errors);
print '</li></ul>';
}
print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
input_select('month', $defaults, $months);
input_select('year', $defaults, $years);
input_submit('Afficher','Afficher');
input_submit('Enregistrer','Enregistrer');
print '<input type="hidden" name="_submit_check" value="1"/>';
print '</form>';
}
function process_form() {
// si on a clicke sur afficher
if (isset($_POST['Afficher'])) {
show_calendar($_POST['month'], $_POST['year']);
}
//sinon, verifier si des dates ont ete selectionnees
//si oui, enregistrer dans la DB
//sinon, rien faire
if (isset($_POST['Enregistrer'])) {
foreach($_POST['mycheckbox'] as $value){
echo 'test';
echo "<BR> You clicked checkbox number " .$value, "\n";
}
}
}
function show_calendar($month, $year) {
global $months;
$weekdays = array('Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa');
// Find the epoch timestamp for midnight on the first day of the month
$first_day = mktime(0,0,0,$month, 1, $year);
// How many days are in the month?
$days_in_month = date('t', $first_day);
// What day of the week (numerically) is the first day of the month?
// You need this to put the first table cell in the right place
$day_offset = date('w', $first_day);
// Print the table header and the row of weekday names
print<<<_HTML_
<table border="0" cellspacing="0" cellpadding="2">
<tr><th colspan="7">$months[$month] $year</th></tr>
<tr><td align="center">
_HTML_;
print implode('</td><td align="center">', $weekdays);
print '</td></tr>';
// If the first day of the month is, say, a Tuesday, then you
// need to put blank table cells under "Su" and "Mo" in the first
// row so that the day 1 table cell goes under "Tu"
if ($day_offset > 0) {
for ($i = 0; $i < $day_offset; $i++) { print '<td> </td>'; }
}
// Print a table cell for each day of the month
for ($day = 1; $day <= $days_in_month; $day++ ) {
print '<td align="center">' . $day ;
input_radiocheck('checkbox','mycheckbox[]', $mycheckbox, $day);
print '</td>';
$day_offset++;
// If this cell was the seventh in the row, then
// end the table row and reset $day_offset
if ($day_offset == 7) {
$day_offset = 0;
print "</tr>\n";
// If there are more days to come, then
// start a new table row
if ($day < $days_in_month) {
print '<tr>';
}
}
}
// At this point, one table cell has been printed for each day
// of the month. If the last day of the month isn't a Saturday
// then the last row of the table needs to be padded with
// some blank cells out to the end of the row
if ($day_offset > 0) {
for ($i = $day_offset; $i < 7; $i++) {
print '<td> </td>';
}
print '</tr>';
}
print '</table>';
}
?>