Search This Blog

Sunday, November 17, 2013

Out put a result of a MySQL query into a CSV file

Out put a result of a MySQL query into a CSV file

SELECT  * FROM m_table INTO OUTFILE '/tmp/orders.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';

Tuesday, September 17, 2013

Public Key encryption in PHP

I created the keys with openssl using:
generate a 1024 bit rsa private key, ask for a passphrase to encrypt it and save to file.

openssl genrsa -des3 -out /path/to/privatekey 1024

generate the public key for the private key and save to file
openssl rsa -in /path/to/privatekey -pubout -out /path/to/publickey







Friday, June 1, 2012

Sort a multi-dimensional array in PHP


$preparedReport = array(

 0=>array('name'=>'Alf','revenue'=>1000),
 1=>array('name'=>'Boor','revenue'=>3000),
 2=>array('name'=>'Cat','revenue'=>4000),
);

usort($preparedReport, 'sortByRevenueOrder');

// use $this in object context 
//usort($preparedReport, array($this, 'sortByRevenueOrder'));

function sortByRevenueOrder($a, $b) {

        if ($a['revenue'] == $b['revenue']) {
            return 0;
        }
        
            return ($a['revenue'] > $b['revenue']) ? 1 : -1; // ascending order
        
         //   return ($a['revenue'] < $b['revenue']) ? 1 : -1; descending order 
       
}

Tuesday, March 27, 2012

Making a mysql back up every day midnight using cron

This is a simple way to back up your database everyday Go to ubuntu command line and log in as root user, type following command to open crontab file

 >crontab -e

 type the following command in the file

 0 0 * * * mysqldump -uYOURUSER -pYOURPASSWORD YOURDBNAME > /home/tommy/my_back_up/mydb_`date +\%y-\%m-\%d`.sql

 you can restart cron by typing

 >restart cron

 OR

 >/etc/init.d/cron restart

This will create a sql dump file everyday midnight
file name would be mydb_2012-04-21.sql

Wednesday, March 7, 2012


/**
     * Converts minutes to hours
     * @param type $mins
     * @return string 
     */
    public static function m2h($mins) {
        if ($mins < 0) {
            $min = Abs($mins);
        } else {
            $min = $mins;
        }
        $H = Floor($min / 60);
        $M = ($min - ($H * 60)) / 100;
        $hours = $H + $M;
        if ($mins < 0) {
            $hours = $hours * (-1);
        }
        $expl = explode(".", $hours);
        $H = $expl[0];
        if (empty($expl[1])) {
            $expl[1] = 00;
        }
        $M = $expl[1];
        if (strlen($M) < 2) {
            $M = $M . 0;
        }
        $hours = $H . "." . $M;
        return $hours;
    }

Tuesday, March 6, 2012

PHP function to return an array representation of calender month

If you need an array representation of a calender month this function will come in handy. This function will return an array of a given month with days properly distributed in to weeks, as in real calendar.

public function buildMonthCalendar($year, $month) {

        $calendar = array(
            'week-1' => array('Mon' => null, 'Tue' => null, 'Wed' => null, 'Thu' => null, 'Fri' => null, 'Sat' => null, 'Sun' => null),
            'week-2' => array('Mon' => null, 'Tue' => null, 'Wed' => null, 'Thu' => null, 'Fri' => null, 'Sat' => null, 'Sun' => null),
            'week-3' => array('Mon' => null, 'Tue' => null, 'Wed' => null, 'Thu' => null, 'Fri' => null, 'Sat' => null, 'Sun' => null),
            'week-4' => array('Mon' => null, 'Tue' => null, 'Wed' => null, 'Thu' => null, 'Fri' => null, 'Sat' => null, 'Sun' => null),
            'week-5' => array('Mon' => null, 'Tue' => null, 'Wed' => null, 'Thu' => null, 'Fri' => null, 'Sat' => null, 'Sun' => null),
            'week-6' => array('Mon' => null, 'Tue' => null, 'Wed' => null, 'Thu' => null, 'Fri' => null, 'Sat' => null, 'Sun' => null),
        );

        $startOfMonth = "{$year}-{$month}-01";
        $result = strtotime("{$year}-{$month}-01");
        $endOfMonth =  date('Y-m-d', strtotime( date('Y-m-d', strtotime( $result . '+1 month')).' -1 second'));

        $j = 1;
        for ($i = $startOfMonth; $i != $endOfMonth;) {

            $weekDay = date('D', strtotime($i));
            $calendar["week-{$j}"][$weekDay] = date('d', strtotime($i));
            if ($weekDay == 'Sun') {
                $j++; //jump to next week
            }
            $i = date('Y-m-d', strtotime($i . ' +1 day'));
        }

        $weekDay = date('D', strtotime($endOfMonth)); //last day of the month
        $calendar["week-{$j}"][$weekDay] = date('d', strtotime($endOfMonth));

        return $calendar;
    }


Change color of table column using jquery

If the table id is punchData and if you are changing second column color
$("#punchData > tbody > tr > td:nth-child(2)").css("background","#F5D0A9");

Tuesday, February 21, 2012

list all cron jobs for all users

for user in $(cut -f1 -d: /etc/passwd); do echo $user; crontab -u $user -l; done

Thursday, January 12, 2012

Select a lable using "for" in JQuery

Jquery selector for the label of a checkbox $("label[for=genTaxRateOrder]").

Difference of self and static in inheritance, php example


class A {
    public static function get_A() {
        return new self();
    }
    public static function get_me() {
        return new static();
    }
}

class B extends A {

}

echo get_class(B::get_A());  // out put A
echo get_class(B::get_me()); // out put B
echo get_class(A::get_me()); // out put A

Wednesday, January 4, 2012

Zend Framework db examine SQL query Or Print SQL query

$db->getProfiler()->setEnabled(true);
$db->update($data, array('id = ?' => $Posts->id));
print $db->getProfiler()->getLastQueryProfile()->getQuery();
print_r($db->getProfiler()->getLastQueryProfile()->getQueryParams());
$db->getProfiler()->setEnabled(false);

Thursday, December 8, 2011

How to do a “git export” From Command Line


git archive

git archive master | tar -x -C /somewhere/else

If you want a compressed archive .

git archive master | bzip2 >source-tree.tar.bz2

ZIP archive

git archive --format zip --output /full/path/to/zipfile.zip master

Thursday, December 1, 2011

Setting Up Name Based Virtual Hosting In Ubuntu

1. Append the following line to your /etc/apache2/apache2.conf .
     NameVirtualHost 127.0.0.2:80
2 . Create unique files for each of my domains within the /etc/apache2/sites-available/ folder.Create a fie called mysitename.com

<VirtualHost 127.0.0.2:80>
ServerName 
mysitename.com
ServerAlias www.
mysitename.com
ServerAdmin me@ubuntu.com
DocumentRoot /var/www/mysitename/html
</VirtualHost>


3. Enable it by creating a symbolic link from one folder to the next ( etc/apache2/sites-enabled/)

sudo a2ensite mysitename.com ( to diable it anytime sudo a2dissite  mysitename.com)  

harder way to create the symbolic link 

cd /etc/apache2/sites-enabled/
ln -s ../sites-available/
mysitename.com .


4. Editing the /etc/hosts by adding the following line

127.0.0.2:80 mysitename.com 

5. Restart apache sudo /etc/init.d/apache2 restart

****

To enable mod_rewrite in Ubuntu, you just need to write this command in terminal

sudo a2enmod rewrite

Monday, November 28, 2011

Add And Update JSON Array Using Jquery

    

Tuesday, November 8, 2011

Validate numbers in JavaScript - IsNumeric()

A simple function to find if a positive number
function isNumber(n) {
  return !isNaN(parseFloat(n)) && isFinite(n);
}

Saturday, October 29, 2011

How to create virtual host In XAMPP in windows

1.Create a folder in C:\xampp\htdocs\mysite
2. Edit your hosts file in windows  located in C:\WINDOWS\system32\drivers\etc\ and add following line
127.0.0.1 mysite.localhost
Don’t delete the existing “127.0.0.1 localhost” line
3.Open your C:\xampp\apache\conf\extra\httpd-vhosts.conf file and add the following lines

NameVirtualHost *:80
<VirtualHost *:80>
    DocumentRoot "C:\xampp\htdocs"
    ServerName localhost
</VirtualHost>


<VirtualHost *:80>
    ServerName mysite.localhost
    DocumentRoot C:\xampp\htdocs\mysite
    <DirectoryC:\xampp\htdocs\mysite>
        DirectoryIndex index.php
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>
4. Reboot your computer .You should now be able to access each dev domain by way of:
http://mysite.localhost/

Wednesday, October 12, 2011

Get phpunit coverage report for a single class

If you want to get the phpunit coverage report for a single class you can use --filter setting with the --coverage option.
phpunit --coverage-html ./report --filter InsuranceStatusEligibilityFilterTest PluginAllTests.php

Wednesday, October 5, 2011

How to remove or edit external svn repositories


svn gedit svn:externals ./

Gedit is the editor that you will see the external repository information, do the nessasary change and save to change the edit external svn repositories

Sunday, September 18, 2011

JQuery Check if element is checked and make it checked

if($('#cbxEmployeeFamily_'+planId).is(':checked')){
             
             $('#cbxEmployeeFamily_'+planId).attr('checked', true);
         }

Wednesday, July 20, 2011

Easy way to get days of current quarter in php



Pass the frequency and current date to get an array of dates.

eg.
getDatesForYear(4,'2011-01-01');
the array you get will have the following dates
"2011-04-30","2011-08-31","2011-12-31"




public function getDatesForYear($monthlyFequency,$date){

$dateParts = explode("-",$date);

 $firstDayOfTheYear = $dateParts[0] . '-01-01';

 $lastDayOfTheYear = $dateParts[0] . '-12-31';
 
 $i = $firstDayOfTheYear;

for(strtotime($i);strtotime($i)<=strtotime($lastDayOfTheYear);) {
 
 $accruDate[] = date('Y-m-d',strtotime($i . " +$monthlyFequency months  -1 day ")) ;
 $i =  date('Y-m-d',strtotime($i . " +$monthlyFequency months"));
 } return $accruDate;
 }