1

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.

8
  • what shows print_r($issuedDetail)? Commented Nov 5, 2019 at 16:46
  • @ Vickel. Where to add print_r($issuedDetail) Commented Nov 5, 2019 at 16:58
  • this is a method to debug, use print_r($data['issuedDetail']));die; in your controller and see what the array output is (it will stop the script there!), then you can check if the array has any values and how it is structured, with that you can then "feed" your view. Here you have already !empty check, but it doesn't do any exemption, if array is empty... Commented Nov 5, 2019 at 17:17
  • @ Vickel. If used, print_r($data['issuedDetail']));die; in my controller, doesn't out anything. Commented Nov 5, 2019 at 17:26
  • because it is empty, you need to debug your $this->Report_model->printIssuedItemData($id);I suppose $id is undefined Commented Nov 5, 2019 at 17:29

1 Answer 1

1

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

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

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.