Monday, June 28, 2010

PHP foreach loop

PHP Class Constructor

//parent class

class BaseModel
{
    public $table;
    public $db_host;
    public $db_usr;
    public $pw;
    public $db;
   
    public function __construct()
    {
        $this->db_host = "some value";
        $this->db_usr = "some value";
        $this->pw = "some value";
        $this->db = "some value";
    }
}

//child class
class ChildModel extends BaseModel
{
    public function __construct()
    {
        parent::__construct();          
       $this->table = "some value";
    }
}

Saturday, June 26, 2010

Wednesday, June 23, 2010

MSSQL SPLIT Function


CREATE FUNCTION [dbo].[SPLIT]
(
        @inputString NVARCHAR(max),
        @delimiter CHAR
)
RETURNS @result TABLE (token NVARCHAR(max))
AS
BEGIN

DECLARE @startposition INT
DECLARE @endposition INT
DECLARE @inputLength INT
DECLARE @lengthToParse INT

SET @startposition = 1
SET @endposition = 1
SET @inputLength = LEN(@inputString)

WHILE @startposition <= @inputLength 
    BEGIN 


        SELECT @endpositioncharindex(@delimiter,@inputString,@startposition)  


        IF @endposition <> 0
        BEGIN
              SET @endposition = @endposition - 1
        END
        ELSE
        BEGIN
              SET @endposition = @inputLength
       END

       SET @lengthToParse = @endposition - @startposition + 1
       INSERT INTO @result(tokenVALUES(substring(@inputString,@startposition,@lengthToParse))
       SET @startposition = @endposition + 2

END

RETURN

END

Sample usage:

SELECT * FROM SPLIT('abc.def.ghi','.')

Will return:
abc
def
ghi

 

MSSQL SPLIT Function


CREATE FUNCTION [dbo].[SPLIT]
(
        @inputString NVARCHAR(max),
        @delimiter CHAR
)
RETURNS @result TABLE (token NVARCHAR(max))
AS
BEGIN

DECLARE @startposition INT
DECLARE @endposition INT
DECLARE @inputLength INT
DECLARE @lengthToParse INT

SET @startposition = 1
SET @endposition = 1
SET @inputLength = LEN(@inputString)

WHILE @startposition <= @inputLength 
    BEGIN 


        SELECT @endpositioncharindex(@delimiter,@inputString,@startposition)  


        IF @endposition <> 0
        BEGIN
              SET @endposition = @endposition - 1
        END
        ELSE
        BEGIN
              SET @endposition = @inputLength
       END

       SET @lengthToParse = @endposition - @startposition + 1
       INSERT INTO @result(tokenVALUES(substring(@inputString,@startposition,@lengthToParse))
       SET @startposition = @endposition + 2

END

RETURN

END

Sample usage:

SELECT * FROM SPLIT('abc.def.ghi','.')

Will return:
abc
def
ghi

 

MSSQL LIKE Clause


SELECT * FROM myTable WHERE name LIKE '%abc%'
- This will return rows that contains the substring "abc"

SELECT * FROM myTable WHERE name LIKE '_abc'
- This will return rows that starts with any single character followed by the word "abc"

SELECT * FROM myTable WHERE name LIKE '[A-J]abc'
- This will return rows that starts with letter A to J followed by the word "abc"


SELECT * FROM myTable WHERE name LIKE '[^z]abc'
- This will return rows that starts with any single character except letter "z" followed by the word "abc"

SELECT * FROM myTable WHERE name LIKE '[abc]efg'
- This will return rows that starts with a or b or c followed by efg


SELECT * FROM myTable WHERE name LIKE '[ja-ju]'
 - This will return rows that starts from ja- to ju-


SELECT * FROM myTable WHERE name NOT LIKE '%[_]%'
SELECT * FROM myTable WHERE name LIKE '%[^_]%'
- This will return rows that does not contain the character "_" (underscore)

MSSQL LIKE Clause


SELECT * FROM myTable WHERE name LIKE '%abc%'
- This will return rows that contains the substring "abc"

SELECT * FROM myTable WHERE name LIKE '_abc'
- This will return rows that starts with any single character followed by the word "abc"

SELECT * FROM myTable WHERE name LIKE '[A-J]abc'
- This will return rows that starts with letter A to J followed by the word "abc"


SELECT * FROM myTable WHERE name LIKE '[^z]abc'
- This will return rows that starts with any single character except letter "z" followed by the word "abc"

SELECT * FROM myTable WHERE name LIKE '[abc]efg'
- This will return rows that starts with a or b or c followed by efg


SELECT * FROM myTable WHERE name LIKE '[ja-ju]'
 - This will return rows that starts from ja- to ju-


SELECT * FROM myTable WHERE name NOT LIKE '%[_]%'
SELECT * FROM myTable WHERE name LIKE '%[^_]%'
- This will return rows that does not contain the character "_" (underscore)

MSSQL Paging Query

Assuming:
@page = 1 (INT)
@itemsPerPage = 1000 (INT)


SELECT * FROM (
    SELECT ROW_NUMBER() OVER (ORDER BY myColumn) as RowNumber,*
    FROM myTable
) as tempTable
WHERE
tempTable.RowNumber BETWEEN (((@page - 1) * @itemsPerPage) + 1) AND (@page * @itemsPerPage)


This query will return the first 1000 rows sorted by myColumn

MSSQL Paging Query

Assuming:
@page = 1 (INT)
@itemsPerPage = 1000 (INT)


SELECT * FROM (
    SELECT ROW_NUMBER() OVER (ORDER BY myColumn) as RowNumber,*
    FROM myTable
) as tempTable
WHERE
tempTable.RowNumber BETWEEN (((@page - 1) * @itemsPerPage) + 1) AND (@page * @itemsPerPage)


This query will return the first 1000 rows sorted by myColumn