Bagaimana cara select count yang benar dengan 2 table pada codeigniter 4?

saya ingin menghitung data yang sudah ada dengan dua table dan ketika dua data itu sudah ada tidak dihitung 2x

WhatsApp Image 2022-10-11 at 12.20.11.jpegsaya ingin menghitung data id kec yang muncul pada data table satu lagi

WhatsApp Image 2022-10-11 at 12.21.39.jpegnah menghitungnya berdasarkan data table yang ini karena memilili satu data yang sama

dan berikut script codenya yang saya sudah buat dan masih error

WhatsApp Image 2022-10-11 at 12.26.59.jpeg

<?php

use App\Models\M_Unitkerja;

use App\Models\M_Desa;

use App\Models\M_Kecamatan;

use App\Models\M_Banksampah;

$this->unitkerja = new M_Unitkerja();

$this->desa = new M_Desa();

$this->kecamatan = new M_Kecamatan();

$this->banksampah = new M_Banksampah();

$thnini = date('Y');

$thnmulai =  $thnini - 11;

$kecamatan = "";

$tahuntrans = "";

$thnjalan = "";

$jmthn = "";

?>

<!--

<figure class="highcharts-figurex">

    <div id="containerx"></div>

</figure> -->

<h4 class="mt-3 header-title text-center" style="font-size: large;"> KINERJA BANK SAMPAH </h4>

<hr>

<div class="table-responsive b-0">

    <table id="sebaranbankxx" class="table table-striped table-bordered table-hover" style="width: 100%">

        <thead>

            <tr>

                <th rowspan="2" style="text-align: center;vertical-align: middle;border-width: 1px;">NO</th>

                <th rowspan="2" style="text-align: center;vertical-align: middle;border-width: 1px;">KECAMATAN</th>

                <th colspan="3" style="text-align: center;border-width: 1px;">PROSENTASE</th>

            </tr>

            <tr>

                <th style="text-align: center;border-width: 1px;">Cakupan Bank Sampah</th>

                <th style="text-align: center;border-width: 1px;">Pengurangan Sampah</th>

                <th style="text-align: center;border-width: 1px;">Pelaporan Bank Sampah</th>

            </tr>

        </thead>

        <tbody id="xx">

            <?php $nomor = 0;

            foreach ($unit as $key => $data2) :

                $nomor++;

                $konkec2    = $data2['nama_kec'];

                $kecamatan2    = "";

                $konpersencakup = "";

                $idkec    = $data2['id_kec'];

                $kecamatan2 .= "'$konkec2'" . ",";

                $totdesa = $this->desa->selectSum('jum_dusun')->where('id_kec =', $idkec)->first();

                $jumdaftar = $this->unitkerja->selectCount('thn_berdiri')->where('id_kec =', $idkec)->where('sts = 1')->where('tipe_id = 1')->first();

                if ($jumdaftar != null) {

                    $jumdaftarbank = $jumdaftar['thn_berdiri'];

                    $persencakupan =  $jumdaftarbank / $totdesa['jum_dusun'] * 100;

                } else {

                        $konpersencakup = 0;

                        $persencakupan = 0;

                        $persenkurang = 0;

                        // $konjmthn    = $jm;

                }

                foreach ($banksampah as $data) :

                    $kertas = $this->banksampah->selectSum('kertas')->join('custome__opd', 'custome__opd.opd_id = custome__kelolasampah.opd_id')

                        ->where('tipe_id =', 1)->where('sts_kelola =', 1)->where('bulan =', $data['bulan'])

                        ->where('tahun =', $data['tahun'])->where('id_kec', $data2['id_kec'])->first();

                    $plastik = $this->banksampah->selectSum('plastik')->join('custome__opd', 'custome__opd.opd_id = custome__kelolasampah.opd_id')

                        ->where('sts_kelola =', 1)->where('tipe_id =', 1)

                        ->where('bulan =', $data['bulan'])->where('tahun =', $data['tahun'])->where('id_kec', $data2['id_kec'])->first();

                    $kaca = $this->banksampah->selectSum('kaca')->join('custome__opd', 'custome__opd.opd_id = custome__kelolasampah.opd_id')

                        ->where('sts_kelola =', 1)->where('tipe_id =', 1)->where('bulan =', $data['bulan'])

                        ->where('tahun =', $data['tahun'])->where('id_kec', $data2['id_kec'])->first();

                    $logam = $this->banksampah->selectSum('logam')->join('custome__opd', 'custome__opd.opd_id = custome__kelolasampah.opd_id')

                        ->where('sts_kelola =', 1)->where('tipe_id =', 1)->where('bulan =', $data['bulan'])

                        ->where('tahun =', $data['tahun'])->where('id_kec', $data2['id_kec'])->first();

                    $lainnya = $this->banksampah->selectSum('lainnya')->join('custome__opd', 'custome__opd.opd_id = custome__kelolasampah.opd_id')

                        ->where('sts_kelola =', 1)->where('tipe_id =', 1)->where('bulan =', $data['bulan'])

                        ->where('tahun =', $data['tahun'])->where('id_kec', $data2['id_kec'])->first();

                        $jumlapor = $this->banksampah->selectCount('custome__kelolasampah.opd_id')

                        ->join('custome__opd', 'custome__opd.opd_id = custome__kelolasampah.opd_id')

                        ->where('sts_kelola =', 1)

                        ->where('custome__opd.opd_id !=', 0)

                        ->where('tipe_id =', 1)->where('bulan =', $data['bulan'])

                        ->where('tahun =', $data['tahun'])->where('id_kec', $data2['id_kec'])->first();

                    if ($banksampah) {

                        $totsampah = $this->kecamatan->selectSum('targetminus')->where('id_kec =', $idkec)->first();

                        if ($jumdaftar != null) {

                            $jumdaftarbank = $jumdaftar['thn_berdiri'];

                            $n1 =  $jumdaftarbank * 1300;

                            $n2 =  $totsampah['targetminus'] ;

                            $persenkurang =  ($n1 / $n2) * 100;

                        } else {

                            $persenkurang = 0;

                        }

                        // $n2 =  $totsampah * 0.3;

                        if ($jumlapor != null) {

                            $jmllapor = $jumlapor['opd_id'];

                            // $persenkurang =  $n1 / $n2 * 100;

                            $persenlapor = ($jmllapor / $jumdaftarbank ) * 100;

                        } else {

                            $persenkurang = 0;

                            $persenlapor = 0;

                        }

                    } else {

                        $totsampah = 0;

                        $persenkurang = 0;

                        $persenlapor = 0;

                        $jmllapor = 0;

                    }

                endforeach;

            ?>

                <tr>

                    <td width="3" class=" text-center p-1"><?= $nomor ?></td>

                    <td width="150" class="p-1 "><?= $konkec2 ?></td>

                    <td width="20" class="p-1 text-center"><?= round($persencakupan, 2) ?> %</td>

                    <td width="20" class="p-1 text-center"><?= round($persenkurang, 2) ?> %</td>

                    <td width="20" class="p-1 text-center"><?= round($persenlapor, 2) ?> %</td>

                </tr>

            <?php endforeach; ?>

        </tbody>

    </table>

</div>
avatar Faqih0776
@Faqih0776

4 Kontribusi 0 Poin

Dipost 2 tahun yang lalu

Belum ada Jawaban. Jadi yang pertama Jawaban

Login untuk ikut Jawaban