Date picking a date out of a php calender - php

We are having a problem with our PHP Calender: We want to pick a certain date, f.e. 15th of December 2016, and change the background color for a start!
Here is the code:
<!DOCTYPE HTML>
<?php
$date = time();
date_default_timezone_set("Europe/Amsterdam");
$day = date('d', $date);
$month = date('m', $date);
$year = date('y', $date);
$first_day = mktime( 0, 0, 0, $month, 1, $year);
$title = date('F', $first_day);
$day_of_week = date('D', $first_day);
switch ($day_of_week){
case "Mon": $blank = 0; break;
case "Tue": $blank = 1; break;
case "Wed": $blank = 2; break;
case "Thu": $blank = 3; break;
case "Fri": $blank = 4; break;
case "Sat": $blank = 5; break;
case "Sun": $blank = 6; break;
}
$days_in_month = cal_days_in_month(0, $month, $year);
echo "<table border=6 width=394>";
echo "<tr><th colspan=60> $title $year </th></tr>";
echo "<tr><td width=62>Monday</td><td width=62>Tuesday</td><td width=62>Wednesday</td><td width=62>Thursday</td><td width=62>Friday</td><td width=62>Saturday</td><td width=62>Sunday</td></tr>";
$day_count = 1;
echo "<tr>";
while ($blank > 0 ) {
echo "<td></td>";
$blank = $blank-1;
$day_count++;
}
$day_num = 1;
// Deze while-loop zorgt ervoor dat de tabel naar de volgende rij gaat na 7 dagen.
while ($day_num <= $days_in_month) {
echo "<td>$day_num</td>"; // if statement hier in zetten om achtergrond te veranderen
$day_num++;
$day_count++;
if ($day_count > 7){
echo "<tr></tr>";
$day_count = 1;
}
}
while ($day_count > 1 && $day_count <= 7){
echo "<td> </td>";
$day_count++;
}
echo "</tr></table>";
?>

Try this
if($day_num == '15'){$bg = "style='background-color:red'";}else{$bg="";}
if you want matching with today day
if($day_num == $day){$bg = "style='background-color:red'";}else{$bg="";}
Put it here
while ($day_num <= $days_in_month) {
if($day_num == $day){$bg = "style='background-color:red'";}else{$bg="";}
echo "<td $bg>$day_num</td>"; // if statement hier in zetten om achtergrond te veranderen
$day_num++;
$day_count++;
if ($day_count > 7){
echo "<tr></tr>";
$day_count = 1;
}
}

Related

How can I retrieve data from mysql into my eventcalendar?

I'm trying to retrieve data from my mysql database into a php calendar which I've downloaded from the web. The result from this code is that the page wont stop loading. I know it's somehow related to a loop. I want to highlight the date if there is an event saved with the same date.
db connection//
class Calendar {
public function __construct($year = '', $month = '') {
$date = time();
if (empty($year) OR empty($month)) {
$year = date('Y', $date);
$month = date('m', $date);
$day = date('d', $date);
}
$first_day = mktime(0, 0, 0, $month, 1, $year);
$title = date('F', $first_day);
$day_of_week = date('D', $first_day);
switch ($day_of_week) {
case "Mon": $blank = 0;
break;
case "Tue": $blank = 1;
break;
case "Wed": $blank = 2;
break;
case "Thu": $blank = 3;
break;
case "Fri": $blank = 4;
break;
case "Sat": $blank = 5;
break;
case "Sun": $blank = 6;
break;
}
$days_in_month = cal_days_in_month(0, $month, $year);
echo '<table width="100%" class="table table-striped">';
echo '<tr>';
echo '<th colspan=60>' . $title . ' ' . $year . '</th>';
echo '</tr>';
echo '<tr>';
echo '<td width=62>Mån</td>';
echo '<td width=62>Tis</td>';
echo '<td width=62>Ons</td>';
echo '<td width=62>Tors</td>';
echo '<td width=62>Fre</td>';
echo '<td width=62>Lör</td>';
echo '<td width=62>Sön</td>';
echo '</tr>';
$day_count = 1;
while ($blank > 0) {
echo '<td></td>';
$blank = $blank - 1;
$day_count++;
}
$day_num = 1;
$day_today = date('Y-m-d');
while ($day_num <= $days_in_month) {
$sql = "SELECT id, event_name, event_date FROM events WHERE event_date = '".$day_today."'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
if(date('d') == $event_date) {
echo "<td> <u>$day_num</u> </td>";
$day_num++;
$day_count++;
}
}
} else {
if(date('d') != $day_num) {
echo "<td> $day_num </td>";
$day_num++;
$day_count++;
}
if(date('d') == $day_num) {
echo "<td> <strong>$day_num</strong> </td>";
$day_num++;
$day_count++;
}
if ($day_count > 7) {
echo '</tr><tr>';
$day_count = 1;
}
}
}
while ($day_count > 1 && $day_count <= 7) {
echo '<td> </td>';
$day_count++;
}
echo '</tr>';
echo '</table>';
}
}
$c = new Calendar($year, $month);
There is no increase of $day_num if the condition in your loop does not apply (and that's the case because $event_date does not exists) and the script will run infinitely.
if (date('d') == $row["event_date"]) // instead of $event_date
On the other hand, this loop and this conditon seems unnecessary to me. If you only want to mark the day, a check on $result->num_rows should be enough.
if ($result->num_rows > 0)
{
echo "<td> <u>$day_num</u> </td>";
$day_num ++;
$day_count ++;
}
Regarding to your database query, you currently request always data for the same day, because $day_today is always the current day. You could try something like this instead:
$check_date = $year ."-". $month ."-". $day_num;
$sql = "SELECT id, event_name, event_date FROM events
WHERE event_date = '" .$check_date . "'";
I hope this helps you further.

How to add row values together in a html table (calendar)?

I have a calendar I made with php using an html table.
Each date has an integer value inserted from the database below the day number.
What I want to do is get the sum of each row (week) of the table and put it in the 8th column. How can I do this?
Any help?
<?php
$conn = mysqli_connect('localhost','username','password','database_name');
?>
<html>
<head>
<title>
Calendar
</title>
<link rel="stylesheet" href="/test/style.css">
<script src="/test/script.js"></script>
</head>
<body>
<?php
//This gets today's date
$date = time() ;
//This puts the day, month, and year in seperate variables
$day = date('d', $date) ;
$month = date('m', $date);
$year = date('Y', $date);
//Here we generate the first day of the month
$first_day = mktime(0,0,0,$month, 1, $year) ;
//This gets us the month name
$title = date('F', $first_day) ;
//Here we find out what day of the week the first day of the month falls on
$day_of_week = date('D', $first_day) ;
switch($day_of_week){
case "Sun": $blank = 0; break;
case "Mon": $blank = 1; break;
case "Tue": $blank = 2; break;
case "Wed": $blank = 3; break;
case "Thu": $blank = 4; break;
case "Fri": $blank = 5; break;
case "Sat": $blank = 6; break;
}
//We then determine how many days are in the current month
$days_in_month = cal_days_in_month(0, $month, $year) ;
//Here we start building the table heads
echo "<table border=1 width=294>";
echo "<tr><th colspan=7> $title $year </th></tr>";
echo "<tr><td width=42>S</td><td width=42>M</td><td width=42>T</td>
<td width=42>W</td><td width=42>T</td><td width=42>F</td><td width=42>S</td><td width=42>Total:</td></tr>";
$day_count = 1;
$row_number = 1;
echo "<tr id='row" . $row_number . "'>";
$row_number++;
//first we take care of those blank days
/////////get beginning of month
if($month-1 != 0) {
$last_month = $month-1;
}
else {
$last_month = 12;
}
if($last_month == '12') {
$year = $year-1;
}
$last_month_first_day = mktime(0,0,0,$last_month, 1, $year);
$last_month_days_in_month = cal_days_in_month(0, $last_month, $year);
$last_month_day_of_week = date('D', $last_mont_days_in_month);
$last_month_days_to_add_to_last_month_end = $blank;
$last_month_end = $last_month_days_in_month-$last_month_days_to_add_to_last_month_end;
//end ^^
while ( $blank > 0) {
echo "<td><span style='color:grey'>" . $last_month_end . "</span></td>";
$last_month_end++;
$blank = $blank-1;
$day_count++;
}
//sets the first day of the month to 1
$day_num = 1;
//count up the days, untill we've done all of them in the month
$week_total_mileage = array();
$x = 0;
while ( $day_num <= $days_in_month ) {
//get total miles from database
$getDay = $year . "-" . $month . "-" . $day_num;
$query = "SELECT * FROM table WHERE date='" . $getDay . "'";
$doQuery = mysqli_query($conn,$query);
while($rows = mysqli_fetch_assoc($doQuery)) {
$total_miles = $rows['total_miles'];
}
$num_rows = mysqli_num_rows($doQuery);
echo "<td id='" . $day_count . $row_number . "' value='" . $total_miles . "'
>
<form method='post' action='/test/day.php'>
<input type='hidden' value='" . $day_num . "' name='day'>
<input type='hidden' value='" . $title . "' name='month'>
<input type='hidden' value='" . $year . "' name='year'>
<input type='button' id='dayNum' value='" . $day_num . "'>
</form>
<span id='totalMiless'>Total miles: ";
if($num_rows == 1) {
echo $total_miles;
}
else {
echo '-';
}
echo "</span>
</td>
<div class='hiddenDay' id='" . $day_num . $title . $year . "' style='display:none'>
<span id='totalMiles'>Total miles: ";
if($num_rows == 1) {
echo $total_miles;
}
else {
echo '0';
}
echo "</span></div>";
$week_total_mileage[$day_num] = $total_miles;
$day_num++;
$day_count++;
if ($day_count > 7) {
$total_total = 0;
while($x < 8) {
$total_total = $total_total + $week_total_mileage[$x];
$x++;
}
while($x < 14 && $x > 8) {
$total_total = $total_total + $week_total_mileage[$x];
}
echo "<td>" . $total_total .
"</td></tr><tr id='row" . $row_number . "'>";
empty($week_total_mileage);
$day_count = 1;
$row_number++;
}
}
//Finaly we finish out the table with some blank details if needed
$end_days = 1;
while ( $day_count >1 && $day_count <=7 ) {
echo "<td><span id='endDays'>" . $end_days . "</span></td>";
$day_count++;
$end_days++;
}
echo "</tr></table>";
?>
</body>
</html>
The above code outputs this:
I just went through your script, put all total miles into an array
and calculate sum with
array_sum
Try this code:
<?php
$conn = mysqli_connect('localhost','username','password','database_name');
?>
<html>
<head>
<title>
Calendar
</title>
<link rel="stylesheet" href="/test/style.css">
<script src="/test/script.js"></script>
</head>
<body>
<?php
//This gets today's date
$date = time() ;
//This puts the day, month, and year in seperate variables
$day = date('d', $date) ;
$month = date('m', $date);
$year = date('Y', $date);
//Here we generate the first day of the month
$first_day = mktime(0,0,0,$month, 1, $year) ;
//This gets us the month name
$title = date('F', $first_day) ;
//Here we find out what day of the week the first day of the month falls on
$day_of_week = date('D', $first_day) ;
switch($day_of_week){
case "Sun": $blank = 0; break;
case "Mon": $blank = 1; break;
case "Tue": $blank = 2; break;
case "Wed": $blank = 3; break;
case "Thu": $blank = 4; break;
case "Fri": $blank = 5; break;
case "Sat": $blank = 6; break;
}
//We then determine how many days are in the current month
$days_in_month = cal_days_in_month(0, $month, $year) ;
//Here we start building the table heads
echo "<table border=1 width=294>";
echo "<tr><th colspan=7> $title $year </th></tr>";
echo "<tr><td width=42>S</td><td width=42>M</td><td width=42>T</td>
<td width=42>W</td><td width=42>T</td><td width=42>F</td><td width=42>S</td><td width=42>Total:</td></tr>";
$day_count = 1;
$row_number = 1;
echo "<tr id='row" . $row_number . "'>";
$row_number++;
//first we take care of those blank days
/////////get beginning of month
if($month-1 != 0) {
$last_month = $month-1;
}
else {
$last_month = 12;
}
if($last_month == '12') {
$year = $year-1;
}
$last_month_first_day = mktime(0,0,0,$last_month, 1, $year);
$last_month_days_in_month = cal_days_in_month(0, $last_month, $year);
$last_month_day_of_week = date('D', $last_mont_days_in_month);
$last_month_days_to_add_to_last_month_end = $blank;
$last_month_end = $last_month_days_in_month-$last_month_days_to_add_to_last_month_end;
//end ^^
while ( $blank > 0) {
echo "<td><span style='color:grey'>" . $last_month_end . "</span></td>";
$last_month_end++;
$blank = $blank-1;
$day_count++;
}
//sets the first day of the month to 1
$day_num = 1;
//count up the days, untill we've done all of them in the month
$week_total_mileage = array();
$weekly_total = array();
$x = 0;
while ( $day_num <= $days_in_month ) {
//get total miles from database
$getDay = $year . "-" . $month . "-" . $day_num;
$query = "SELECT * FROM table WHERE date='" . $getDay . "'";
$doQuery = mysqli_query($conn,$query);
while($rows = mysqli_fetch_assoc($doQuery)) {
$total_miles = $rows['total_miles'];
}
$num_rows = mysqli_num_rows($doQuery);
echo "<td id='" . $day_count . $row_number . "' value='" . $total_miles . "'
>
<form method='post' action='/test/day.php'>
<input type='hidden' value='" . $day_num . "' name='day'>
<input type='hidden' value='" . $title . "' name='month'>
<input type='hidden' value='" . $year . "' name='year'>
<input type='button' id='dayNum' value='" . $day_num . "'>
</form>
<span id='totalMiless'>Total miles: ";
if($num_rows == 1) {
echo $total_miles;
}
else {
echo '-';
}
echo "</span>
</td>
<div class='hiddenDay' id='" . $day_num . $title . $year . "' style='display:none'>
<span id='totalMiles'>Total miles: ";
if($num_rows == 1) {
echo $total_miles;
}
else {
echo '0';
}
echo "</span></div>";
if(is_numeric($total_miles)) {
$weekly_total[] = $total_miles;
}
$week_total_mileage[$day_num] = $total_miles;
$day_num++;
$day_count++;
if ($day_count > 7) {
$total_total = 0;
while($x < 8) {
$total_total = $total_total + $week_total_mileage[$x];
$x++;
}
while($x < 14 && $x > 8) {
$total_total = $total_total + $week_total_mileage[$x];
}
echo "<td>" . array_sum($weekly_total) .
"</td></tr><tr id='row" . $row_number . "'>";
empty($week_total_mileage);
$weekly_total = array();
$day_count = 1;
$row_number++;
}
}
//Finaly we finish out the table with some blank details if needed
$end_days = 1;
while ( $day_count >1 && $day_count <=7 ) {
echo "<td><span id='endDays'>" . $end_days . "</span></td>";
$day_count++;
$end_days++;
}
echo "</tr></table>";
?>
</body>
</html>

php calendar: adding days of week from previous month to first week

I have a calendar that I made with php. It looks like this:
The image above is of the month March. It looks fine, but when I choose August it looks like this:
(the two numbers in each box are: top one: day. bottom one: another number that i put in that doesn't mean anything)
If you notice on the right side for day one, it is not even on the calendar.
How can I fix this?
Here is my code:
<?php
$date = time() ;
$day = date('d', $date) ;
$month = date('m', $date);
$year = date('Y', $date);
$first_day = mktime(0,0,0,$month, 1, $year) ;
$title = date('F', $first_day) ;
$day_of_week = date('D', $first_day) ;
switch($day_of_week){
case "Sun": $blank = 0; break;
case "Mon": $blank = 1; break;
case "Tue": $blank = 2; break;
case "Wed": $blank = 3; break;
case "Thu": $blank = 4; break;
case "Fri": $blank = 5; break;
case "Sat": $blank = 6; break;
}
$days_in_month = cal_days_in_month(0, $month, $year) ;
echo "<table border=1 width=294>";
echo "<tr><th colspan=7> $title $year </th></tr>";
echo "<tr><td width=42>S</td><td width=42>M</td><td width=42>T</td><td width=42>W</td><td width=42>T</td><td width=42>F</td><td width=42>S</td></tr>"; //This counts the days in the week, up to 7
$day_count = 1; echo "<tr>";
if($month-1 != 0) {
$last_month = $month-1;
}
else {
$last_month = 12;
}
if($last_month == '12') {
$year = $year-1;
}
$last_month_first_day = mktime(0,0,0,$last_month, 1, $year);
$last_month_days_in_month = cal_days_in_month(0, $last_month, $year);
$last_month_day_of_week = date('D', $last_mont_days_in_month);
$last_month_days_to_add_to_last_month_end = $blank++;
$last_month_end = $last_month_days_in_month- $last_month_days_to_add_to_last_month_end;
while ( $blank > 0 ) {
echo "<td><span style='color:grey'>" . $last_month_end . "</span></td>";
$last_month_end++;
$blank = $blank-1;
$day_count++;
}
$day_num = 1;
while ( $day_num <= $days_in_month ) {
echo "<td><a href='/test/day.php?day=" . $day_num . "&month=" . $title . "&year=" . $year . "'>" . $day_num . '<br>';
echo "</a></td>";
$day_num++;
$day_count++;
if ($day_count > 7) {
echo "</tr><tr>";
$day_count = 1;
}
}
$end_days = 1;
while ( $day_count >1 && $day_count <=7 ) {
echo "<td><span style='color:grey;'>" . $end_days . "</span></td>";
$day_count++;
$end_days++;
}
echo "</tr></table>";
?>
or is there a better way to add the end of the last month to the beginning of the new calendar? and the same for the next month to the end of this calendar?
You have to change the line
$last_month_days_to_add_to_last_month_end = $blank++;
to
$last_month_days_to_add_to_last_month_end = $blank;
<?php
$date = time() ;
$day = date('d', $date) ;
$month = date('m', $date);
$year = date('Y', $date);
$first_day = mktime(0,0,0,$month, 1, $year) ;
$title = date('F', $first_day) ;
$day_of_week = date('D', $first_day) ;
switch($day_of_week){
case "Sun": $blank = 0; break;
case "Mon": $blank = 1; break;
case "Tue": $blank = 2; break;
case "Wed": $blank = 3; break;
case "Thu": $blank = 4; break;
case "Fri": $blank = 5; break;
case "Sat": $blank = 6; break;
}
if($month-1 != 0) {
$last_month = $month-1;
}
else {
$last_month = 12;
}
if($last_month == '12') {
$year = $year-1;
}
$last_month_first_day = mktime(0,0,0,$last_month, 1, $year);
$last_month_days_in_month = cal_days_in_month(0, $last_month, $year);
$last_month_day_of_week = date('D', $last_mont_days_in_month);
$last_month_days_to_add_to_last_month_end = $blank;
$last_month_end = $last_month_days_in_month- $last_month_days_to_add_to_last_month_end;
$days_in_month = cal_days_in_month(0, $month, $year) ;
echo "<table border=1 width=294>";
echo "<tr><th colspan=7> $title $year </th></tr>";
switch($blank){
case "0": echo "<tr><td width=42>S</td><td width=42>M</td><td width=42>T</td><td width=42>W</td><td width=42>T</td><td width=42>F</td><td width=42>S</td></tr>"; break;
case "1": echo "<tr><td width=42>M</td><td width=42>T</td><td width=42>W</td><td width=42>T</td><td width=42>F</td><td width=42>S</td><td width=42>S</td></tr>"; break;
case "2": echo "<tr><td width=42>T</td><td width=42>W</td><td width=42>T</td><td width=42>F</td><td width=42>S</td><td width=42>S</td><td width=42>M</td></tr>"; break;
case "3": echo "<tr><td width=42>W</td><td width=42>T</td><td width=42>F</td><td width=42>S</td><td width=42>S</td><td width=42>M</td><td width=42>T</td></tr>"; break;
case "4": echo "<tr><td width=42>T</td><td width=42>F</td><td width=42>S</td><td width=42>S</td><td width=42>M</td><td width=42>T</td><td width=42>W</td></tr>"; break;
case "5": echo "<tr><td width=42>F</td><td width=42>S</td><td width=42>S</td><td width=42>M</td><td width=42>T</td><td width=42>W</td><td width=42>T</td></tr>"; break;
case "6": echo "<tr><td width=42>S</td><td width=42>S</td><td width=42>M</td><td width=42>T</td><td width=42>W</td><td width=42>T</td><td width=42>F</td></tr>"; break;
default: echo "<tr><td width=42>S</td><td width=42>M</td><td width=42>T</td><td width=42>W</td><td width=42>T</td><td width=42>F</td></tr>";
}
//This counts the days in the week, up to 7
$day_count = 1; echo "<tr>";
$day_num = 1;
while ( $day_num <= $days_in_month ) {
echo "<td><a href='/test/day.php?day=" . $day_num . "&month=" . $title . "&year=" . $year . "'>" . $day_num . '<br>';
echo "</a></td>";
$day_num++;
$day_count++;
if ($day_count > 7) {
echo "</tr><tr>";
$day_count = 1;
}
}
$end_days = 1;
while ( $day_count >1 && $day_count <=7 ) {
echo "<td><span style='color:grey;'> - </span></td>";
$day_count++;
$end_days++;
}
echo "</tr></table>";
?>

PHP calendar with left write navigation

I'm trying to create a calendar with only PHP.
Here is what I have done:
<?pop
$date = time():
$day = date("d", $date);
$month = date("m", $date);
$year = date("y", $date);
$firstDay = mktime(0, 0, 0, $month, 1, $year);
$title = date("F", $firstDay);
$dayOfWeek = date("D", $firstDay);
switch($dayOfWeek) {
case "Sun": $blank = 0; break;
case "Mon": $blank = 1; break;
case "Tue": $blank = 2; break;
case "Wed": $blank = 3; break;
case "Thu": $blank = 4; break;
case "Fri": $blank = 5; break;
case "Sat": $blank = 6; break;
}
$daysInMonth = cal_days_in_month(0, $month, $year);
echo "<table>";
echo "<tr><th>" . $title . " " . $year "</th></tr>";
echo "<tr><td>Sun</td><td>Mon</td><td>Tue</td><td>Wed</td><td>Thu</td><td>Fri</td><td>Sat</td></tr>";
$dayCount = 1;
while ($blank > 0) {
echo "<td></td>";
$blank--;
$dayCount++;
}
$dayNum = 1;
while ($dayNum <= $daysInMonth) {
echo "<td>" . $dayNum . "</td>";
$dayNum++;
$dayCount++;
if ($dayCount > 7) {
echo "</tr><tr>";
$dayCount = 1;
}
}
while ($dayCount > 1 and $dayCount <= 7) {
echo "<td></td>";
$dayCount++;
}
echo "</tr></table>";
?>
The logic seems to be fine (at leaset to me for now). However, when I tried this on my server, I got this error message:
"; echo "" . $title . " " . $year ""; echo "SunMonTueWedThuFriSat"; $dayCount = 1; while ($blank > 0) { echo ""; $blank--; $dayCount==; } $dayNum = 1; while ($dayNum <= $daysInMonth) { echo "" . $dayNum . ""; $dayNum++; $dayCount++; if ($dayCount > 7) { echo ""; $dayCount = 1; } } while ($dayCount > 1 and $dayCount <= 7) { echo ""; $dayCount++; } echo ""; ?>
I have absolutely no idea why this happened. I would like to ask for you help on this.
Also, I wonder if I can add two arrow so the user can click on them in order to go to the next or previous months using only PHP.
Thanks
Just Check what you've written in the PHP opening tag at the begining of the file... It has to be
<?php not <?pop
Do Refer this PHP Basic Syntax which explains the basic PHP syntax

php calendar that connects to database mysql for events

So i have code for a very simple calendar, however I can't figure out how to query mysql database to find any events for each day of the month. I know how to query the database for that information, but i dont know where i would put it in the code of creating a calendar or how to structure it or anything. ive looked at many codes of php calendars and for each code i havent been able to get data from my database, i just have no idea how to do it. any help is grateful, thanks.
calendar code editted to include db stuff:
include ("connection.php");
$date =time ();
$day = date('d', $date);
$month = date('m', $date);
$month = $month + "02";
$year = date('Y', $date);
$years = substr($year, 2, 2);
$first_day = mktime(0,0,0,$month, 1, $year);
$title = date('F', $first_day);
$day_of_week = date('D', $first_day);
switch($day_of_week){
case "Sun": $blank = 0; break;
case "Mon": $blank = 1; break;
case "Tue": $blank = 2; break;
case "Wed": $blank = 3; break;
case "Thu": $blank = 4; break;
case "Fri": $blank = 5; break;
case "Sat": $blank = 6; break;
}
$days_in_month = cal_days_in_month(0, $month, $year);
echo "<table border=1 width=294>";
echo "<tr><th colspan=7> $title $year </th></tr>";
echo "<tr><td width=42>S</td><td width=42>M</td><td
width=42>T</td><td width=42>W</td><td width=42>T</td><td
width=42>F</td><td width=42>S</td></tr>";
$day_count = 1;
echo "<tr>";
while ( $blank > 0 )
{
echo "<td></td>";
$blank = $blank-1;
$day_count++;
}
$day_num = 1;
while ( $day_num <= $days_in_month )
{
echo "<td> $day_num <br/>";
$result = mysql_query("SELECT time, length FROM hire WHERE day = '$day_num' and month = '$month' and year = '$years'") or die ('Error: '.mysql_error ());
while ($row = mysql_fetch_array($result)){
$time = $row['time'];
$length = $row['length'];
}
if (isset($time) and (isset($length))) {
echo "Time: " . $time . "<br/> Length: " . $length . "<br/>";
}
"</td>";
$day_num++;
$day_count++;
if ($day_count > 7)
{
echo "</tr><tr>";
$day_count = 1;
}
}
while ( $day_count >1 && $day_count <=7 )
{
echo "<td> </td>";
$day_count++;
}
echo "</tr></table>";
You'll need to put your database code to the place where the date cells are outputted. Here's the code:
<?php
include ("connection.php");
$date =time ();
$day = date('d', $date);
$month = date('m', $date);
$month = $month + "01";
$year = date('Y', $date);
$first_day = mktime(0,0,0,$month, 1, $year);
$title = date('F', $first_day);
$day_of_week = date('D', $first_day);
switch($day_of_week){
case "Sun": $blank = 0; break;
case "Mon": $blank = 1; break;
case "Tue": $blank = 2; break;
case "Wed": $blank = 3; break;
case "Thu": $blank = 4; break;
case "Fri": $blank = 5; break;
case "Sat": $blank = 6; break;
}
$days_in_month = cal_days_in_month(0, $month, $year);
echo "<table border=1 width=294>";
echo "<tr><th colspan=7> $title $year </th></tr>";
echo "<tr><td width=42>S</td><td width=42>M</td><td width=42>T</td><td width=42>W</td><td width=42>T</td><td width=42>F</td><td width=42>S</td></tr>";
$day_count = 1;
echo "<tr>";
while ( $blank > 0 )
{
echo "<td></td>";
$blank = $blank-1;
$day_count++;
}
$day_num = 1;
while ( $day_num <= $days_in_month )
{
//database code here
$result = ''; //formatted html result
echo "<td> $day_num $result</td>";
$day_num++;
$day_count++;
if ($day_count > 7)
{
echo "</tr><tr>";
$day_count = 1;
}
}
while ( $day_count >1 && $day_count <=7 )
{
echo "<td> </td>";
$day_count++;
}
echo "</tr></table>";

Categories