Read 171 times | Created 2013-07-25 04:58:55 | Updated 2014-07-01 06:08:55 | | |

 

<?php

/*--
-- Database: `test`
--
-- --------------------------------------------------------
--
-- Table structure for table `tbl_data`
--
USE `test`;
 
DROP TABLE IF EXISTS `tbl_data`;
CREATE TABLE IF NOT EXISTS `tbl_data` (
  `no` smallint NOT NULL AUTO_INCREMENT,
  `nama` varchar(10) NOT NULL,
  `kelas` varchar(20) NOT NULL,
  PRIMARY KEY (`no`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

INSERT INTO `tbl_data`
VALUES
(1,'dede','satu'),      
(2,'deni','dua'),       
(3,'narji','tiga'),     
(4,'wendi','empat'),    
(5,'tatang','lima'),    
(6,'dudi','enam'),      
(7,'doni','enam'),      
(8,'dudung','dua'),     
(9,'dadang','tiga'),    
(10,'nani','empat'),    
(11,'dudi','lima'),     
(12,'doni','dua'),      
(13,'dudung','tiga'),   
(14,'dadang','empat'),  
(15,'nani','lima'),     
(16,'rodin','sepuluh'), 
(17,'syukur','sepuluh'),
(18,'asep','tujuh'),    
(19,'kohar','sembilan'),
(20,'asti','dua');

*/
?>
<!DOCTYPE html>
<html>
  <head>
    <title>Dynamic Column</title>
  </head>
  <body>
  <?php
  $jml_kolom=4;
  $dbhost='localhost';
  $dbuser='root';
  $dbpass='';
  $dbname='test';
  $db=new mysqli($dbhost,$dbuser,$dbpass,$dbname);
  $sql="SELECT * FROM tbl_data";
  $result=$db->query($sql);
  $data=array();
  while($record=$result->fetch_row()) $data[]=$record;
  /*
  $data=array(
    array(1,'dede','satu'),
    array(2,'deni','dua'),
    array(3,'narji','tiga'),
    array(4,'wendi','empat'),
    array(5,'tatang','lima'),
    array(6,'dudi','enam'),
    array(7,'doni','enam'),
    array(8,'dudung','dua'),
    array(9,'dadang','tiga'),
    array(10,'nani','empat'),
    array(11,'dudi','lima'),
    array(12,'doni','dua'),
    array(13,'dudung','tiga'),
    array(14,'dadang','empat'),
    array(15,'nani','lima'),
    array(16,'rodin','sepuluh'),
    array(17,'syukur','sepuluh'),
    array(18,'asep','tujuh'),
    array(19,'kohar','sembilan'),
    array(20,'asti','dua'),
  );
  */
  $jml_data=count($data);
  $jml_baris=ceil($jml_data/$jml_kolom);
  echo "jumlah data : $jml_data<br>jumlah kolom: $jml_kolom";
  ?>
    <h3>Data urut berdasarkan baris baru pindah kolom</h3>
    <table border="0">
      <tr>
      <?php
      $i=0;
      $b=$a=0;
      $sisa=($jml_data%$jml_kolom!=0?$jml_kolom-$jml_data%$jml_kolom:0);
      $jml_data=count($data);
      while($jml_data)
      {
        $n=$i++;
        $k=$n%$jml_kolom;
        if($k==0&&$n!=0){
          echo "</tr>n<tr>";
          ++$b;
        }
        $m=$k*$jml_baris+$b;
        echo "<td>".$data[$m][1]."</td><td>=</td><td>".$data[$m][2]."</td>";
        $jml_data--;
      }
      //tampilkan sisa cell yang tidak ada datanya
      if($sisa)
        for($i=0;$i<$sisa;$i++)
          echo "<td>&nbsp;</td>";
      ?>
      </tr>
    </table>
    
    <h3>Data urut berdasarkan kolom baru pindah baris</h3>
    <table border="0">
      <tr>
      <?php
      $i=0;
      $sisa=($jml_data%$jml_kolom!=0?$jml_kolom-$jml_data%$jml_kolom:0);
      $jml_data=count($data);
      while($jml_data)
      {
        $n=$i++;
        echo (($n%$jml_kolom==0&&$n!=0)?"</tr>n<tr>":'')
             ."<td>".$data[$n][1]."</td><td>=</td><td>".$data[$n][2]."</td>";
        $jml_data--;
      }
      //tampilkan sisa cell yang tidak ada datanya
      if($sisa)
        for($i=0;$i<$sisa;$i++)
          echo "<td>&nbsp;</td>";
      ?>
      </tr>
    </table>    
    <?php 
    //echo "sisa cell $sisa";
    ?>
  </body>
</htmL>