Monday, October 22, 2012

PHP Function to calculate days difference between given 2 dates.

Here is a simple  Function to calculate days difference between given 2 dates.



function daysDifference($endDate, $beginDate)
{

              //explode the date by "-" and storing to array
             $date_parts1=explode("-", $beginDate);
             $date_parts2=explode("-", $endDate);
             //gregoriantojd() Converts a Gregorian date to Julian Day Count
            $start_date=gregoriantojd($date_parts1[1], $date_parts1[2], $date_parts1[0]);
             $end_date=gregoriantojd($date_parts2[1], $date_parts2[2], $date_parts2[0]);
             return $end_date - $start_date;
}

PHP Function to convert date from mm/dd/yyyy to yyyy-mm-dd

Here is a simple Function to convert date from  mm/dd/yyyy to yyyy-mm-dd.



function convert_date($date)
{
                        if($date=='')
                        return '';
                     else
                        $dateArray=explode('/',$date);

                        return "$dateArray[2]-$dateArray[0]-$dateArray[1]";
}

PHP Function to validate an email address

Here is a simple Function to validate an email address in php.



function check_email($mail_address)
{
                        if($mail_address == '')
                        {
                                    return false;
                        }
                        else
                        {
if (preg_match("/^[0-9a-z]+(([\.\-_])[0-9a-z]+)*@[0-9a-z]+(([\.\-])[0-9a-z-]+)*\.[a-z]{2, 4}$/i",  strtolower($mail_address)))
                                    return true;
                        else
                                    return false;
                        }
}

PHP Function to get extension of a file.

Here is a simple function which returns the extension of the file.



           function getExtension($str)
{
                        $i = strrpos($str,".");
                        if (!$i) { return ""; }
                        $l = strlen($str) - $i;
                        $ext = substr($str,$i+1,$l);
                        return $ext;

Saturday, August 11, 2012

Create table in PostgreSQL.

In this Section we are going to learn that how are tables created in PostgreSQL.

First of all you need the PostgreSQL software installed on your computer. If you don't have no problem you can download from the following link. Choose your platform and you are ready to download.

Download :  PostgreSQL

OK after you download and install the software then run it from the command prompt as mentioned in the read me manual.

A screen will appear in front of you when you run the program by filling in the appropriate login or authentication information.

Then simple write the following lines on the screen and run it.

Type the two paragraphs A) and B) on your screen and run it.

A) Item Table

     create table item
      (item_no integer primary key,
      item_name text,
      qty float);

B) Supplier table.

     create table supplier
     (supp_no integer primary key,
     supp_name text,
     address text,
     city text,
     phoneno integer);

When you run these two codes in your command prompt two tables named Item  and Supplier will be created in your database.

Now we will insert data in these two tables. now Run the Following statements on your command prompt and data will be inserted in both tables.

C) Inserting Data in Item Table.

     insert into item values(1,'cdrom',1200);
     insert into item values(2,'cd writer',1500);
     insert into item values(3,'fdd',800);
     insert into item values(4,'hdd',3200);
     insert into item values(5,'speaker',1200);
     insert into item values(6,'usb',500);
     insert into item values(7,'mouse',500);
     insert into item values(8,'monitor',1200);





D) Inserting Data in Supplier table.


     insert into supplier values(1,'singh0','ozar0','nasik0',5602077);
     insert into supplier values(2,'singh1','ozar1','nasik1',5602076);
     insert into supplier values(3,'singh2','ozar2','nasik2',5602075);
     insert into supplier values(4,'singh3','ozar3','nasik3',5602074);
     insert into supplier values(5,'singh4','ozar4','nasik4',5602073);
     insert into supplier values(6,'singh5','ozar5','nasik5',5602072);
     insert into supplier values(7,'singh6','ozar6','nasik6',5602071);
     insert into supplier values(8,'singh7','ozar7','nasik7',5602070);*/
     insert into supplier values(9,'singh8','ozar8','nasik0',2775411);





PL/pgSql



The Full Form of PL/pg Sql is  Procedural language/Postgre Sql.
It is a Structured Query language. It is Almost Similar to Sql Queries but it is more structured and more easy than the Sql Commands and functions.

Specifications of the Pl/pg Sql is as Follows:

1) Database created has no Size Limit ie it can be unlimited.
2) The table created in it can have a maximum size of  32 TB and not more than that.
3) The maximum Row Size can be maximum  upto 1.6 TB and not more than that.
4) There can be any number of rows per tables .
5) It is Highly Customizable.

Postgres Sql is available under the liberal open source license(LOSL) to download and modify,distribute and use for development. It is a powerful tool that can be used for home applications, web development, ecommerce and at places where softwares or products of high RDBMS is required.

Below is a Simple example of Postgres sql function.



 
create table Doctor
    (
        doc_no        integer        primary key    ,
        doc_name     text        not null    ,
        address        text        not null    ,
        city        text        not null   
    );

create table Hospital
    (
        hosp_no        integer        primary key    ,
        hosp_name    text        not null    ,
        hosp_city    text        not null   
    );   

create table Doc_Hosp
    (
        doc_no        integer        references Doctor    ,
        hosp_no        integer        references Hospital   
    );

insert into Doctor values(1,'Mr. Chandan','Ram krishna Nagar','Nasik');
insert into Doctor values(2,'Mr. Rahul','Gagapur road','Pune');
insert into Doctor values(3,'Mr. Amit','Ojhar','Mumbai');
insert into Doctor values(4,'Mr. Prasanna','Trimurti','Nagpur');
insert into Doctor values(5,'Mr. Atul','Colony','Kanpur');

insert into Hospital values(1,'Grand Hospital','Nasik');
insert into Hospital values(2,'Charity Hospital','Pune');
insert into Hospital values(3,'Perfect Hospital','Nagpur');
insert into Hospital values(4,'Maya Hospital','Kanpur');
insert into Hospital values(5,'Hayat Hospital','Mumbai');

insert into Doc_Hosp values(1,2);
insert into Doc_Hosp values(1,3);
insert into Doc_Hosp values(1,4);
insert into Doc_Hosp values(1,5);
insert into Doc_Hosp values(2,3);
insert into Doc_Hosp values(2,4);
insert into Doc_Hosp values(3,5);
insert into Doc_Hosp values(4,4);
insert into Doc_Hosp values(5,3);
insert into Doc_Hosp values(5,1);


a) 
create or replace function ass1()
    returns text as'

declare
    cursor1 cursor for
            select doc_no,doc_name,city
            from doctor;
   
    cursor2 cursor(city text)  for
            select hosp_no
            from hospital
            where hospital.hosp_city <> city;

    cursor3 cursor(dno integer) for
            select *
            from doc_hosp
            where doc_hosp.doc_no = dno;
       
    temp1 record;           
    temp2 record;
    temp3 record;
    flag integer;
    output text := ''\n'';

begin
   
    open cursor1;
    loop
        fetch cursor1 into temp1;
        exit when not found;   

        flag := 1;
       
        open cursor2(temp1.city);
        loop
            fetch cursor2 into temp2;   
            exit when not found;

            open cursor3(temp1.doc_no);
            loop
                fetch cursor3 into temp3;   
                exit when not found;

                if(temp2.hosp_no = temp3.hosp_no) then
                    flag := 1;
                output := output||''Match found : ''||temp2.hosp_no||''\n'';
                    exit;   
                else
                    flag := 0;
                end if;
            end loop;
            close cursor3;   -- cursor3 end

            if(flag = 0) then
                exit;
            end if;   
        end loop;
        close cursor2;  -- cursor2 end

        if(flag = 1) then
            output := output || temp1.doc_name ||''\n'';
        end if;
    end loop;
    close cursor1;    -- cursor1 end

    return output;
end;

'language plpgsql

/*
    OUTPUT 

SLIP=# select ass1();
     ass1
---------------
 
Mr. Chandan

(1 row)

*/



b)   create or replace function ass2()returns text as'

declare
    cursor1 cursor for
            select doc_no,doc_name
            from doctor;   

    cursor2 cursor(dno integer) for
            select *
            from doc_hosp
            where doc_hosp.doc_no = dno;

    temp3 hospital %rowtype;
    temp1 record;
    temp2 record;
    output text := ''\n'';

begin
    open cursor1;
    loop
        fetch cursor1 into temp1;
        exit when not found;

        output := output || temp1.doc_name || ''\n'';

        open cursor2(temp1.doc_no);
        loop
            fetch cursor2 into temp2;
            exit when not found;   

            for temp3 in select * from hospital where hospital.hosp_no = temp2.hosp_no
            loop
                 output := output ||''\t''||temp3.hosp_name||''\n'';
            end loop; -- close for temp3
        end loop;
        close cursor2; -- close cursor2
    end loop;   
    close cursor1; -- close cursor1

    return output;
end;

'language plpgsql;
/*

    OUTPUT

SLIP=# select ass2();
                                                                                                             ass2                                                                                                    
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 
Mr. Chandan
        Charity Hospital
        Perfect Hospital
        Maya Hospital
        Hayat Hospital
Mr. Rahul
        Perfect Hospital
        Maya Hospital
Mr. Amit
        Hayat Hospital
Mr. Prasanna
        Maya Hospital
Mr. Atul
        Perfect Hospital
        Grand Hospital

(1 row)

*/

Wednesday, August 8, 2012

Linklist in Java

Here is simple Link List program in java.

import java.util.*;

class linklist
{
    public static void main(String args[])
    {
        LinkedList l1 = new LinkedList();
        LinkedList l2 = new LinkedList();
        LinkedList l3 = new LinkedList();
        LinkedList l4 = new LinkedList();
        int i,j;
      
        l1.add("a");
        l1.add("b");
        l1.add("c");
        l1.add("d");
        l1.add("e");
      
        l2.add("a");
        l2.add("c");
        l2.add("e");
        l2.add("g");
        l2.add("i");

        System.out.print("Intersection of link list : ");
        for(i = 0 ; i < 5 ; i++)
        {
            for(j = 0 ; j < 5 ; j++)
            {
                if(l1.get(i).equals(l2.get(j)))
                {
                    l3.add(l1.get(i));
                    break;
                }
            }
        }
        System.out.print(l3);
      
        System.out.print("\nUnion of list : ");
        for(i = 0 ; i < 5 ; i++)
        {
            l4.add(l1.get(i));
        }  
        for(i = 0 ; i < 5 ; i++)
        {
             for(j = 0 ; j < 5 ; j++)
             {
                 if(l2.get(i).equals(l1.get(j)))
                 {
                     break;
                 }
             }
             if(j == 5)
             {
                 l4.add(l2.get(i));
             }
        }
        System.out.print(l4);
    }
}

Dialog box in Java

Java Program which displays a dialog box on body load(Page Load).

import java.awt.*;
import java.awt.event.*;


public class msgDialog extends Frame
{
  msgDialog()
  {
    messageBox mb = new messageBox(this,"This is a Simple Dialog Box!!!");
    mb.setLocation(200,200);
    mb.setVisible(true);
  }
 
  public static void main(String args[])
  {
    msgDialog md = new msgDialog();
    System.out.println("Displaying Dialog Box....");
    md.setVisible(true);
  }
}

Message Box in Java

Display a Message box in Java on Button Click.

import java.awt.*;
import java.awt.event.*;

public class messageBox extends Dialog
{
  Button b1,b2;
  messageBox(Frame fm,String lab)
  {
    super(fm,"Message",true);
    setLayout(new GridLayout(2,1,0,0));

    Panel p1 = new Panel();
    Panel p2 = new Panel();

    p1.setLayout(new FlowLayout(FlowLayout.CENTER,20,15));
    p2.setLayout(new FlowLayout(FlowLayout.CENTER,20,15));

    p1.add(new Label(lab));

    b1 = new Button("Ok");
    b1.addActionListener(new B1());
    p2.add(b1);

    b2 = new Button("Cancel");
    b2.addActionListener(new B1());
    p2.add(b2);

    add(p1);
    add(p2);

    setSize(350,125);
    addWindowListener(new WindowAdapter()
    {
      public void windowClosing(WindowEvent we)
      {
        System.exit(0);
      }
    });
  }

  class B1 implements ActionListener
  {
    public void actionPerformed(ActionEvent ae)
    {
      try
      {
        //Button ok = (Button)ae.getSource();
        //String s = ok.getLabel();
        if(ae.getSource() == b1 || ae.getSource() == b2)
        {
      System.out.println("Button Clicked");
      dispose();
      System.exit(0);
        }
      }
      catch(Exception e){} 
    }
  }
}

Servlet Session in Java

Here is a simple servlet program which display session information.

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class sessionInfo extends HttpServlet
{
  public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
  {
    HttpSession mySession = req.getSession(true);
   
    res.setContentType("text/html");
    PrintWriter out = res.getWriter();
   
    out.println("<HTML><BODY>");
    out.println("<h3>Session Information</H3>");
    out.println("<BR>New Session    : " + mySession.isNew());
    out.println("<BR>Session ID : " + mySession.getId());
    out.println("<BR>Session Creation Time : " + mySession.getCreationTime());
    out.println("<BR>Session Last Accessed Time : " + mySession.getLastAccessedTime());

    out.println("<BR><H3>Request Information</H3>");
    out.println("<BR>Session ID from Request : " + req.getRequestedSessionId());
    out.println("<BR>Session ID via Cookie : " + req.isRequestedSessionIdFromCookie());
    out.println("<BR>Valid Session ID : " + req.isRequestedSessionIdValid());

    out.println("</BODY></HTML>");
    out.close();
  }
}

MS SQL : How to identify fragmentation in your indexes?

Almost all of us know what fragmentation in SQL indexes are and how it can affect the performance. For those who are new Index fragmentation...