1

I really need help . i don't know to solve Undefined variable: chart

ERROR

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: chart

Filename: shop/header.php

Line Number: 242

My Controller

function mychart($id_user)
{
    $where = array('id_user' => $this->session->userdata('id_user'));
    $datachart['chart'] = $this->m_product->tampil_chart($where,'chart')->result();
    $this->load->view('shop/header',$datachart);
}

My Model

function tampil_chart($where,$table)
{
    $datachart =  $this->db->get_where($table,$where);
    return $datachart->result();
}

View(shop/header)

<div class="header-cart-content flex-w js-pscroll">
            <?php foreach($chart as $a){
      ?>
            <ul class="header-cart-wrapitem w-full">
                <li class="header-cart-item flex-w flex-t m-b-12">
                    <div class="header-cart-item-img">
                        <img src="<?php echo base_url() ?>assets/user/images/item-cart-01.jpg" alt="IMG">
                    </div>

                    <div class="header-cart-item-txt p-t-8">
                        <a href="#" class="header-cart-item-name m-b-18 hov-cl1 trans-04">
                            <?php echo $a->id_product ?>
                        </a>

                        <span class="header-cart-item-info">
                            <?php echo $a->jumlah ?>
                        </span>
                    </div>
                </li>

            </ul>  <?php } ?>
Pradeep
  • 9,667
  • 13
  • 27
  • 34

2 Answers2

0

You must check the "chart" variable is defined at the view file before to iterate it.

For this example you must do it 241 line of shop/header.php:

<div class="header-cart-content flex-w js-pscroll">
            <?php 
            if (isset($chart)):
            foreach($chart as $a){?>
            <ul class="header-cart-wrapitem w-full">
                <li class="header-cart-item flex-w flex-t m-b-12">
                    <div class="header-cart-item-img">
                        <img src="<?php echo base_url() ?>assets/user/images/item-cart-01.jpg" alt="IMG">
                    </div>

                    <div class="header-cart-item-txt p-t-8">
                        <a href="#" class="header-cart-item-name m-b-18 hov-cl1 trans-04">
                            <?php echo $a->id_product ?>
                        </a>

                        <span class="header-cart-item-info">
                            <?php echo $a->jumlah ?>
                        </span>
                    </div>
                </li>

            </ul>  <?php } endif; ?>
Mesuti
  • 878
  • 1
  • 13
  • 29
0

Remove ->result() from mychart controller because you already have in model tampil_chart ;

Replace it :

$datachart['chart'] = $this->m_product->tampil_chart($where,'chart')->result();

with :

$datachart['chart'] = $this->m_product->tampil_chart($where,'chart');

Your controller should be like this :

function mychart($id_user)
{
    $where = array('id_user' => $this->session->userdata('id_user'));
    $datachart['chart'] = $this->m_product->tampil_chart($where,'chart');
    $this->load->view('shop/header',$datachart);
}
Pradeep
  • 9,667
  • 13
  • 27
  • 34