Tuesday, April 5, 2011

Download Youtube Video


Don't you sometime wonder why youtube doesn't allow you to download video ?
Here is a simple utility which will allow you to download youtube video of varing quality and format.

http://happy-igniter.co.cc/lab/grab-youtube-video/index.php

And you can download code for this utility here
http://happy-igniter.co.cc/lab/grab-youtube-video/grab-youtube-video.7z

Cheers

Tuesday, March 29, 2011

Facebook like human readable time stamp using php function

Want to share php function which results in grammatically correct facebook like human reable time format.
   
    example:
    echo get_time_ago(strtotime('now'));
   
    Result:
    less than 1 minute ago

function get_time_ago($time_stamp)
    {
        $time_difference = strtotime('now') - $time_stamp;

        if ($time_difference >= 60 * 60 * 24 * 365.242199)
        {
            /*
             * 60 seconds/minute * 60 minutes/hour * 24 hours/day * 365.242199 days/year
             * This means that the time difference is 1 year or more
             */
            return get_time_ago_string($time_stamp, 60 * 60 * 24 * 365.242199, 'year');
        }
        elseif ($time_difference >= 60 * 60 * 24 * 30.4368499)
        {
            /*
             * 60 seconds/minute * 60 minutes/hour * 24 hours/day * 30.4368499 days/month
             * This means that the time difference is 1 month or more
             */
            return get_time_ago_string($time_stamp, 60 * 60 * 24 * 30.4368499, 'month');
        }
        elseif ($time_difference >= 60 * 60 * 24 * 7)
        {
            /*
             * 60 seconds/minute * 60 minutes/hour * 24 hours/day * 7 days/week
             * This means that the time difference is 1 week or more
             */
            return get_time_ago_string($time_stamp, 60 * 60 * 24 * 7, 'week');
        }
        elseif ($time_difference >= 60 * 60 * 24)
        {
            /*
             * 60 seconds/minute * 60 minutes/hour * 24 hours/day
             * This means that the time difference is 1 day or more
             */
            return get_time_ago_string($time_stamp, 60 * 60 * 24, 'day');
        }
        elseif ($time_difference >= 60 * 60)
        {
            /*
             * 60 seconds/minute * 60 minutes/hour
             * This means that the time difference is 1 hour or more
             */
            return get_time_ago_string($time_stamp, 60 * 60, 'hour');
        }
        else
        {
            /*
             * 60 seconds/minute
             * This means that the time difference is a matter of minutes
             */
            return get_time_ago_string($time_stamp, 60, 'minute');
        }
    }

    function get_time_ago_string($time_stamp, $divisor, $time_unit)
    {
        $time_difference = strtotime("now") - $time_stamp;
        $time_units      = floor($time_difference / $divisor);

        settype($time_units, 'string');

        if ($time_units === '0')
        {
            return 'less than 1 ' . $time_unit . ' ago';
        }
        elseif ($time_units === '1')
        {
            return '1 ' . $time_unit . ' ago';
        }
        else
        {
            /*
             * More than "1" $time_unit. This is the "plural" message.
             */
            // TODO: This pluralizes the time unit, which is done by adding "s" at the end; this will not work for i18n!
            return $time_units . ' ' . $time_unit . 's ago';
        }
    }

Thursday, March 24, 2011

Float vs Inline-Block, with IE work around





Let's say you want to display images using css, which can be done using simple html tags <ul> <li>

and wrap them around using css's float properties.
.float-gallery li{
  list-style-type:none;
  border: 1px solid #CCCCCC;
  float:left;
  height: 100%;
  margin: 5px 15px;
  padding:10px;
}


Something interesting happens if images are of uneven height/width like here in this example:


But that's easy to fix all u now have to do is use display-inline. CSS property with so much potential only problem is it's not supported by IE 7 or IE 6. Here is the css with solution which work across all IE.

.inline-gallery li{
  list-style-type:none;
  border: 1px solid #CCCCCC;
  width: auto;
  min-height: inherit;
  display: inline-block;
  vertical-align: top;
  margin: 5px 15px;
  padding:10px;
  /* IE 7 hack to make it behave like inline-block */
  zoom: 1;
  *display: inline;
  /* IE 6 hack to fix height */
  _height: auto;
}

Monday, March 21, 2011

data grid table for codeigniter

I wonder, why can't we have something simpler like this in codeigniter and get the data grid listing display done ?

$config = array(
 'query' => $this->db->get_where('table', array($condition))
);

echo $data_grid->render(($config));


Normally query object should have all the information needed for table grid display.
Why can't we just use it and come up with a table grid listing ?

Fed up of implementing table list for each data listing display page or having to implement javascript base tables I decide to do write some experimental library code for it. Not sure if this is good or right way to do it. All suggestion are welcome you can email me at aman.tuladhar[at]gmail.com
Just a prototype code for moment. Using ocular library at moment to test idea and implementation. My ultimate goal would be to have one liner configuration code and make it independent of any other 3rd party codeigniter libraries.

DEMO

you can download code at https://github.com/amant/ca_gridview


A Simple Example:

class Test extends CI_Controller
{
    ...
  
    function example()
    {  
        $grid_params = array(
      
            'set_query' => $this->app_model->country(),

            'set_pagination' => array(
                'enable' => true,
                'initialize' => array('base_url' => site_url('example/example_1'), 'per_page'=>25, 'num_links' => 5),
                'order_field' => 'country_id',
                'order_type' => 'ASC',
                'offset' => 0
            ),

            'set_column' => array(
          
                'country_id' => array(
                    'title' => 'Country-ID',
                    'type' => array('text'),
                ),
              
                'region_id' => array(
                    'title' => 'Region-ID',
                    'type' => array('text'),
                ),
              
                'country_name' => array(
                    'title' => 'Country',
                    'type' => array('text'),              
                ),
              
                'country_3_code' => array(
                    'title' => 'CODE 3',
                    'type' => array('text'),
                ),
              
                'country_2_code' => array(
                    'title' => 'CODE 2',
                    'type' => array('text'),
                )
            )
        );

        $this->ca_gridview->render_grid($grid_params);
  
        $this->ocular->render();  
    }
}

And in View page.


<h1>Here is data listing</h1>
<?php echo render_partial('/ca_gridview') ?>
Page Renders in {elapsed_time} seconds

Saturday, March 19, 2011

First blog, my message Live to relax

The highest level of inner calm comes from the development of love and compassion. The more concerned we are with the happiness of others, the more we increase our own well-being. Friendliness and warmth towards others allow us to relax and help us to dispel any sense of fear or insecurity so we can overcome whatever obstacles we face.

Many famous thinkers got their bright ideas while they were relaxing and not thinking about the problem. Archimedes got his sudden flash of genius while taking a bath and Darwin figured out evolution while driving down the road. These flashes of insight happen because the unconscious mind continues to process information in the background while you are doing other things. If your subconscious figures something out, it will seem like the idea came out of nowhere.

So Relax. Don't take things so seriously. For as Norman Cousins wrote, "Death is not the greatest loss in life. The greatest loss is what dies inside us while we live."