phpmysqlcodeignitermodel-view-controller

Codeigniter View Undefined Variable Error


I have a program that used to manage items in a store. Everything is working fine. Then I need to get a report referred to update_stock_id for issued items.

Tried the following code

Controller

class Reports extends FZ_Controller
{

    function __construct()
    {
        parent::__construct();
        $this->load->library('form_validation');

        $this->load->model('Report_model');

    }
public function printIssuedItems($id)
    {
        $data['issuedDetail'] = $this->Report_model->printIssuedItemData($id);
        $this->load->view('reports/printIssuedItems', $data);
    }
}

Model

function printIssuedItemData($id)
    {
        $this->db->select('store_update_stock.request_no, store_update_stock.billed_date, tbl_user.username,store_item.item_name,
        sum(store_update_stock_details.r_qty) as r_qty, sum(store_update_stock_details.ap_qty) as ap_qty,
        sum(store_update_stock_details.is_qty) as is_qty');
        $this->db->from('store_update_stock');
        $this->db->join('store_update_stock_details', 'store_update_stock.update_stock_id=store_update_stock_details.update_stock_id');
        $this->db->join('tbl_user', 'store_update_stock.supplier=tbl_user.userId');
        $this->db->join('store_item', 'store_update_stock_details.item=store_item.item_id', 'right');
        $this->db->where(array('store_update_stock.update_stock_id' => $id, 'store_update_stock_details.status' => 1));
        $this->db->group_by('store_update_stock_details.item');

        $q = $this->db->get();
        if ($q->num_rows() > 0) {
            return $q->result();
        }
        return false;


    }

View (Relevant part of)

<?php
if (!empty($issuedDetail)) {
    $issuedData = $issuedDetail[0];
}
?>

<div class="col-xs-12">
            <h2 class="page-header">

                <small class="text-center"> Request No: <b><?=$issuedData->request_no?></b>&nbsp;&nbsp;&nbsp;&nbsp;
                Request Date: <b><?=$issuedData->billed_date?></b>&nbsp;&nbsp;&nbsp;&nbsp;
                Officer Name: <b><?=$issuedData->username?></b></small>
            </h2>
     </div>

The model outs the correct result set. But executing the view, the following error message displays.

A PHP Error was encountered
Severity: Notice
Message: Undefined variable: issuedData
Filename: reports/printIssuedItems.php
Line Number: 52

Backtrace:

File:
C:\xampp\htdocs\wpsstores\application\views\reports\printIssuedItems.php
Line: 52
Function: _error_handler

This refers to the variable $issuedData.

I have checked every part of the code. But didn't identify any issue.


Solution

  • in order to avoid the undefined variable error in your view, you need to exclude your html code from being executed, in case the database query doesn't return any results.

    change:

    <?php
    if (!empty($issuedDetail)) {
        $issuedData = $issuedDetail[0];
    }
    ?>
    YOUR HTML CODE
    

    to:

    <?php
    if (!empty($issuedDetail)):
        $issuedData = $issuedDetail[0];
    ?>
    YOUR HTML CODE
    <?php else:?>no records found
    <?php endif?>
    

    this will show html code if query returns records, otherwise "no recs" message