Saturday, March 29, 2014

Php Array Pagination Script

///////////////FILLING ARRAY WITH DUMMY DATA////////////////////
$key = array();
for($i=0; $i<200; $i++)
{
 //fill array data
 $key[] = "num = ".$i;
}
////////////////////////////////////////////////////////////////

/////////////////////START OF ARRAY PAGINATION CODE/////////////////////
$ptemp="http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
$pt=explode('&',$ptemp);
if (strpos($ptemp,'pageno'))
 array_pop($pt);
$pt=implode('&',$pt);
$ptemp=$pt;
$array=$key; // REPLACE $KEY WITH YOUR ARRAY VARIABLE
$page = $_REQUEST['pageno'];

$currentpage = isset($page) ? (integer)$page : 1;
$numperpage = 10; //NUMBER OF RECORDS TO BE DISPLAYED PER PAGE

$total = count($array);
$numofpages = ceil($total / $numperpage); //TOTAL NUMBER OF PAGES

if(isset($array))
{
    if (($currentpage > 0) && ($currentpages <= $numofpages))
 {
        //STARTING LOOP FOR ARRAY DATA
        $start = ($currentpage-1) * $numperpage;
        for($i=$start;$i<=($numperpage+$start-1);$i++) 
  {
            ///////////PLACE YOUR CODE HERE//////////////////////////
            echo $array[$i] .'
';
   ////////////////////////////////////////////////////////
        }
 }
}
if ($currentpage != 1) 
{ //GOING BACK FROM PAGE 1 SHOULD NOT BET ALLOWED
 $previous_page = $currentpage - 1;
 $previous = ' < ';    
}    
$pages = '';
for ($a=1; $a<=$numofpages; $a++)
{
  if ($a == $currentpage) 
 $pages .= $a .' ';
  else 
 $pages .= ''. $a .' ';
}
$pages = substr($pages,0,-1); //REMOVING THE LAST COMMA (,)

if ($currentpage != $numofpages) 
{ //GOING AHEAD OF LAST PAGE SHOULD NOT BE ALLOWED
 $next_page = $currentpage + 1;
 $next = '  >';
}
echo '

'. $previous . $pages . $next; //PAGINATION LINKS
/////////////////////END OF ARRAY PAGINATION CODE/////////////////////

Monday, March 24, 2014

How to calculate difference in days between two dates in MySQL

Let's say we have a MySQL table where one column (date or datetime type) is namedactivated and contains dates in one of the YYYY-MM-DD or YYYY-MM-DD HH:MM:SSformats from the past and we need to calculate the difference in days since that date untill current date for each row.

First let's see a preview of the activated column:
mysql> SELECT activated FROM table_dates LIMIT 0,5;
+--------------+
| activated     |
+--------------+
| 2007-06-06 |
| 2007-10-15 |
| 2007-10-17 |
| 2007-10-18 |
| 2007-10-19 |
+--------------+
5 rows in set (0.00 sec)
mysql>
so we see dates lik 06th of June, 15th of October and so on. Now, let's calculate the difference between these dates and current date:

mysql> SELECT DATEDIFF(CURDATE(), activated) AS intval FROM table_dates LIMIT 0,5;
+--------+
| intval   |
+--------+
|    259  |
|    128  |
|    126  |
|    125  |
|    124  |
+--------+
5 rows in set (0.00 sec)
so the difference in days between 6th of June and present day is 259 days. Same with the others.
I used in the queries above two MySQL date functions: DATEDIFF() and CURDATE().

How to backup and download Database using PHP

< ?php $mysqlUserName = 'databaseusername' ; $mysqlPassword = 'databasepassword' ; $mysqlHostNa...