0

So im trying to make a detail page of a product, linked from lists of products. I already configured the routes. But the page says error with undefined foreach variable.

This is the error:

A PHP Error was encountered
Severity: Notice

Message: Undefined variable: listproduk

Filename: user/view.php

Line Number: 28

Backtrace:

File: E:\xampp\htdocs\emedco\application\views\user\view.php
Line: 28
Function: _error_handler

File: E:\xampp\htdocs\emedco\application\controllers\ProdukList.php
Line: 19
Function: view

File: E:\xampp\htdocs\emedco\index.php
Line: 315
Function: require_once

detail page, view.php

<div class="container-fluid">
   <h4>Detail</h4>
   <h2><?php echo $listproduk['nama_produk']; ?></h2> <!--line 28-->
   <p><?php echo $listproduk['deskripsi_produk']; ?></p>
</div>

view of product list (previous page before detail view, no error), produklist.php

<?php foreach ($produk as $listproduk) : ?>
    <div class="col-6 col-md-4 col-xl-3 mb-4 ">
        <div class="card text-center shadow h-100 py-2">
            <img class="card-img-top" src="<?php echo base_url('images/product/' . $listproduk['img_produk']) ?>" width="100" alt="Card image cap">
            <div class="card-body">
                <h6 class="card-title"><?php echo $listproduk['nama_produk']; ?></h6>
                <p style="font-size:80%;" class="card-text">Kategori obat</p>
                <p style="font-size:80%;" class="card-text">Rp. <?php echo $listproduk['harga_produk']; ?></p>
            </div>
            <div class="card-footer">
                <a href="<?php echo site_url('ProdukList/'.$listproduk['kode_produk']); ?>" class="btn btn-primary btn-block btn-sm">Detail</a>
            </div>
        </div>
    </div>
<?php endforeach; ?>

controller, ProdukList.php

<?php

class ProdukList extends CI_Controller
{
    public function index()
    {
        $data['produk'] = $this->ProdukList_model->get_produk();
        $this->load->view('user/produklist', $data);
    }

    public function view($kode_produk = NULL)
    {
        $data['produkdet'] = $this->ProdukList_model->get_produk($kode_produk);

        if (empty($data['produkdet'])) {
            show_404();
        }

        $this->load->view('user/view', $data); <!--line 19-->

    }
}

tried to add true after $data, to be $this->load->view('user/view', $data, true); and it shows blank white page.

model ProdukList_model.php

<?php

class ProdukList_model extends CI_Model
{
    public function __construct() {
        $this->load->database();
    }

    public function get_produk($kode_produk = FALSE)
    {
        if($kode_produk === FALSE)
        {
            $query = $this->db->get('tb_produk');
            return $query->result_array();
        }
        $query = $this->db->get_where('tb_produk', array('kode_produk' => $kode_produk));
        return $query->row_array();
    }
}

routes.php

$route['ProdukList/(:any)'] = 'ProdukList/view/$1';
$route['default_controller'] = 'Dashboard';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

autoload.php

$autoload['model'] = array('ProdukList_model');

What's the problem?

3 Answers 3

1

please see in your controller that what you are sending to the view. your are sending data['produk'] in ur conttroller index function and in view u are adding 'listproduk' instead of 'produc'; try this "

Sign up to request clarification or add additional context in comments.

Comments

1

I think you should have $produkdet['nama_produk'] instead of $listproduk['nama_produk'] on line 28 in the view. The same on the next line.

Comments

0

The Error message is...

A PHP Error was encountered Severity: Notice

Message: Undefined variable: listproduk

Filename: user/view.php

Line Number: 28

So looking at what would appear to be line 28 in your user/view.php file you have...

<h2><?php echo $listproduk['nama_produk']; ?></h2> <!--line 28-->

So what you would do now is look at where you are passing in the variables into your view.

In your ProdukList.php Controller you have

public function view($kode_produk = NULL)
{
   $data['produkdet'] = $this->ProdukList_model->get_produk($kode_produk);

   if (empty($data['produkdet'])) {
     show_404();
   }

   $this->load->view('user/view', $data); <!--line 19-->
}

In the above, you can see that you are assigning the variable $data['produket'], to be passed into your view. This would become $produket, which is an array, in your view.

But your view is expecting an array called $data['listproduk'], which would result in an called $listproduk inside the view.

So the error message is clearly indicating that you are either not passing in the correct value to the view, or the variable in the view is wrong, or it's the wrong view... Either way, it's not correct.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.