Insert photos into calendar - php

Im trying to create a calendar in php and want the selected month to display a picture (e.g. one for january, another for february and so on). I want the pictures to be extracted from a folder called 'months'. How do I do that?
This is my code:
<?php
class Calendar
{
var $events;
function Calendar($date)
{
if(empty($date)) $date = time();
define('NUM_OF_DAYS', date('t',$date));
define('CURRENT_DAY', date('j',$date));
define('CURRENT_MONTH_A', date('F',$date));
define('CURRENT_MONTH_N', date('n',$date));
define('CURRENT_YEAR', date('Y',$date));
define('START_DAY', (int) date('N', mktime(0,0,0,CURRENT_MONTH_N,1, CURRENT_YEAR)) - 1);
define('COLUMNS', 7);
define('PREV_MONTH', $this->prev_month());
define('NEXT_MONTH', $this->next_month());
$this->events = array();
}
function prev_month()
{
return mktime(0,0,0,
(CURRENT_MONTH_N == 1 ? 12 : CURRENT_MONTH_N - 1),
(checkdate((CURRENT_MONTH_N == 1 ? 12 : CURRENT_MONTH_N - 1), CURRENT_DAY, (CURRENT_MONTH_N == 1 ? CURRENT_YEAR - 1 : CURRENT_YEAR)) ? CURRENT_DAY : 1),
(CURRENT_MONTH_N == 1 ? CURRENT_YEAR - 1 : CURRENT_YEAR));
}
function next_month()
{
return mktime(0,0,0,
(CURRENT_MONTH_N == 12 ? 1 : CURRENT_MONTH_N + 1),
(checkdate((CURRENT_MONTH_N == 12 ? 1 : CURRENT_MONTH_N + 1) , CURRENT_DAY ,(CURRENT_MONTH_N == 12 ? CURRENT_YEAR + 1 : CURRENT_YEAR)) ? CURRENT_DAY : 1),
(CURRENT_MONTH_N == 12 ? CURRENT_YEAR + 1 : CURRENT_YEAR));
}
function getEvent($timestamp)
{
$event = NULL;
if(array_key_exists($timestamp, $this->events))
$event = $this->events[$timestamp];
return $event;
}
function addEvent($event, $day = CURRENT_DAY, $month = CURRENT_MONTH_N, $year = CURRENT_YEAR)
{
$timestamp = mktime(0, 0, 0, $month, $day, $year);
if(array_key_exists($timestamp, $this->events))
array_push($this->events[$timestamp], $event);
else
$this->events[$timestamp] = array($event);
}
function makeEvents()
{
if($events = $this->getEvent(mktime(0, 0, 0, CURRENT_MONTH_N, CURRENT_DAY, CURRENT_YEAR)))
foreach($events as $event) echo $event.'<br />';
}
function makeCalendar()
{
echo '<table border="1" cellspacing="4"><tr>';
echo '<td colspan="7" style="text-align:center"> <img src= \"img/months/$month.jpg\" > </td>';
echo '</tr><tr>';
echo '<td width="30"><<</td>';
echo '<td colspan="5" style="text-align:center">'.CURRENT_MONTH_A .' - '. CURRENT_YEAR.'</td>';
echo '<td width="30">>></td>';
echo '</tr><tr>';
echo '<td width="30">Mon</td>';
echo '<td width="30">Tue</td>';
echo '<td width="30">Wed</td>';
echo '<td width="30">Thu</td>';
echo '<td width="30">Fri</td>';
echo '<td width="30">Sat</td>';
echo '<td width="30">Sun</td>';
echo '</tr><tr>';
echo str_repeat('<td> </td>', START_DAY);
$rows = 1;
for($i = 1; $i <= NUM_OF_DAYS; $i++)
{
if($i == CURRENT_DAY)
echo '<td style="background-color: #C0C0C0"><strong>'.$i.'</strong></td>';
else if($event = $this->getEvent(mktime(0, 0, 0, CURRENT_MONTH_N, $i, CURRENT_YEAR)))
echo '<td style="background-color: #99CCFF">'.$i.'</td>';
else
echo '<td>'.$i.'</td>';
if((($i + START_DAY) % COLUMNS) == 0 && $i != NUM_OF_DAYS)
{
echo '</tr><tr>';
$rows++;
}
}
echo str_repeat('<td> </td>', (COLUMNS * $rows) - (NUM_OF_DAYS + START_DAY)).'</tr></table>';
}
}
$epcMonthPic = date("m", mktime(0,0,0,$mo)); //change the "F" for "m" if you want to use numbers
$epcImagePath = "/img/"; // the path to your monthly images (keep the trailing slash)
$epcImageExt = "jpg"; // the extension you'll be using for your images
echo "<img src=\"$epcImagePath$epcMonthPic.$epcImageExt\">";
$month = $_REQUEST["month"];
$cal = new Calendar($_GET['date']);
$cal->makeCalendar();
?>

In the method makeCalendar() fix line:
echo '<td colspan="7" style="text-align:center"> <img src= \"img/months/$month.jpg\" > </td>';
with:
echo '<td colspan="7" style="text-align:center"> <img src="img/months/' . CURRENT_MONTH_N . '.jpg" > </td>';
and make sure that images exists:
img/months/1.jpg
img/months/2.jpg
...
img/months/12.jpg

Related

How to get number of if block execute count

I am working with attendance management system in php. I try to create report for employee.in case some person late(LP) come between 15 min and 30 min it should be recorded as late present and if mod(Late present count/3) = 0 should be record as Short leave(SL) how do this task ? Other states are recorded very fine.
if($row['Time_in'] > $earliest_arrival_time && $row['Time_in'] <= $shift_start_time){
$table .='<td border border-dark>' . 'P' . '</td>';
}elseif($row['Time_in'] > $shift_start_time && $row['Time_in'] <= $shift_start_time_new){
$table .='<td border border-dark>' . 'P' . '</td>';
}elseif ($row['Time_in'] > $shift_start_time_new && $row['Time_in'] <= $shift_start_time_new02 ){
$table .='<td border border-dark>' . 'LP' . '</td>';//need the check above mention logic in here
}elseif ($row['Time_in'] > $shift_start_time_new02 && $row['Time_in'] <= $lastest_arrival_time){
$table .='<td border border-dark>' . 'SL' . '</td>';
}
how do get the LP printed time?and how to apply the logic?
$row['Time_in'] = 08:50:00 (this change time to time)
$earliest_arrival_time =03:00:00
$shift_start_time = 08:30:00
$shift_start_time_new = 08:45:00($shift_start_time + 15 min)
$shift_start_time_new02 = 09:00:00($shift_start_time + 30 min)
$lastest_arrival_time = 12:15:00
$count = 0;
foreach($rows as $row){
if(!empty($row['time_in'])){
if($row['time_in'] <= $shift_start_time) {
echo 'present';
} else if($row['time_in'] > $shift_start_time && $row['time_in'] >= $shift_start_time_new && $row['time_in'] <= $shift_start_time_new02 ) {
$count++;
if($count%3 == 0){
echo 'short leave'
} else {
echo 'late present';
}
} else {
echo 'very late';
}
} else {
echo 'absent';
}
}
I do not understand this variable $earliest_arrival_time

Highlight td elements with events

I am generating a calendar via PHP date() function, I also have an events table that has a name and date_start, what I want to do is to highlight the td elements that have events on them and leave the ones without with the basic formatting.
Basically, this is the script that loops the day:
<?php
echo "<tr>";
for ($i = 1; $i < $numDays+1; $i++, $counter++) {
$timeStamp = strtotime ("$year-$month-$i");
if($i == 1){
$firstDay = date ("w", $timeStamp);
for ($j = 0; $j < $firstDay; $j++, $counter++){
//blank space
echo "<td> </td>";
}
}
if($counter % 7 == 0 ){
echo "<tr></tr>";
}
$todayDate = date("Y-m-d");
if (date("Y-m-d", $timeStamp) == $todayDate) {
?>
<td class="today-date"><?php echo $i; ?></td>
<?php
} else {
?>
<td><?php echo $i; ?></td>
<?php
}
}
echo "</tr>";
?>
.today-date is a class to highlight the current day. This is my table columns:
id | name | description | date_start | date_end | time | pastor_id | pastor | category | venue
I would probably put my events into an array with a key for the start date, so you end up with something like:
$events = [
'2017-08-22' => [
'name' => 'Event Title',
],
'2017-08-28' => [
'name' => 'Event Title',
'name' => 'Event Title',
],
];
There are multiple ways of doing this from a DB. Something like this:
$events = [];
while($row = mysql_fetch_assoc($eventQuery))
{
$events[$row['start_date']][] = $row['name'];
}
Then in your loop, you can check the date for an event and put it in:
<td class="<?php echo (array_key_exists(date("Y-m-d", $timeStamp), $events)) ? 'has-event-class' : ''; ?>">
Making database queries for every day in the calendar will be very inefficient compared to pulling all, or even the months'.
I would also be inclined to move this into a function so it's not as long in your loop:
function highlightEventTD($timeStamp, $events)
{
$date = date("Y-m-d", $timeStamp);
return (array_key_exists($date, $events)) ? 'has-event-class' : '';
}
Then in your forloop:
<td class="<?php echo highlightEventTD($timeStamp, $events); ?>">
It also means that if you need to show the name of the event, you can use the same array and a function:
function getEvents($timestamp, $events)
{
$date = date("Y-m-d", $timeStamp);
return (array_key_exists($date, $events)) ? $events[$date] : null;
}
Ok, first of all, PHP7 doesn't like anymore open and close php tag many times... I changed syntax with echo.
So try this :
// here make a SQL query (only one query is enough)
// on your database, for example :
$dates = array();
$q = "SELECT * FROM `events`";
if($res = $pdo->query($q) {
if($res = $res->fetchAll(PDO::FETCH_OBJ)) {
if(sizeof($res) > 0) {
foreach($res as $k => $d) {
$events[$d['date_start']] = $d;
}
}
}
}
echo "<tr>";
for ($i = 1; $i < $numDays + 1; $i++, $counter++) {
$timeStamp = strtotime ("$year-$month-$i");
if($i == 1){
$firstDay = date ("w", $timeStamp);
for ($j = 0; $j < $firstDay; $j++, $counter++) {
echo "<td> </td>"; //blank space
}
}
if($counter % 7 == 0 ) {
echo "<tr></tr>";
}
if (date("Y-m-d", $timeStamp) == date("Y-m-d")) {
$class = "today-date ";
}
if (array_key_exists(date("Y-m-d"), $events)) {
$class .= "event-date ";
}
// 1. solution based on your code
echo "<td class=\"$class\">$i</td>";
// 2. alternative with events in each day :
echo "<td class=\"$class\">";
if(sizeof($eventsOfTheDay = $events[$d['date_start']]) > 0 {
foreach($eventsOfTheDay as $k => $event) {
echo '', $event['name'], '<br>';
}
}
echo "</td>";
}
echo "</tr>";

Wordpress ajax->php request

Sorry if there is theme like this, but i didn't find solution for my problem. So let's go... =)(I'm starting to learn php).
This is calendar of events and it will be widget for WP. It need to work onclick event. All code written in one file(this is plugin). In widget(in right sidebar) i write phpcode(for testing) where i call this functions.
http://herytire.esy.es/calendar
You can push '2' or '5' dates, there is data in mysql for these dates.
All works fine, but i never work with php in wordpress, and have problems with this code.
The problem is:
When i use this code in simple html page, you can see it at link above, all works fine, I get results for day that I click, BUT in wp, alert(for error), returns undefined. Two days I'm trying to fix this problem, but nothing. I hope somebody can help me please..!
**admin-ajax.php is done for view side of wp!
<script type="text/javascript"> // ajaxurl in header.php
var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";
</script>
/*
Plugin Name: Release Dates
Description: Small calendar for serials release dates.
Author: J_J
Version: 1.0
*/
function draw_calendar($month, $year) // creating calendar
{
$month = date('m');
$year = date('y');
$calendar = '<table class="calendar">';
$headings = array('S', 'M', 'T', 'W', 'T', 'F', 'S');
$calendar .= '<tr class="calendar-row"><td class="calendar-day-head">'.implode('</td><td class="calendar-day-head">', $headings).'</td></tr>';
$running_day = date('w', mktime(0, 0, 0, $month, 1, $year));
$days_in_month = date('t', mktime(0, 0, 0, $month, 1, $year));
$days_in_this_week = 1;
$day_counter = 0;
$dates_array = array();
$calendar .= '<tr class="calendar-row">';
for ($x = 0; $x < $running_day; ++$x) {
$calendar .= '<td class="calendar-day-np"> </td>';
++$days_in_this_week;
}
for ($list_day = 1; $list_day <= $days_in_month; ++$list_day) {
$calendar .= '<td class="calendar-day"><div class="day-number"><a style="cursor:pointer;" onClick="relDaySerial('.$list_day.');">'.$list_day.'</a></div></td>';
if ($running_day == 6) {
$calendar .= '</tr>';
if (($day_counter + 1) != $days_in_month) {
$calendar .= '<tr class="calendar-row">';
}
$running_day = -1;
$days_in_this_week = 0;
}
++$days_in_this_week;
++$running_day;
++$day_counter;
}
if ($days_in_this_week < 8) {
for ($x = 1; $x <= (8 - $days_in_this_week); ++$x) {
$calendar .= '<td class="calendar-day-np"> </td>';
}
}
$calendar .= '</tr>'.'</table>';
return $calendar; }
function get_serials($day) { // here i catch relDaySerial:id
if ($_POST['relDaySerial'] === null) {
$day = date('d');
} else {
$day = $_POST['relDaySerial'];
}
global $wpdb;
$result = $wpdb->get_results("SELECT * FROM tvt_calendar WHERE day=$day");
foreach ($result as $value) {
echo "
<table id='cal-data'>
<tr>
<td class='calendar-day-head'>TV Show</td>
<td class='calendar-day-head'>S</td>
<td class='calendar-day-head'>E</td>
</tr>
<tr class='cal-content'>
<td><a href='#'>".$value->title.'</a></td>
<td>'.$value->season.'</td>
<td>'.$value->series.'</td>
</tr>
</table>
';
}}
function my_action_javascript() {
?>
<script type="text/javascript" >
function relDaySerial(id) {
jQuery.ajax({
type:'POST',
url:ajaxurl,
data:{relDaySerial:id},
success:function(data) {
if(relDaySerial.type == "success") {
jQuery('#cal-bottom').html(data)
}
else {
alert()
}
}});}
</script>
<?php}
add_action('wp_footer', 'my_action_javascript');
add_action('wp_ajax_my_action_javascript', 'my_action_javascript');
add_action('wp_ajax_nopriv_my_action_javascript', 'my_action_javascript');?>
Because your alert() requires a parameter.
Try with this:
alert('An error has occurred.');

Change <td> color on condition

Hi i have this PHP which take sql query divide it into two columns and print table. Now i need to change color of TD where value > 0. I've add color change by class. It's working but not correct. It change color of whole string but not TD cell.
$stmt=ociparse($olink, $sql);
if (!$stmt) {
$e = oci_error($olink);
trigger_error(htmlentities($e['message']), E_USER_ERROR);
}
$r =ociexecute($stmt,OCI_NO_AUTO_COMMIT);
if (!$r) {
$e = oci_error($stmt); // For oci_execute errors pass the statement handle
print htmlentities($e['message']);
print "\n<pre>\n";
print htmlentities($e['sqltext']);
printf("\n%".($e['offset']+1)."s", "^");
print "\n</pre>\n";
}
$ncols = oci_num_fields($stmt);
$cur_dt = 1;
echo "<TABLE border=\"1\" width=\"100%\" align='center' cellspacing='0' cellpadding='0' class=\"sortable\">";
/* echo "\n<tr>"; */
for ($i = 1; $i <= $ncols; $i++) {
echo "<th bgcolor=\"#2B75C1\" class=\"header\">".oci_field_name($stmt, $i)."</th>";
}
for ($i = 1; $i <= $ncols; $i++) {
echo "<th bgcolor=\"#2B75C1\" class=\"header\">".oci_field_name($stmt, $i)."</th>";
}
$str=1;
while (oci_fetch($stmt)) {
if ($str % 2 == 1)
{
echo "\n";
echo "<tr";
$hrr="";
echo ">";
}
for ($i = 1; $i <= $ncols; $i++) {
echo "<td";
echo $hrr;
if (oci_result($stmt, 2) >= $cur_dt) {$hrr= " class=\"hour\"";echo $hrr;}
echo ">";
echo oci_result($stmt, $i);
}
echo "</td>";
if ($str % 2 == 0) {echo "</tr> \n";}
$str++;
}
echo "</TABLE>";
oci_close($olink);
?>
</div>
</body>
</html>
0 in second column should be blue, but it ged "hour" class and change color
create a class in your css file like .change{background-color:green} in your
<td>
for ($i = 1; $i <= $ncols; $i++) {
$hrr ="";
if (oci_result($stmt, 2) >= $cur_dt) {$hrr = 'class="change"';}
echo "<td ".$hrr.">";
echo oci_result($stmt, $i);
}
echo "</td>";
if ($str % 2 == 0) {echo "</tr> \n";}
$str++;
}
Use css to style a single cell style="background-color: #2B75C1;"
If you want to change the color of the string style="color: #2B75C1;"
echo '<th style="background-color: #2B75C1;" class=\"header\">'.oci_field_name($stmt, $i).'</th>';
Replace
echo "<td";
echo $hrr;
if (oci_result($stmt, 2) >= $cur_dt) {$hrr= " class=\"hour\"";echo $hrr;}
echo ">";
echo oci_result($stmt, $i);
}
echo "</td>";
For
echo '<td' . (oci_result($stmt, 2) >= $cur_dt ? ' class="hour"' : '') . '>';
// ^ shouldn't be there '$i'?
echo oci_result($stmt, $i);
echo '</td>'; // closing tag for <td> in optional, you can remove that to make your HTML more readable.
// remove $hrr variable, you don't need it.
I think, you have to replace the "2" with $i . Replace the following code
if (oci_result($stmt, 2) >= $cur_dt)
With the solution.
Solution 1 :
if (oci_result($stmt, $i ) >= $cur_dt)
Solution 2 :
if ( (oci_result($stmt, $i ) >= $cur_dt) || oci_result($stmt, $i ) > 0)

php code to change background colour based on date

Hi I have this code which should change the background colour if the date is 2 months near expiry but for some reason it doesn't work properly. I think it only changes if the year is smaller not the actual date. Any help is appreciated.
my code is:
$expires = date("Y/m/d",(mktime(0, 0, 0, date("m")+2, date("d"), date("Y"))));
echo ($expires."<br>");
$dateDue = date('m/d/Y');
$result = $db->prepare("SELECT * FROM tblitemdates ORDER BY name ");
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
?>
<tr class="record">
<?php
if ($row["dateDue"] == NULL)
echo '<tr style="background-color:#ffffff">';
else if ($row["dateDue"]< $expires)
echo '<tr style="background-color:#FFCC99">';
else if ($row["st1"] == NULL)
echo '<tr style="background-color:#ffffff">';
else if ($row["st1"]< $expires)
echo '<tr style="background-color:#FFCC99">';
else
echo '<tr style="background-color:#ffffff">'; ?>
regards,
B
Firstly, I find its always better to compare unix timestamps as its a much easier format to work with in terms of comparing dates to each other.
you can get this with your expires variable as follows...
$expires = date("Y/m/d",(mktime(0, 0, 0, date("m")+2, date("d"), date("Y"))));
$expiresUnix = strtotime($expires); // This should give you the unix timestamp of your formatted date.
[ RUN DB FETCH CODE ]
$dateDueUnix = strtotime($row['dateDue']);
$st1Unix = strtotime($row['st1']);
and then in your if else...
<?php
if ($dateDueUnix == NULL)
echo '<tr style="background-color:#ffffff">';
else if ($dateDueUnix< $expiresUnix)
echo '<tr style="background-color:#FFCC99">';
else if ($st1Unix == NULL)
echo '<tr style="background-color:#ffffff">';
else if ($st1Unix < $expiresUnix)
echo '<tr style="background-color:#FFCC99">';
else
echo '<tr style="background-color:#ffffff">'; ?>
Note you may need to change the == NULL to == 0 as I'm not too sure if $st1Unix will do the conversion if no argument is supplied. Hope this gets you somewhere
Remove the extra <tr class="record">
$expires = date("Y/m/d",(mktime(0, 0, 0, date("m")+2, date("d"), date("Y"))));
echo ($expires."<br>");
$dateDue = date('m/d/Y');
$result = $db->prepare("SELECT * FROM tblitemdates ORDER BY name ");
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
?>
<?php
if ($row["dateDue"] == NULL)
echo '<tr style="background-color:#ffffff">';
else if ($row["dateDue"]< $expires)
echo '<tr style="background-color:#FFCC99">';
else if ($row["st1"] == NULL)
echo '<tr style="background-color:#ffffff">';
else if ($row["st1"]< $expires)
echo '<tr style="background-color:#FFCC99">';
else
echo '<tr style="background-color:#ffffff">'; ?>

Categories