1

this is inside my controller

if ( $training_code == null || $batch_no == null)
        {
            $data = $this->_public_data();
            $no_reg = $this->sys_model->get_registration($this->session->userdata('admin'))->no_reg;
            $data['trainingl_list']= $this->sys_model->list_training_by_user($no_reg);
        }

this is inside my model

function list_training_by_user($no_reg)
{
    $this->db->where('no_reg', $no_reg);
    return $this->db->get('tbl_peserta_training')->row(); 
}

and this is my view

<?php foreach($trainingl_list as $row){
echo $row->kd_training; //<-- this is 13th line
echo $row->no_batch; //<-- this is 19th line
?>

and it got

Severity: Notice
Message: Trying to get property of non-object
Line Number: 13, 19

if i just echo $noreg (by putting it into $data['noreg']=$no_reg) inside my view, it has no error, but with $training_list array, and put it into foreach in my view, i got those error am I missing something?

tereško
  • 58,060
  • 25
  • 98
  • 150
Henry J
  • 344
  • 2
  • 4
  • 14

2 Answers2

1

Is $training_list coming from list_training_by_user()? If so, list_training_by_user() is returning only a single row, and not a list of rows. Therefore, iterating over that row is not effective. You might have to try something like this:

function list_training_by_user($no_reg)
{
    $this->db->where('no_reg', $no_reg);
    $result = $this->db->get('tbl_peserta_training');
    $results = array();
    while ($row = $result->row()) {
      $results[] = $row;
    }
    return $results;
}

EDIT

Better yet, you should be returning result(), which seems to be an instance of Iterator:

function list_training_by_user($no_reg)
{
    $this->db->where('no_reg', $no_reg);
    return $this->db->get('tbl_peserta_training')->result(); 
}

EDIT 2

Due to the memory issues with my first example and the result() method (see comments), we should only load one result at a time. You'll have to just return the query and fetch one row at a time from that object.

function list_training_by_user($no_reg)
{
    $this->db->where('no_reg', $no_reg);
    return $this->db->get('tbl_peserta_training'); 
}

And then in your view:

$results = $this->sys_model->list_training_by_user($no_reg);
while ($row = $results->row()) {
  echo $row->kd_training; //<-- this is 13th line
  echo $row->no_batch; //<-- this is 19th line
}

Unfortunately, the CodeIgnitor result() method loads all results into a single array much like my first solution did. Of course, this is not memory efficient if you have any sizable number of results. Each row needs to be lazy loaded. If you want a beautiful alternative, you can write a class that implements the Iterator interface to automagically lazy load result rows while you iterate over results in a foreach loop, and I can show you what that would look like if you want.

kuujo
  • 7,785
  • 1
  • 26
  • 21
  • BTW, if you haven't already, check out [this documentation](http://ellislab.com/codeigniter/user-guide/database/results.html) for more methods. – kuujo Jun 16 '13 at 10:35
  • the first one got `Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 36 bytes) in C:\Program Files\xampp\htdocs\sys\etraining\models\sys_model.php on line 142`, and the second one got same error :s – Henry J Jun 16 '13 at 10:38
  • Hmm... I can see why you would get that with the first, but with the second I would assume that the CodeIgnitor library lazy loads the results. I'm surprised. – kuujo Jun 16 '13 at 10:42
  • Ahh... according to the documentation the `result()` method does essentially the exact same thing as my first code example. I'm not sure why they would do that. I've built libraries for my own projects that lazy load results in an iterator. What's happening here is there are too many results being loaded into memory. I'll edit my post to show you another solution. – kuujo Jun 16 '13 at 10:44
  • with updates of EDIT 2, i;m afraid it was not ablee to complete, as $no_reg are undefined... :| – Henry J Jun 16 '13 at 11:35
  • That `$no_reg` variable comes from the first code block in your question. Simply add that to the top of the code and it will be defined, as it is defined by the `$no_reg = $this->sys_model->get_registration($this->session->userdata('admin'))->no_reg;` line. – kuujo Jun 16 '13 at 11:53
  • Sorry, I can't give you a complete example of which variables were defined where unless you give me a complete example of your code, but I used all the same variables you had defined in your code for clarity. It's up to you to put the code blocks together within the framework of your application. – kuujo Jun 16 '13 at 11:55
0

In model :

function list_training_by_user($no_reg)
{
   $this->db->where('no_reg', $no_reg);
   $result=$this->db->get('tbl_peserta_training'); 
   return $result->$result_array();

}

controller

$data=$this->model_name->list_training_by_user($no_reg);
$this->load->view("your_view_name",array('trainingl_list'=>$data));

and in the view use it

<?php
foreach($trainingl_list as $row){
   echo $row['kd_training']; //<-- this is 13th line
   echo $row['no_batch']; //<-- this is 19th line
}
?>

or

In model :

function list_training_by_user($no_reg)
{
   $this->db->where('no_reg', $no_reg);
   $result=$this->db->get('tbl_peserta_training'); 
   return $result->result();

}

controller : same as earliar;

and in the view use it

foreach($trainingl_list as $row){
   echo $row->kd_training; //<-- this is 13th line
   echo $row->no_batch; //<-- this is 19th line
}
?>
sharif2008
  • 2,716
  • 3
  • 20
  • 34
  • We went through that (see the comments on my answer). Apparently the `result()` and `result_array()` methods load *all* the results into memory, which causes a memory limit error with large result sets. See [Generating Query Results](http://ellislab.com/codeigniter/user-guide/database/results.html) from the CodeIgnitor documentation. _This function returns the query result as an array of objects..._ (not an iterator which, in my opinion, it should). There may be a better way within that framework to do this without memory issues, I'm just not that familiar with CodeIgnitor. – kuujo Jun 16 '13 at 13:27