How to get number of if block execute count - php

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

Related

Displaying retrieve data in different table

Okay, what I want is to display retrieved data in a table form database, I want my table to be limit in 5 data per table. I need the tables to be display horizontally.
Like this:
while($row1 = sqlFetchArray($row))
{ $ctr = 0;
$ctr+1;
if($ctr=1 || $ctr<=5)
{
$html .='<table width="100px" style="float:left;"><tr><td>'.$row1['id'].'</td></tr></table>';
}
if($ctr=6 || $ctr<=10)
{
$html .='<table width="110px" style="float:left;"><tr><td>'.$row1['id'].'</td></tr></table>';
}
if($ctr=11 || $ctr<=15)
{
$html .='<table width="120px" style="float:left;"><tr><td>'.$row1['id'].'</td></tr></table>';
}
}
Output:
1 6
2 7
3 8
4 9
5 10
This should work for you:
(But the function sqlFetchArray as to work like mysql_fetch_array)
<?php
$limit = 15;
for($numberCounter = 0; $numberCounter < $numberCount = mysql_num_rows($row); $numberCounter++) {
$count = 0;
if($numberCounter >= mysql_num_rows($row))
break;
if ($count == 0 || $count % $limit == 0)
echo "<table width='100px' style='float:left;'>";
while ($row1 = sqlFetchArray($row) && $count < $limit) {
if($numberCounter >= mysql_num_rows($row))
break;
echo "<tr><td>" . $row1[$numberCounter] . "</td></tr>";
$count++;
$numberCounter++;
}
if($count == 0 || $count % $limit == 0)
echo "</table>";
}
?>
As an example:
<?php
$test = range(1, 43);
$limit = 15;
for($numberCounter = 0; $numberCounter < $numberCount = count($test); $numberCounter++) {
$count = 0;
if($numberCounter >= count($test))
break;
if ($count == 0 || $count % $limit == 0)
echo "<table width='100px' style='float:left;'>";
while ($count < $limit) {
if($numberCounter >= count($test))
break;
echo "<tr><td>" . $test[$numberCounter] . "</td></tr>";
$count++;
$numberCounter++;
}
if($count == 0 || $count % $limit == 0)
echo "</table>";
}
?>

Check for multiple rows - PHP & MySQL

I am cycling through cells in a HTML table using PHP to color specific cells.
I want to color the cells that return true to this statement:
if ($currentCell >= $reservationStartDayOfMonth && $currentCell <= $reservationEndDayOfMonth) {
make red
} else {
make normal color
}
This is not the exact statement, but the idea of it.
I get the reservationEndDayOfMonth and reservationStartDayOfMonth from a MySQL server.
For each row in the SQL server, there is a new reservation with a start and an end date. Along with the id (1...2...3...)
Basically I got a reservation in the current month. And it works when I just cycle through the cells with the data from ONE reservation. But I got lots of reservations in my database. I need to check and see if the current cell corresponds to any of the many reservations I have.
My code is long and bulky. It's basically just a table with all the checkers and parameters inside. Here you go:
<table border="1" id="calendar">
<?php
$dk_database = array("Januar","Februar","Marts","April","Maj","Juni","Juli","August","September","Oktober","November","December");
$dk_month = $dk_database[(int)date("m")-1];
ECHO '<caption><h1 style="color:#554100;">' . $dk_month . '</h1></caption>';
$sqldateStart = sqlRequest("SELECT res_datestart FROM spottrup_reservations WHERE res_id = 1");
$dateStartRaw = mysql_fetch_array($sqldateStart);
$dateStartArray = explode("-", $dateStartRaw[0]);
$dateStartDay = (int)$dateStartArray[2];
$dateStartMonth = (int)$dateStartArray[1];
$dateStartYear = (int)$dateStartArray[0];
$sqldateEnd = sqlRequest("SELECT res_dateend FROM spottrup_reservations WHERE res_id = 1");
$dateEndRaw = mysql_fetch_array($sqldateEnd);
$dateEndArray = explode("-", $dateEndRaw[0]);
$dateEndDay = (int)$dateEndArray[2];
$dateEndMonth = (int)$dateEndArray[1];
$dateEndYear = (int)$dateEndArray[0];
$currentYear = (int)date("y")+2000;
$currentDate = (int)date("d");
$currentMonth = (int)date("m");
$columnsInRow = 7;
$daysInMonth = date("t");
$currentDay = 1;
for ($i=0 ; $i < $daysInMonth ; $i++) {
ECHO '<tr class="cal">';
for ($j=0 ; $j < $columnsInRow && $currentDay <= $daysInMonth ; $j++) {
if($currentDate==$currentDay) {
if($currentDay>=$dateStartDay && $currentDay<=$dateEndDay && $currentMonth==$dateStartMonth && $currentYear==$dateStartYear) {
ECHO '<div style="background:blue;height:100%;width:100%text-decoration:none;"><td class="cal"><h2 style="color:red;">' . $currentDay . '</h2></td></div>';
}else{
ECHO '<div style="background:#D4BB6A;height:100%;width:100%text-decoration:none;"><td class="cal"><h2 style="color:red;">' . $currentDay . '</h2></td></div>';
}
}else{
if($currentDay>=$dateStartDay && $currentDay<=$dateEndDay && $currentMonth==$dateStartMonth && $currentYear==$dateStartYear) {
ECHO '<div style="background-color:blue;height:100%;width:100%text-decoration:none;"><td class="cal" style="background-color:#FF8080;"><h2 style="color:#554100;">' . $currentDay . '</h2></td></div>';
}else{
ECHO '<div style="background-color:#D4BB6A;height:100%;width:100%text-decoration:none;"><td class="cal"><h2 style="color:#554100;">' . $currentDay . '</h2></td></div>';
}
}
$currentDay++;
}
ECHO '</tr>';
}
?>
The sqlRequest(string); calls a function that returns the result of the query you give.
Not the most efficient way since lots of queries but it does reduce a for loop, but you could run a query for each day to see if there is a reservation for it. Simple to implement.
for ($i=0 ; $i < $daysInMonth ; $i++)
{
...
$query = "Select * from table where currentDate >= startDate and currentDate <= endDate"
...
}

Insert photos into calendar

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

360 grid -- Finish out row with odd number of records

I'm trying to accomplish this, with the 360 grid system: http://imgur.com/4ZFll
From a database i'm getting products which will be displayed in lines with 4 on each.
It's working perfectly if there is exactly 4 products under each category, but if there is less than 4 products in a category, the design is messed up, because the div's not closed properly.
Problem is that sometimes there's only 3 or less products on a line.
Is there any of you who knows how to accomplish this?
for($i=0 ; $i<$countprod ; $i++){
$prevprod = $products[$i-1]['name'];
$curprod = $products[$i]['name'];
if($curprod != $prevprod){
echo '<div class="grid_12 alpha omega"><h2>'.$products[$i]['catname'].'</h2></div>';
}
if ($i == 0){ echo '<div class="grid_3 '; }
if ($i % 4 == 0) { echo ' alpha">'; }
elseif($i % 4 == 3) { echo '</div><div class="grid_3 omega">'; }
else{ echo '</div><div class="grid_3">';
}
echo $product[$i]['image'];
if ($i % 4 == 3) {
echo '</div><div class="clear"></div>';
echo '<div class="grid_3';
}
}
(sorry about the title, i didnt know what to call this question :) )
echo '<div class="grid_3';
You aren't closing this tag.
Have a try with
$countprod = count($product);
$prevprod = '';
$close_div = false;
for ($i=0; $i<$countprod; $i++){
$curprod = $products[$i]['name'];
if($curprod != $prevprod){
if ($close_div) echo '</div>';
echo '<div class="grid_12 alpha omega"><h2>'.$products[$i]['catname'].'</h2></div>';
}
if ($i % 4 == 0) {
echo '<div class="grid_3 alpha">';
$close_div = true;
}
elseif ($i % 4 == 3) {
echo '</div><div class="grid_3 omega">';
$close_div = true;
}
else {
echo '</div><div class="grid_3">';
$close_div = true;
}
echo $product[$i]['image'];
if ($i % 4 == 3) {
echo '</div><div class="clear"></div>';
$close_div = false;
}
$prevprod = $curprod;
}
$p = 10; // Current number of products
$ppr = 4; // Products per row
$x = $i % $ppr;
if($x != 0){
$countprod = $p + ($ppr - $x);
}
echo $countprod; // 12 (4 * 3)
Loop with FOR if there is no product just print empty DIV, if that's what you have asked...

PHP & MySQL pagination display problem

I asked a similar question like this yesterday but after waiting for ever I figured out part of the problem but now I'm stuck again I'm trying to display ... when the search results are to long because my pagination links will keep on displaying and will not stop until every link is displayed on the page.
For example I'm trying to achieve the following in the example below. Can some one help me fix my code so I can update my site. Thanks
This is what I want to be able to do.
First Previous 1 2 ... 5 6 7 8 9 10 11 12 13 ... 199 200 Next Last
Here is my pagination code that displays the links.
$display = 20;
if (isset($_GET['p']) && is_numeric($_GET['p'])) {
$pages = $_GET['p'];
} else {
$q = "SELECT COUNT(id) FROM comments WHERE user_id=3";
$r = mysqli_query ($mysqli, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($mysqli));
$row = mysqli_fetch_array ($r, MYSQLI_NUM);
$records = $row[0];
if ($records > $display) {
$pages = ceil ($records/$display);
} else {
$pages = 1;
}
}
if (isset($_GET['s']) && is_numeric($_GET['s'])) {
$start = $_GET['s'];
} else {
$start = 0;
}
//content goes here
if ($pages > 1) {
echo '<br /><p>';
$current_page = ($start/$display) + 1;
if ($current_page != 1) {
echo 'First';
}
if ($current_page != 1) {
echo 'Previous ';
}
for ($i = 1; $i <= $pages; $i++) {
if ($i != $current_page) {
echo '' . $i . ' ';
} else {
echo '<span>' . $i . '</span> ';
}
}
if ($current_page != $pages) {
echo 'Next';
}
if ($current_page != $pages) {
echo 'Last';
}
echo '</p>';
}
Instead of the loop just use something like this:
if($current_page > 8 && $pages > 11) {
echo '1 ';
echo '2 ';
echo '...';
}
for ($i = max(1, $current_page - 4); $i < min($current_page + 4, $pages); $i ++) {
echo '' . $i . ' ';
}
if ($current_page < $pages - 8 && $pages > 11) {
echo '...';
echo '' . ($pages - 1) . ' ';
echo '' . $pages . ' ';
}

Categories