Read 127 times | Created 2014-04-23 04:12:40 | Updated 2014-04-23 04:12:40 | | |

 

<?php
/*
USE `test`;

--
-- Table structure for table `buku`
--

DROP TABLE IF EXISTS `buku`;
CREATE TABLE IF NOT EXISTS `buku` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `nama_buku` varchar(100) NOT NULL,
  `jenis_id` int(11) unsigned NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 ;

--
-- Dumping data for table `buku`
--

INSERT INTO `buku` (`id`, `nama_buku`, `jenis_id`) VALUES
(1, 'Habis Gelap TT', 1),
(2, 'Mataharimu', 2),
(3, 'Selayang pandang', 3),
(4, 'Novel', 1),
(5, 'Bintang', 1);

-- --------------------------------------------------------

--
-- Table structure for table `jenisbuku`
--

DROP TABLE IF EXISTS `jenisbuku`;
CREATE TABLE IF NOT EXISTS `jenisbuku` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `jenisbuku` varchar(30) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 ;

--
-- Dumping data for table `jenisbuku`
--

INSERT INTO `jenisbuku` (`id`, `jenisbuku`) VALUES
(1, 'Komedi'),
(2, 'Ilmiah'),
(3, 'seni');

-- --------------------------------------------------------

--
-- Table structure for table `tujuan`
-- 

DROP TABLE IF EXISTS `tujuan`;
CREATE TABLE IF NOT EXISTS `tujuan` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `komedi` varchar(100) NOT NULL,
  `ilmiah` varchar(100) NOT NULL,
  `seni` varchar(100) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 ;
 
 
*/
$dbhost='localhost';
$dbuser='root';
$dbpass='greatday35';
$dbname='skripsi';
$db=new mysqli($dbhost,$dbuser,$dbpass,$dbname);
$sql="SELECT namabuku,jenis_id FROM buku";
$result=$db->query($sql);
$komedi=array();
$ilmiah=array();
$seni=array();
while($data=$result->fetch_object()){
  if($data->jenis_id==1){
    $komedi[]=$data->namabuku;
  }elseif($data->jenis_id==2){
    $ilmiah[]=$data->namabuku;
  }else{
    $seni[]=$data->namabuku;
  }
}
$num=max(count($komedi),count($ilmiah),count($seni));
$sql='INSERT INTO tujuan(`komedi`,`ilmiah`,`seni`) VALUES ';
$value=array();
for($i=0;$i<$num;++$i)
{
$value[]="(".(isset($komedi[$i])?"'{$komedi[$i]}'":'NULL').","
            .(isset($ilmiah[$i])?"'{$ilmiah[$i]}'":'NULL').","
            .(isset($seni[$i])?"'{$seni[$i]}'":'NULL').")";
}
$sql.=implode(",n",$value);
$db->query($sql);
?>