Search This Blog

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);