Tuesday, June 25, 2013

Simple PHP Pagination

Download this file
https://docs.google.com/file/d/0B-WSEGv0VCtJYXRRQkRIVlFpNEE/edit?usp=sharing

Make this as pagination.php and include to your php file

 <?php include("pagination.php");
            echo pagination(20, $page, 'mypage.php?page=', $count);
?>
In mypage.php you should use the sql query according to pagination
<?php
$query="select * from table "
if (isset($_GET['page'])) {
        $start = ($_GET['page'] - 1) * 20;
       $query.=" limit $start,20";
     

    } else {
$query.=" limit 20";
     
    }
$result=mysql_fetch_array(mysql_query($query));
?>

Download any file with php using header

You can download any file using this
Step1:Copy the code to a page called download.php(note:dont need any html tags should only contains this code)
Step2:Create a link with your file name
<a href="download.php?filename=myfile.jpg">Download</a> in your page(not at download page)


<?php
 //BUILD THE FILE INFORMATION
     $file = $_REQUEST['filename'];

     //CREATE/OUTPUT THE HEADER
     header("Content-type: application/force-download");
     header("Content-Transfer-Encoding: Binary");
     header("Content-length: ".filesize($file));
     header("Content-disposition: attachment; filename=\"".basename($file)."\"");
     readfile($file);
?>

Find total time using mysql query when you have two datetime fields

SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(TIMEDIFF(date_out,date_in)))) as total FROM mytable


Also you can find this using php

function sum_the_time($time1, $time2) {
  $times = array($time1, $time2);
  $seconds = 0;
  foreach ($times as $time)
  {
    list($hour,$minute,$second) = explode(':', $time);
    $seconds += $hour*3600;
    $seconds += $minute*60;
    $seconds += $second;
  }
  $hours = floor($seconds/3600);
  $seconds -= $hours*3600;
  $minutes  = floor($seconds/60);
  $seconds -= $minutes*60;
  // return "{$hours}:{$minutes}:{$seconds}";
  return sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds);
}

function diff_the_time($time1, $time2) {
 $seconds=strtotime($time1)-strtotime($time2);
  $hours = floor($seconds/3600);
  $seconds -= $hours*3600;
  $minutes  = floor($seconds/60);
  $seconds -= $minutes*60;
  // return "{$hours}:{$minutes}:{$seconds}";
  return sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds);
}