This is my table structure , Here i need to swap items .Which means, you can see type 3 always comes paired( type 3 items are always paired ).
 I just named paired items for understanding first 1 in pair is master and second one is sub. So master of the pair should not come 5,10 and 15 positions
if it comes that place i need to swap the next item into that place(neaxt item will be sub it should not considered as next item)
for example
pid 10 (comes in 10 position) i need to swap it like this
pid   type  name
..     ..   ..
10      2    B2
11      3    E1(master) 
12      3    A2(sub)
..     ..   ..
Table
 pid    type    pname
  1       1     A
  2       1     B
  3       2     C
  4       3     D(mater)
  5       3     E(sub)
  6       1     A1
  7       2     B1
  8       1     C1
  9       2     D1
  10      3     E1(master)    
  11      3     A2(sub)
  12      2     B2  
  13      1     C2
  14      2     D2
  15      1     E3 
screenshot
FOR FURTHER HELP
I GIVING YOU TABLE STRUCTURE AND TEST DATA, PLEASE IF YOU HAVE IDEA SHARE WITH ME !
CREATE QUERY
CREATE TABLE IF NOT EXISTS `table_swap1` (
    `pid` int(11) NOT NULL AUTO_INCREMENT,
    `type` int(11) NOT NULL,
    `name` varchar(50) NOT NULL,
    PRIMARY KEY (`pid`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=17;
INSERT QUERY
INSERT INTO `table_swap1` (`pid`, `type`, `name`) VALUES
    (1, 1, 'A'),(2, 1, 'B'),(3, 2, 'D'),(4, 3, 'E(master)'),
    (5, 3, 'f(sub)'),(6, 1, 'A1'),(7, 2, 'B1'),(8, 1, 'C1'),
    (9, 2, 'D1'),(10, 3, 'E1(master)'),(11, 3, 'A2(sub)'),(12, 2, 'B2'),
    (13, 1, 'C2'),(14, 2, 'D2'), (15, 1, 'E2');
My work and Result
  SELECT aa.pid, (
    CASE aa.pid
    WHEN bb.apid
    THEN bb.atype
    WHEN bb.bpid
    THEN bb.btype
    WHEN bb.cpid
    THEN bb.ctype
    ELSE aa.type
    END
    )
    TYPE , (
    CASE aa.pid
    WHEN bb.apid
    THEN bb.aname
    WHEN bb.bpid
    THEN bb.bname
    WHEN bb.cpid
    THEN bb.cname
    ELSE aa.name
    END
    )name
    FROM (
    SELECT a.pid +1 apid, a.TYPE atype, a.NAME aname,
           b.pid +1 bpid, b.type btype, b.name bname,
           c.pid -2 cpid, c.type ctype, c.name cname
    FROM table_swap1 a, table_swap1 b, table_swap1 c
    WHERE MOD( a.pid, 5 ) =0
    AND a.pid +1 = b.pid
    AND a.type =3
    AND a.type = b.type
    AND a.pid +2 = c.pid
    )bb, table_swap1 aa
    GROUP BY pid
This Query did what i exactly need ... But In my case pid is Primary key. so i can't get the results in 1 to 15 order . So can i do this row number... how can i do
all kind of suggestions are welcome even solution in php let me is this possible in mysql or any other way to do this ..
 
     
     
     
     
     
    