Home > Mobile >  An uncaught Exception was encountered Error
An uncaught Exception was encountered Error

Time:01-30

An uncaught Exception was encountered

Type: Error

Message: Call to a member function num_rows() on bool

Filename: C:\xampp\htdocs\sikan_v2\application\views\transaction\sale\cart_data.php

Line Number: 2

Backtrace:

File: C:\xampp\htdocs\sikan_v2\application\views\transaction\sale\sale_form.php
Line: 134
Function: view

File: C:\xampp\htdocs\sikan_v2\application\libraries\Template.php
Line: 14
Function: view

File: C:\xampp\htdocs\sikan_v2\application\controllers\Sale.php
Line: 26
Function: load

File: C:\xampp\htdocs\sikan_v2\index.php
Line: 315
Function: require_once

My Controllers: controllers/sale.php

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Sale extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        check_not_login();
        check_admin();
        $this->load->model(['sale_m','item_m', 'supplier_m', 'stock_m']);
    
    } 

    public function index()
    {
        $this->load->model(['customer_m', 'item_m']);
        $customer = $this->customer_m->get()->result();
        $item = $this->item_m->get()->result();
        $cart = $this->sale_m->get_cart();
        $data = array(
            'customer' => $customer,
            'item' => $item,
            'cart' => $cart,
            'invoice' => $this->sale_m->invoice_no(),
        );
        $this->template->load('template', 'transaction/sale/sale_form', $data);
    }

    public function process()
    {
        $data = $this->input->post(null, TRUE);

        if(isset($_POST['add_cart'])) {
            $this->sale_m->add_cart($data);
        }

        if($this->db->affected_rows() > 0) {
            $params = array("success" => true);
        } else {
            $params = array("success" => false);
        }
        echo json_encode($params);
    }

}

My Models: models/Sale_m.php

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Sale_m extends CI_Model {

    public function invoice_no()
    {
        $sql = "SELECT MAX(MID(invoice,9,4)) AS  invoice_no
            FROM t_sale
            WHERE MID(invoice,3,6) = DATE_FORMAT(CURDATE(), '%y%m%d')";
        $query = $this->db->query($sql);
        IF($query->num_rows() > 0) {
            $row = $query->row();
            $n = ((int)$row->invoice_no)   1;
            $no = sprintf("%'.04d", $n);
        }else{
            $no = "0001";
        }
        $invoice = "MP".date('ymd').$no;
        return $invoice;
        $query = $this->db->get();
        return $query;
    }

    public function get_cart($params = null)
    {
        $this->db->select('*, p_item.name as item_name, t_cart.price as cart_price');
        $this->db->from('cart');
        $this->db->join('p_item', 't_cart.item_id = p_item.item_id');
        if($params != null) {
            $this->db->where($params);
        }
        $this->db->where('user_id', $this->session->userdata('userid'));
        $query = $this->db->get();
        return $query;
    }



    public function add_cart($post) 
    {
        $query = $this->db->query("SELECT MAX(cart_id) AS cart_no FROM t_cart");
        if($query->num_rows() > 0) {
            $row = $query->row();
            $car_no = ((int)$row->cart_no)   1;
        } else {
            $car_no = "1";
        }

        $params = array(
            'cart_id' => $car_no,
            'item_id' => $post['item_id'],
            'price' => $post['price'],
            'qty' => $post['qty'],
            'total' => ($post['price'] * $post['qty']),
            'user_id' => $this->session->userdata('userid')
        );
        $this->db->insert('t_cart', $params);
    }

    

}

My View: transaction/sale/cart_data.php

<?php $no = 1;
if($cart->num_rows() > 0) {
    foreach ($cart->result() as $c =>$data) { ?>
        <tr>
            <td><?=$no  ?>.</td>
            <td><?=$data->barcode?></td>
            <td><?=$data->item_name?></td>
            <td ><?=$data->cart_price?></td>
            <td ><?=$data->qty?></td>
            <td ><?=$data->discount_item?></td>
            <td id="total"><?=$data->total?></td>
            <td  width="160px">
            <button id="update_cart" data-toggle="modal" data-target="#modal-item-edit"
            data-cartid="<?=$data->cart_id?>"
            data-barcode="<?=$data->barcode?>"
            data-product="<?=$data->item_name?>"
            data-price="<?=$data->cart_price?>"
            data-qty="<?=$data->qty?>"
            data-discount="<?=$data->discount_item?>"
            data-total="<?=$data->total?>"
            >
            <i ></i> Update
            </button>
            <button id="del_cart" data-cartid="<?=$data->cart_id?>" >
                <i ></i> Delete
            </button>
            </td>
        </tr>
    <?php
    }
} else { 
    echo '<tr>
    <td colspan="8"> >Tidak ada item</td>
    </tr>';
} ?>

please help me solve the error

CodePudding user response:

It will return bool when there's any errors in your query. Check this function again:

public function get_cart($params = null)
{
    $this->db->select('*, p_item.name as item_name, t_cart.price as cart_price');
    $this->db->from('cart');
    $this->db->join('p_item', 't_cart.item_id = p_item.item_id');
    if($params != null) {
        $this->db->where($params);
    }
    $this->db->where('user_id', $this->session->userdata('userid'));
    $query = $this->db->get();
    return $query;
}

I think it should be:

$this->db->from('t_cart');

If you check again and find no errors, you can use method: last_query to get the query then try to query directly in phpmyadmin or MYSQL Workbench to check if there's any errors. Like this:

public function get_cart($params = null)
{
    // above source code
    $query = $this->db->get();
    return $this->db->last_query();
}

CodePudding user response:

You can do a

var_dump($data)

before calling this line to see if the 'cart' gets what you expect.

$this->template->load('template', 'transaction/sale/sale_form', $data);

My guess is that you are missing the result() at the end:

return $this->db->query("<your query>")->result();
  • Related