codeigniter-3

Duplicated foreach in CI3


so I'm trying to use foreach to shows data but it duplicates the things inside the foreach. Here's the code

<!-- Begin Page Content -->
<div class="container-fluid">
    <?= $this->session->flashdata('pesan'); ?>
    <div class="row">
        <div class="col-lg-6">
            <?php if (validation_errors()) {
                $this->session->set_flashdata('pesan', '<div class="alert alert-danger alert-message" role="alert">Nama Kategori tidak boleh Kosong</div>');
                redirect('buku/ubahkategori/' . $k['id_kategori']);
            } ?>
            <?php foreach ($kategori as $k) { ?>
                <form action="<?= base_url('buku/ubahKategori'); ?>" method="post">
                    <div class="form-group">
                    <input type="hidden" name="id" id="id" value="<?php echo $kategori['id_kategori']; ?>">
                    <input type="text" class="form-control form-control-user" id="kategori" name="kategori" placeholder="Masukkan Kategori Buku" value="<?= $kategori['kategori']; ?>">
                    </div>
                    <div class="form-group">
                        <input type="button" class="form-control form-control-user btn btn-dark col-lg-3 mt-3" value="Kembali" onclick="window.history.go(-1)">
                        <input type="submit" class="form-control form-control-user btn btn-primary col-lg-3 mt-3" value="Update">
                    </div>
                </form>
            <?php } ?>

And here's the controller

 public function ubahKategori()
    {
        $data['judul'] = 'Ubah Kategori Buku';
        $data['user'] = $this->ModelUser->cekData(['email' => $this->session->userdata('email')])->row_array();

        $where = ['id_kategori' =>  $this->uri->segment(3)];
        $data['kategori'] = $this->ModelBuku->kategoriWhere($where)->row_array();
        
        
        $this->form_validation->set_rules(
            'kategori', 
            'Kategori',
            'required', [
                'required' => 'Judul Buku harus diisi'
        ]);

        if ($this->form_validation->run() == false) 
        {
            $this->load->view('templates/header', $data);
            $this->load->view('templates/sidebar', $data);
            $this->load->view('templates/topbar', $data);
            $this->load->view('buku/ubah_kategori', $data);
            $this->load->view('templates/footer');
        } else {
            $data = ['kategori' => $this->input->post('kategori')];
            $this->ModelBuku->updateKategori(['id_kategori' => $this->input->post('id')], $data);
            redirect('buku/kategori');
        }
    }

It duplicates the things inside of the foreach, tried to use Limit on the query but it still doesn't works

Tried to add a limit


Solution

  • If I understand correctly, you're trying to update one category in a form. You get this category from the database with ->row_array(), so $data['kategory'] contains a single database row/the properties of a single category.

    If you then do a foreach on this category, the foreach loops over the category's properties and repeats the form for each property. Since you only want to display one category, you do not need to use foreach here, you can just access the properties directly.

    Remove the foreach and also replace the $k['id_kategori'] in the redirect with $kategori['id_kategori']:

    <!-- Begin Page Content -->
    <div class="container-fluid">
        <?= $this->session->flashdata('pesan'); ?>
        <div class="row">
            <div class="col-lg-6">
                <?php if (validation_errors()) {
                    $this->session->set_flashdata('pesan', '<div class="alert alert-danger alert-message" role="alert">Nama Kategori tidak boleh Kosong</div>');
                    redirect('buku/ubahkategori/' . $kategori['id_kategori']);
                } ?>
    
                    <form action="<?= base_url('buku/ubahKategori'); ?>" method="post">
                        <div class="form-group">
                        <input type="hidden" name="id" id="id" value="<?php echo $kategori['id_kategori']; ?>">
                        <input type="text" class="form-control form-control-user" id="kategori" name="kategori" placeholder="Masukkan Kategori Buku" value="<?= $kategori['kategori']; ?>">
                        </div>
                        <div class="form-group">
                            <input type="button" class="form-control form-control-user btn btn-dark col-lg-3 mt-3" value="Kembali" onclick="window.history.go(-1)">
                            <input type="submit" class="form-control form-control-user btn btn-primary col-lg-3 mt-3" value="Update">
                        </div>
                    </form>