Advertisement
sebbu

for vatipa

May 5th, 2012 (edited)
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.23 KB | None | 0 0
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <!-- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> -->
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <meta http-equiv="Content-type" content="text/html;charset=UTF-8"/>
  6. <title>Time Sheet</title>
  7. </head>
  8. <body>
  9.  
  10. <div><?php
  11. $date_format='d/m/Y, H:i:s';//CAN be changed
  12. $db=mysql_connect('localhost','sebbu','********') or die('access denied !</div></body></html>');//MUST be changed
  13. mysql_select_db('sebbu',$db) or die('couldn&#039;t select database !</div></body></html>');//MUST be changed
  14. function print_duration($time) {
  15.     assert(is_numeric($time));
  16.     $time+=0;
  17.     $str='';
  18.     $seconds=(int)($time%60);
  19.     $minutes=(int)(($time/60)%60);
  20.     $hours=(int)(($time/3600)%24);
  21.     $days=(int)($time/86400);
  22.     if($days>0) $str.=$days.'d';
  23.     if($days>0||$hours>0) $str.=($hours<10?'0'.$hours:$hours).'h';
  24.     if($days>0||$hours>0||$minutes>0) $str.=($minutes<10?'0'.$minutes:$minutes).'m';
  25.     if($days>0||$hours>0||$minutes>0||$seconds>0) $str.=($seconds<10?'0'.$seconds:$seconds).'s';
  26.     return $str;
  27. }
  28. if(array_key_exists('action', $_REQUEST)&&array_key_exists('eid', $_REQUEST)) {
  29.     $action=$_REQUEST['action'];
  30.     $eid=$_REQUEST['eid'];
  31.     assert(is_numeric($_REQUEST['eid'])) or die('eid should be numeric !</div></body></html>');
  32.     $eid+=0;
  33.     if($action=='start') {
  34.         $res=mysql_query('SELECT count(*) FROM `times` WHERE `eid`='.$eid.' AND `end_time` IS NULL ORDER BY `start_time`', $db);
  35.         $nb=mysql_fetch_row($res);
  36.         $nb=$nb[0];
  37.         @assert($nb==0) or die('You can&#039;t start the counter without stopping it first !</div></body></html>');
  38.         $res=mysql_query('INSERT INTO `times` VALUES('.$eid.', '.time().', NULL)', $db);
  39.         @assert($res==1) or die('ERROR : iNSERT INTO failed !</div></body></html>');
  40.         @assert(mysql_affected_rows($db)==1) or die('ERROR : INSERT INTO insered wrong number of lines !</div></body></html>');
  41.         echo 'Counter successfully started !<br/>'."\r\n";
  42.         echo '<a href="javascript:history.go(-1);">Go back</a>.'."\r\n";
  43.     }
  44.     elseif($action=='stop') {
  45.         $res=mysql_query('SELECT count(*) FROM `times` WHERE `eid`='.$eid.' AND `end_time` IS NULL ORDER BY `start_time`', $db);
  46.         $nb=mysql_fetch_row($res);
  47.         $nb=$nb[0];
  48.         @assert($nb==1) or die('You can&#039;t stop the counter without starting it first !</div></body></html>');
  49.         $res=mysql_query('UPDATE `times` SET `end_time`='.time().' WHERE `eid`='.$eid.' AND `end_time` IS NULL', $db);
  50.         @assert($res==1) or die('ERROR : UPDATE failed !</div></body></html>');
  51.         @assert(mysql_affected_rows($db)==1) or die('ERROR : UPDATE updated wrong number of lines : '.mysql_affected_rows($db).'!</div></body></html>');
  52.         echo 'Counter successfully stopped !<br/>'."\r\n";
  53.         echo '<a href="javascript:history.go(-1);">Go back</a>.'."\r\n";
  54.     }
  55.     else {
  56.         echo 'Action should be start or stop.';
  57.     }
  58. }
  59. else if(array_key_exists('eid', $_REQUEST)) {
  60.     $eid=$_REQUEST['eid'];
  61.     assert(is_numeric($_REQUEST['eid'])) or die('eid should be numeric !</div></body></html>');
  62.     $eid+=0;
  63.     echo '<a href="'.$_SERVER['PHP_SELF'].'?eid='.$eid.'&action=start">start</a><br/>'."\r\n";
  64.     echo '<a href="'.$_SERVER['PHP_SELF'].'?eid='.$eid.'&action=stop">stop</a><br/>'."\r\n";
  65.     $res=mysql_query('SELECT `start_time`, `end_time` FROM `times` WHERE `eid`='.$eid.' ORDER BY `start_time`', $db);
  66.     $data=array();
  67.     while(($line=mysql_fetch_row($res))!==FALSE) {
  68.         $data[]=$line;
  69.     }
  70.     //check only one NULL
  71.     $nulls=0;
  72.     foreach($data as $line) {
  73.         if($line[1]===NULL) ++$nulls;
  74.     }
  75.     @assert($nulls<=1) or die ('only one NULL allowed !</div></body></html>');
  76.     //show times
  77.     echo '<table border="1">'."\r\n";
  78.     if(count($data)!=0) {
  79.         echo "\t".'<thead>'."\r\n";
  80.         echo "\t\t".'<tr>'."\r\n";
  81.         echo "\t\t\t".'<th>start time</th>'."\r\n";
  82.         echo "\t\t\t".'<th>end time</th>'."\r\n";
  83.         echo "\t\t".'</tr>'."\r\n";
  84.         echo "\t".'</thead>'."\r\n";
  85.     }
  86.     echo "\t".'<tbody>'."\r\n";
  87.     foreach($data as $line) {
  88.         echo "\t\t".'<tr>'."\r\n";
  89.         echo "\t\t\t".'<td>'.date($date_format, $line[0]).'</td>'."\r\n";
  90.         if($line[1]===NULL) {
  91.             echo "\t\t\t".'<td><i>still counting</i></td>'."\r\n";
  92.         }
  93.         else {
  94.             echo "\t\t\t".'<td>'.date($date_format, $line[1]).'</td>'."\r\n";
  95.         }
  96.         echo "\t\t".'</tr>'."\r\n";
  97.     }
  98.     if(count($data)==0) {
  99.         echo "\t\t".'<tr>'."\r\n";
  100.         echo "\t\t\t".'<td>No times yet</td>'."\r\n";
  101.         echo "\t\t".'</tr>'."\r\n";
  102.     }
  103.     echo "\t".'</tbody>'."\r\n";
  104.     echo '</table>'."\r\n";
  105.     //show total
  106.     $total=0;
  107.     foreach($data as $line) {
  108.         if($line[1]!==NULL) {
  109.             assert($line[1]>$line[0]) or die('end_time should be greater than start_time !</div></body></html>');
  110.             $total+=($line[1]-$line[0]);
  111.         }
  112.         else {
  113.             $total+=(time()-$line[0]);
  114.         }
  115.     }
  116.     if(count($data)>0) {
  117.         echo 'Total time is : '.print_duration($total);
  118.         if($nulls>0) echo ' and counting';
  119.         echo '.';
  120.     }
  121. }
  122. else if(array_key_exists('action', $_REQUEST)) {
  123.     $action=$_REQUEST['action'];
  124.     if($action=='add') {
  125.         echo '<form action="'.$_SERVER['PHP_SELF'].'?action=add2" method="POST">'."\r\n";
  126.         echo 'Employee name :<br/>'."\r\n";
  127.         echo '<input type="text" name="name"/><br/>'."\r\n";
  128.         echo '<br/><input type="submit" value="Submit"/>'."\r\n";
  129.         echo '<br/><input type="reset" value="RESET"/>'."\r\n";
  130.         echo '</form>'."\r\n";
  131.     }
  132.     elseif($action='add2') {
  133.         if(array_key_exists('name', $_REQUEST)) {
  134.             $name=mysql_real_escape_string($_REQUEST['name']);
  135.             $res=mysql_query('INSERT INTO `employee` (`name`) VALUES("'.$name.'")');
  136.             @assert($res==1) or die('ERROR : iNSERT INTO failed !</div></body></html>');
  137.             @assert(mysql_affected_rows($db)==1) or die('ERROR : INSERT INTO insered wrong number of lines !</div></body></html>');
  138.             echo 'Employee successfully added !<br/>'."\r\n";
  139.             echo '<a href="javascript:history.go(-2);">Go back</a>.'."\r\n";
  140.         }
  141.         else {
  142.             echo 'ERROR : name is missing !'."\r\n";
  143.         }
  144.     }
  145. }
  146. else {
  147.     $res=mysql_query('SELECT `eid`, `name` FROM `employee` ORDER BY `name`', $db);
  148.     while(($line=mysql_fetch_row($res))!==FALSE) {
  149.         echo '<a href="'.$_SERVER['PHP_SELF'].'?eid='.$line[0].'">'.$line[1].'</a><br/>';
  150.     }
  151.     echo '<br/><a href="'.$_SERVER['PHP_SELF'].'?action=add">Add new employee</a>';
  152. }
  153. mysql_close($db);
  154. ?></div>
  155.  
  156. </body>
  157. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement