Skip to content

Instantly share code, notes, and snippets.

@amin007
Last active December 29, 2015 10:58
Show Gist options
  • Save amin007/7660151 to your computer and use it in GitHub Desktop.
Save amin007/7660151 to your computer and use it in GitHub Desktop.
Hendak buat bandingan jualan antara 2 bulan seperti jan dan feb
<?php
/*
//anggap $bulan adalah array seperti ini
$bulan = array('rangka',
'jan', 'feb', 'mac', 'apr',
'mei', 'jun', 'jul', 'ogo',
'sep', 'okt', 'nov', 'dis');
*/
class Bandingan_Tanya extend Tanya
{
public cariBandingan($bulan, $cari, $id)
{// kalau guna union sql
foreach ($bulan as $key => $myTable):
if ( ! in_array($key,array(0,1)))
{
$sebelum = $bulan[$key - 1];
$semasa = $myTable;
// hasil
$hasilSebelum = 'format(sebelum.hasil,0)';
$hasilSemasa = 'format(semasa.hasil,0)';
$hasil = "concat($hasilSebelum,'<br>',$hasilSemasa) as hasil";
$peratus = " format( (($hasilSemasa-$hasilSebelum)/$hasilSebelum)*100 ,2) as `Beza Hasil` ";
// dptLain
$dptLainSebelum = 'format(sebelum.dptLain,0)';
$dptLainSemasa = 'format(semasa.dptLain,0)';
$dptLain = "concat($dptLainSebelum,'<br>',$dptLainSemasa) as hasil";
$peratus2 = " format( (($dptLainSemasa-$dptLainSebelum)/$dptLainSebelum)*100 ,2) as `Beza dptLain` ";
// gaji
$gajiSebelum = 'format(sebelum.gaji,0)';
$gajiSemasa = 'format(semasa.gaji,0)';
$gaji = "concat($gajiSebelum,'<br>',$gajiSemasa) as hasil";
$peratus3 = " format( (($gajiSemasa-$gajiSebelum)/$gajiSebelum)*100 ,2) as `Beza Gaji` ";
// staf
$stafSebelum = 'format(sebelum.staf,0)';
$stafSemasa = 'format(semasa.staf,0)';
$staf = "concat($stafSebelum,'<br>',$stafSemasa) as hasil";
$peratus4 = " format( (($stafSemasa-$stafSebelum)/$stafSebelum)*100 ,2) as `Beza Staf` ";
// sql
$sql[] = "SELECT "
. "$hasil, $peratus, "
. "$dptLain, $peratus2, "
. 'semasa.web,'
. "$gaji, $peratus3, "
. "$staf, $peratus4, "
. 'semasa.outlet,semasa.sebab, substring($semasa,5,5) as bulan '
. "FROM $sebelum as sebelum, $semasa as semasa "
. "WHERE sebelum.id = semasa.id "
. "AND semasa.id = '$id' ";
}
elseif ( in_array($key,array(0)))
{
//
}
endforeach;
// masuk dalam $data
$senaraiSql = implode("UNION",$sql);
$data['Kawal'][] = $this->db->select($senaraiSql);
return $data;
}
}
<?php
/*
//anggap $bulan adalah array seperti ini
$bulan = array('rangka',
'jan', 'feb', 'mac', 'apr',
'mei', 'jun', 'jul', 'ogo',
'sep', 'okt', 'nov', 'dis');
*/
class Bandingan_Tanya extend Tanya
{
public cariBandingan($bulan, $cari, $id)
{// kalau guna foreach
foreach ($bulan as $key => $myTable):
if ( ! in_array($key,array(0,1)))
{
$sebelum = $bulan[$key - 1];
$semasa = $myTable;
// hasil
$hasilSebelum = 'format(sebelum.hasil,0)';
$hasilSemasa = 'format(semasa.hasil,0)';
$hasil = "concat($hasilSebelum,'<br>',$hasilSemasa) as hasil";
$peratus = " format( (($hasilSemasa-$hasilSebelum)/$hasilSebelum)*100 ,2) as `Beza Hasil` ";
// dptLain
$dptLainSebelum = 'format(sebelum.dptLain,0)';
$dptLainSemasa = 'format(semasa.dptLain,0)';
$dptLain = "concat($dptLainSebelum,'<br>',$dptLainSemasa) as hasil";
$peratus2 = " format( (($dptLainSemasa-$dptLainSebelum)/$dptLainSebelum)*100 ,2) as `Beza dptLain` ";
// gaji
$gajiSebelum = 'format(sebelum.gaji,0)';
$gajiSemasa = 'format(semasa.gaji,0)';
$gaji = "concat($gajiSebelum,'<br>',$gajiSemasa) as hasil";
$peratus3 = " format( (($gajiSemasa-$gajiSebelum)/$gajiSebelum)*100 ,2) as `Beza Gaji` ";
// staf
$stafSebelum = 'format(sebelum.staf,0)';
$stafSemasa = 'format(semasa.staf,0)';
$staf = "concat($stafSebelum,'<br>',$stafSemasa) as hasil";
$peratus4 = " format( (($stafSemasa-$stafSebelum)/$stafSebelum)*100 ,2) as `Beza Staf` ";
// sql
$sql = "SELECT "
. "$hasil, $peratus, "
. "$dptLain, $peratus2, "
. 'semasa.web,'
. "$gaji, $peratus3, "
. "$staf, $peratus4, "
. 'semasa.outlet,semasa.sebab, substring($semasa,5,5) as bulan '
. "FROM $sebelum as sebelum, $semasa as semasa "
. "WHERE sebelum.id = semasa.id "
. "AND semasa.id = '$id' ";
// masuk dalam $data
$data['Kawal'][] = $this->db->select($sql);
}
elseif ( in_array($key,array(0)))
{
//
}
endforeach;
return $data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment