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

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>

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.

PHP Calendar Issues

I have the following code that I created to generate a Calendar, but it has some issues:
//Labels
$dayLabels = array("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday");
$monthLables = array("January","February","March","April","May","June","July","August","September","October","November","December");
//max values
$maxDays = 7;
$maxMonths = 12;
//stats
$forceMonth = $_GET['m'];
$forceYear = $_GET['y'];
$todayDate = date("d-m-Y");
$todayDate = date("d-m-Y", strtotime($todayDate));
$explodeToday = explode("-", $todayDate);
$currentDay = $explodeToday[0];
if(isset($forceMonth)) {
$currentMonth = $forceMonth;
} else {
$currentMonth = $explodeToday[1];
};
if(isset($forceYear)) {
$currentYear = $forceYear;
} else {
$currentYear = $explodeToday[2];
};
$daysInMonth = cal_days_in_month(CAL_GREGORIAN, $currentMonth, $currentYear);
//database values
$startDate = array("01-06-2015","25-06-2015");
$endDate = array("05-06-2015","05-07-2015");
$bookedUser = array("Dexter","James");
//counters
$daysIntoMonth = 0;
$dayCounter = 0;
//debug
echo '<p>Current Month: ' .$monthLables[$currentMonth-1]. ' / ' .$currentMonth. '</p>';
echo '<p>Current Year: ' .$currentYear. '</p>';
//start of Calendar
echo '<table>';
//print days of week
echo '<tr>';
foreach($dayLabels as $day) {
echo '<td style="border-bottom:dashed 1px #DDD;">' .$day. '</td>';
};
echo '</tr>';
while($daysIntoMonth < $daysInMonth) {
//days into month
$daysIntoMonth++;
$temp_inMonth = sprintf("%02d", $daysIntoMonth);
$daysIntoMonth = $temp_inMonth;
//days into week
$dayCounter++;
$temp_dayCounter = sprintf("%02d", $dayCounter);
$dayCounter = $temp_dayCounter;
//current calendar date
$calDate = date('d-m-Y', strtotime($daysIntoMonth. '-' .$currentMonth. '-' .$currentYear));
$timeCal = strtotime($calDate);
if($dayCounter == 1) {
echo '<tr>';
};
if($startKey = array_search($calDate, $startDate) !== FALSE) {
$booked = true;
};
if($endKey = array_search($calDate, $endDate) !== FALSE) {
$booked = false;
};
if($booked == true) {
echo '<td style="background-color:red;">' .$calDate. ' / ' .$daysIntoMonth. ' ' .$dayCounter. '</td>';
} else if($booked == true && array_search($calDate, $startDate) !== FALSE) {
echo '<td style="background-color:red;">' .$calDate. ' / ' .$daysIntoMonth. ' ' .$dayCounter. '</td>';
} else if($booked == false && array_search($calDate, $endDate) !== FALSE) {
echo '<td style="background-color:red;">' .$calDate. ' / ' .$daysIntoMonth. ' ' .$dayCounter. '</td>';
} else {
echo '<td>' .$calDate. ' / ' .$daysIntoMonth. ' ' .$dayCounter. '</td>';
}
if($dayCounter == $maxDays) {
echo '</tr>';
$dayCounter = 0;
};
};
//table is kill
echo '</table>';
The issues I have noticed:
Unable to put a $bookedUser for respective $startDate,$endDate.
When a booking laps over to another month, it skips all the dates until the $endDate.
All Months start on Monday, how would I go about making them start of correct days of the week.
Possible code examples to help me solve my issues would be great, thanks in advance.
Edit:
I have solved problem 3 by using the following code:
$firstDayofMonth = strtotime("01-$currentMonth-$currentYear");
$firstDayofMonth = date("D", $firstDayofMonth);
$firstDayofMonth = array_search($firstDayofMonth, $dayMiniLabels);
$firstDayofMonth = $firstDayofMonth + 1;
$startMonth = 0;
if($firstDayofMonth != 7) {
while($startMonth < $firstDayofMonth) {
echo '<td></td>';
$startMonth++;
$dayCounter++;
$temp_dayCounter = sprintf("%02d", $dayCounter);
$dayCounter = $temp_dayCounter;
};
};
For the days and months (problem 3), I would do this:
$todaysNumber = date('w');
$currentDayInText = $dayLabels[$todaysNumber];
And the same for the monhts.
Mostly, in my MySQL-tables, dates are placed like 2015-06-05 and not in European time notation. Maybe that could solve problem 1?

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