Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- USE test;
- DROP TABLE IF EXISTS pupuk;
- CREATE TABLE IF NOT EXISTS pupuk(
- id_pupuk INT AUTO_INCREMENT,
- nama_pupuk VARCHAR(50),
- harga_pupuk INT,
- PRIMARY KEY(id_pupuk)
- );
- INSERT INTO pupuk(nama_pupuk,harga_pupuk)
- VALUES
- ('urea',2300),
- ('npk',3000),
- ('ts',3100),
- ('kcl',9000);
- DROP TABLE IF EXISTS benih;
- CREATE TABLE IF NOT EXISTS benih(
- id_benih INT AUTO_INCREMENT,
- nama_benih VARCHAR(50),
- harga_benih INT,
- PRIMARY KEY(id_benih)
- );
- INSERT INTO benih(nama_benih,harga_benih)
- VALUES
- ('IR64',1275),
- ('Setra Ramos',1325),
- ('Rojolele',1420),
- ('Pandan wangi',1500),
- ('Flint Corn',730),
- ('Dent Corn',740),
- ('Sweet Corn',765),
- ('Baluran',1520),
- ('Mahameru',1480);
- DROP TABLE IF EXISTS tanaman;
- CREATE TABLE IF NOT EXISTS tanaman(
- id_tanaman INT AUTO_INCREMENT,
- id_benih INT,
- nama_tanaman VARCHAR(50),
- patokan_benih INT,
- PRIMARY KEY(id_tanaman)
- );
- INSERT INTO tanaman(id_benih,nama_tanaman,patokan_benih)
- VALUES
- (2,'Padi',1),
- (7,'Jagung',2),
- (8,'Kacang Kedelai',1);
- DROP TABLE IF EXISTS pupuk_tanaman;
- CREATE TABLE IF NOT EXISTS pupuk_tanaman(
- id_tanaman INT,
- id_pupuk INT,
- patokan INT
- );
- INSERT INTO pupuk_tanaman
- VALUES
- (1,1,3),
- (1,2,2),
- (1,4,1),
- (2,1,1),
- (2,3,2),
- (3,2,2),
- (3,4,1);
- */
- $dbhost='localhost';
- $dbuser='root';
- $dbpass='';
- $dbname='test';
- $con=new mysqli($dbhost,$dbuser,$dbpass,$dbname);
- //-- luas lahan
- $jml_lahan=100;
- //-- data pupuk
- $sql='
- SELECT
- id_pupuk,nama_pupuk,harga_pupuk
- FROM
- pupuk
- ORDER BY
- id_pupuk';
- $result=$con->query($sql);
- $data_pupuk=array();
- while($row=$result->fetch_object()){
- $data_pupuk[]=$row;
- }
- $jml_pupuk=count($data_pupuk);
- //-- data tanaman
- $sql='
- SELECT
- a.id_tanaman,
- a.nama_tanaman,
- a.patokan_benih,
- b.harga_benih,
- d.id_pupuk,
- c.patokan
- FROM
- tanaman a
- JOIN benih b USING(id_benih)
- JOIN pupuk_tanaman c USING(id_tanaman)
- JOIN pupuk d USING(id_pupuk)
- ORDER BY
- a.id_tanaman,
- d.id_pupuk';
- $result=$con->query($sql);
- $data=array();
- $tanaman='';
- $i=0;
- while($row=$result->fetch_object()){
- if($tanaman<>$row->id_tanaman){
- $tanaman=$row->id_tanaman;
- ++$i;
- $data[++$i]=array(
- 'nama'=>$row->nama_tanaman,
- 'patokan'=>$row->patokan_benih,
- 'harga'=>$row->harga_benih
- );
- }
- $data[$i][$row->id_pupuk]=$row->patokan;
- }
- ?>
- <table border='1'>
- <thead>
- <tr>
- <th rowspan='3'>Tanaman</th>
- <th rowspan='3'>Luas</th>
- <th colspan='2' rowspan='2'>Benih</th>
- <th colspan='<?php echo $jml_pupuk*2;?>'>Pupuk</th>
- </tr>
- <tr>
- <?php
- foreach($data_pupuk as $pupuk){
- echo "<th colspan='2'>{$pupuk->nama_pupuk}</th>";
- }
- ?>
- </tr>
- <tr>
- <?php
- for($i=0;$i<$jml_pupuk+1;++$i){
- echo '<th>Q</th><th>TH</th>';
- }
- ?>
- </tr>
- </thead>
- <tbody>
- <?php
- foreach($data as $d){
- echo "<tr>
- <td>{$d['nama']}</td>
- <td>{$jml_lahan}</td>
- <td>".($d['patokan']*$jml_lahan)."</td>
- <td>".($d['patokan']*$jml_lahan*$d['harga'])."</td>";
- foreach($data_pupuk as $pupuk){
- echo
- '<td>'
- .(isset($d[$pupuk->id_pupuk])?$d[$pupuk->id_pupuk]:' ')
- .'</td><td>'
- .(isset($d[$pupuk->id_pupuk])?$d[$pupuk->id_pupuk]*$pupuk->harga_pupuk:' ')
- .'</td>';
- }
- echo "</tr>";
- }
- ?>
- </tbody>
- </table>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement