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.

Vickel
  • 7,879
  • 6
  • 35
  • 56
mcode
  • 87
  • 10
  • what shows print_r($issuedDetail)? – Vickel Nov 05 '19 at 16:46
  • @ Vickel. Where to add print_r($issuedDetail) – mcode Nov 05 '19 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... – Vickel Nov 05 '19 at 17:17
  • @ Vickel. If used, print_r($data['issuedDetail']));die; in my controller, doesn't out anything. – mcode Nov 05 '19 at 17:26
  • because it is empty, you need to debug your `$this->Report_model->printIssuedItemData($id);`I suppose $id is undefined – Vickel Nov 05 '19 at 17:29
  • @ Vickel. But $id is defined in the model. – mcode Nov 05 '19 at 17:36
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/201896/discussion-between-vickel-and-mcode). – Vickel Nov 05 '19 at 17:42
  • Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-warning-undefined-arr) – Nico Haase Apr 06 '23 at 13:09

1 Answers1

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

Vickel
  • 7,879
  • 6
  • 35
  • 56