Monday, 7 July 2014

php round off value to multiples of 10

PHP Round Off values to multiples of 10

public function trimprice($currency, $x=5, $sf=2) {
$scale = pow(10,$sf);
$temp = round($currency * $scale, $sf); // get rid of small rounding errors
return number_format(round(ceil($temp / $x), $sf) * $x / $scale, $sf);

Thursday, 12 June 2014

how to import gmail contacts using php

PHP Import Contacts From Gmail:

You can import all gmails contacts through php and its working in localhost also.


$clientid = 'client id';
$clientsecret = 'client secret key';
$redirecturi = 'http://localhost/gmail/result.php'; 
$maxresults = 50; // Number of mailid you want to display.


<?php require_once('config.php'); ?>

<a href="<?php print $clientid;?>&redirect_uri=<?php print $redirecturi; ?>&scope=">Invite Friends From Gmail</a>


ini_set('display_errors', '1');
echo '<pre>';
$authcode = $_GET["code"];
$post = array(
'code'=> urlencode($authcode),
'client_id'=> urlencode($clientid),
'client_secret'=> urlencode($clientsecret),
'redirect_uri'=> urlencode($redirecturi),
'grant_type'=> urlencode('authorization_code')

$query_string = '';
foreach($post as $key=>$value){ 
$query_string .= $key.'='.$value.'&'; 
$query_string = rtrim($query_string,'&');

$ch = curl_init();//open connection
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$result = json_decode($result);

$accesstoken = $result->access_token;
if(!empty($accesstoken)) {
$_SESSION['token']= $accesstoken;
$contacts = file_get_contents(''.$maxresults.'&oauth_token='. $_SESSION['token']);
$xml= new SimpleXMLElement($contacts);
$xml->registerXPathNamespace('gd', '');
$result = $xml->xpath('//gd:email');
$count = 0;
foreach ($result as $title) {
echo $count.". ".$title->attributes()->address . "<br><br>";


Tuesday, 10 September 2013

How to use has_many in rails view

How to use has_many in rails view

Here is the index_controller

  def index
    @categories = Category.all

Here is the two models

1. Category model

has_many :sub_categories

2. sub_category model

belongs_to :category

And your view should contain

<% @categories.each do |category| %>
    <h1><%= category.title %></h1>
    <% category.sub_categories.each do |sub_category| %>
        <%= sub_category.descriptino %>
    <% end %>
<% end %>

Tuesday, 3 September 2013

phppgadmin configuration for rails

phpPgAdmin Configuration for rails

1. cd /etc/postgresql/9.1/main
2. sudo nano pg_hba.conf
3. Change the line from
    local        all        postgres               peer


    local        all        postgres               md5

4.  save and exit
5. Then open sudo nano postgresql.conf
6. Change the listen address as listen_address = '*'

7. Now try localhost/phppgadmin 

Sunday, 1 September 2013

get current week of the month and year

Get current week of the month and year

Get current week of the current year

select extract(week from date_trunc('month', current_date))

Get current week of the current month

select extract(week from current_date) - extract(week from date_trunc('month',current_date))+1

get last month last and first date

To get last month's last and first date

SELECT (date_trunc('month', now()-interval '6 month')::date - 1) as last_month_date

SELECT (date_trunc('month', now()-interval '6 month')::date ) as first_month_date 

The above query can be used to get last month's last date from current month

Thursday, 30 May 2013

Android view output in logcat

How to view android output in logcat:

Log.i("ref_name", String.valueOf(jArray.length()) );