[PostgreSQL] Take backup of the individual table in postgres

pg_dump -t <table-name> <database-name> > /tmp/file_name.dump
Continue Reading

[Javascript]: Unload Event upon closing the page

 <script>
          window.addEventListener("beforeunload", function (e) {
            var confirmationMessage = "\o/";

            (e || window.event).returnValue = confirmationMessage;     //Gecko + IE
            return confirmationMessage;                                //Webkit, Safari, Chrome etc.
          });
        </script>
Continue Reading

LINUX: Shortkut keys to use Konsole

Activate Menu-> Ctrl+Shift+F10
Add Bookmark -> Ctrl_Shift+B
Clear Scrollbak and Reset -> Ctrl+Shift+X
Close Active Tab -> Ctrl+Shift+S
Close Other Tabs -> Ctrl+Shift+O
Close Current Tab -> Ctrl+Shift+W
Copy -> Ctrl+Shift+C
Detach Current Tab -> Ctrl+Shift+H
Enlarge Font -> Ctrl++
Expand View -> Ctrl+Shift+]
Find Next Search-> F3
Find Previous Search -> Shift+F3
Find -> Ctrl+Shift+F
Monitor for Activity -> Cttl+Shift+A
Monitor for Silence -> Ctrl+Shift+I
Move to Left Tab -> Ctrl+Shift+Left
Move to Right Tab -> Ctrl+Shift+Right
Open New Tab -> Ctrl+Shift+T
Open New Window -> Ctrl+Shift+N
Move to Next Tab -> Shift+Right
Next View Container -> Shift+Tab
None -> Ctrl+Shift+/
Paste -> Ctrl+Shift+V or Shift+Ins
Paste Selection -> Ctrl+Shift+Ins
Previous TAb -> Shift+Left
Quit -> Ctrl+Shift+Q
Rename Tab -> Ctrl+Alt+S



Continue Reading

PostgreSQL: Handling the sequence value in psql(Reset sequence/ Get sequence next value)

PostgreSQL: Reset the sequence value of the table

Example1

select setval('students_id_seq',1706);

Result

  setval
--------
   1706
(1 row)

===============================

Example2(Recommanded)

To reset the sequence exactly to the recent primarykey id. This is the best idea to reset the sequence by avoiding usual conflicts.

select setval('students_id_seq',(select max(id) from students)) 

Result

  setval
--------
   1706
(1 row)

 

===============================

PostgreSQL: Get the next sequence value of the table

select nextval('students_id_seq');

Result

 nextval
---------
    1707
(1 row)

Continue Reading

RUBY: Easy to create first controller in ruby on rails

Create new controller in ruby on rails is the easy thing. Just follow the following process

[root]# script/generate controller <controller-name>

Sample
script/generate controller registration

this will genereate the following folders/files.

      exists  app/controllers/
      exists  app/helpers/
      create  app/views/registration
      exists  test/functional/
      create  app/controllers/registration_controller.rb
      create  test/functional/registration_controller_test.rb
      create  app/helpers/registration_helper.rb
Continue Reading

RUBY: slice() - ruby on rails

Slicing the string or characters from the given start position with the given length of characters.

Look at the qucik example:

        dob = '10/13/1986'
        dob_mm = dob.slice(0,2)  #this returns 10
        dob_dd = dob.slice(2,2) #this returns 13
        dob_yy = dob.slice(4,4) #this returns 1986
Continue Reading

RUBY: Creating dynamic directory in ruby

system()  method executes any commands that we can able to execute through terminal or konsole(console).

e.g
folder = "/tmp"
system("mkdir #{folder}") if !File.directory?(folder)

If you found any errors in the above coding , try to include the ftools library.

e.g
require 'ftools'
folder = "/tmp"
system("mkdir #{folder}") if !File.directory?(folder)

Continue Reading

Write a file oin ruby on rails


log_path = '/home/project/logs'
system("mkdir #{log_path}") if !File.directory?(log_path)
log_content = "test content"
File.open("#{log_path}/log_#{Time.now.to_i}.txt", 'a') { |file|
  file.write "#{log_content}"
}
Continue Reading

RUBY: Ruby on Rails - string replace

gsub

gsub() is the always very useful function in ruby.

Syntax:
string.gsub("<search char>","<replace Char>")
        string.gsub(/'/,"") #replce/remove single quotes from the string


You should be careful that the string variable should not have a nil value. Otherwise it will rise a nil exception. 


Continue Reading

Ruby On Rails - Getting Started


Ruby on Rails, often shortened to Rails, is an open source full-stack web application framework for the Ruby programming language. Ruby on Rails runs on the general-purpose programming language Ruby, which predates it by more than a decade. Rails is a full-stack framework, meaning that it gives the web developer the ability to create pages and applications that gather information from the web server, talk to or query the database, and render templates out of the box. As a result, Rails features a routing system that is independent of the web server.
Continue Reading

Add two Time values in php

function addTime($a, $b)
{
   $sec=$min=$hr=0;
   $a = explode(':',$a);
   $b = explode(':',$b);

   if(($a[2]+$b[2])>= 60)
   {
     $sec=($a[2]+$b[2]) % 60;
     $min+=1;

   }
   else
   $sec=($a[2]+$b[2]);

   if(($a[1]+$b[1]+$min)>= 60)
   {
      $min=($a[1]+$b[1]+$min) % 60;
     $hr+=1;
   }
   else
    $min=$a[1]+$b[1]+$min;
    $hr=$a[0]+$b[0]+$hr;

   $added_time=$hr.":".$min.":".$sec;
   return $added_time;
}
Continue Reading

PHP: Get full web page URL from address bar in php



There are two functions in php to get full url from address bar.


1. $_SERVER['HTTP_HOST']
2. $_SERVER['REQUEST_URI']


$_SERVER['HTTP_HOST'] this function will show only server name.
$_SERVER['REQUEST_URI'] this function will show you the path to file of your url.


Sample Code:
$url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
Continue Reading

PHP: Create CSV file format in php

fputcsv

(PHP 5 >= 5.1.0RC1)
fputcsv --  Format line as CSV and write to file pointer

Description

int fputcsv ( resource handle [, array fields [, string delimiter [, string enclosure]]] )

fputcsv() formats a line (passed as a fields array) as CSV and write it to the specified file handle. Returns the length of the written string, or FALSE on failure.
The optional delimiter parameter sets the field delimiter (one character only). Defaults as a comma: ,.
The optional enclosure parameter sets the field enclosure (one character only) and defaults to a double quotation mark: ".

Example 1. fputcsv() example

$list 
= array (
    
'aaa,bbb,ccc,dddd',
    
'123,456,789',
    
'"aaa","bbb"');
$fp fopen('file.csv''w');

foreach (
$list as $line) {
    
fputcsv($fpsplit(','$line));
}
fclose($fp);?>
Continue Reading

What Is PHP?

What Is PHP? PHP stands for PHP: Hypertext Preprocessor, a recursive acronym. It is mainly a Web server side scripting language. But it can also be used for other purposes.

PHP was originally created by Rasmus Lerdorf in 1995 as a simple Web page generation tool named as PHP/FI (Personal Home Page/Forms Interpreter). Today it becomes the number one of server side scripting languages.


Main features of PHP:

* Cookies - PHP transparently supports HTTP cookies.
* Sessions - PHP supports sessions to allow you to preserve certain data across subsequent accesses.
* Connection handling - PHP maintains a connection in 3 possible states: NORMAL, ABORTED, and TIMEOUT.
* Using remote files - PHP allows you to open remote files with Internet protocols like HTTP or FTP.
* Persistent Database Connections - PHP supports persistent connections to database servers.
* File uploads - PHP allows Web users to upload single or multiple files.
* Safe mode - PHP supports a safe mode with many built-in functions restricted or disabled.
* Command line - PHP supports many command line options.
Continue Reading

What is the difference between Primary Key and Unique key?

Primary Key: A column in a table whose values uniquely identify the
rows in the table. A primary key value cannot be NULL.

Unique Key: Unique Keys are used to uniquely identify each row in the
table. There can be one and only one row for each unique key value. So
NULL can be a unique key.There can be only one primary key for a table but there can be more
than one unique for a table.
Continue Reading

PHP: Performance Tips - For Loop

Don't use for($i=0; $i<=count($arrPerson); $i++) { . . } 
instead of use for($i=0,$total = count($arrPerson); $i<$total; $i++) { .  . }
The previous one will call count function for each iteration of the loop while the second one will call count function just once

Continue Reading

Connect the Mysql Database from local client Computer

Create the New privilege for the Client computer name as the host name.
Then try the following sample code into your client computer,
It will be work exactly.

Sample PHP Code :
$con=mysql_connect('IP Address','root','') or die("Database Connection Failed
". mysql_error());
mysql_select_db('',$con);
$test_query=mysql_query('SELECT * FROM

'); while($row=mysql_fetch_array($test_query)) { echo $row[column].' '; } ?>
Continue Reading