CodeIgniter Forums
Undefined Index - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Undefined Index (/showthread.php?tid=63963)



Undefined Index - mpar612 - 12-28-2015

Hi Everyone,

I am new to Codeigniter and am working on my first project and am getting an Undefined Index error and am not sure why. I have been modeling my code after the News example on the Codeigniter website.

My code is posted below, but it seems that the following line in my controller is not passing the $id to the query in the model, because anytime I try to reference the output of this query (e.g. $data['title'] = $data['family']['familyName']) I get:
Quote:Message: Undefined index: familyName
.
PHP Code:
$data['family'] = $this->heart_model->get_families($id); 


My model is:

PHP Code:
<?php
class Heart_model extends CI_Model {

    public function 
__construct()
    {
        
$this->load->database();
    }
    
    public function 
get_families($id FALSE)
    {
            
$this->db->order_by('familyName''ASC');
            
$query $this->db->get('families');
            return 
$query->result_array();
    } 


My Controller is:

PHP Code:
<?php
class Heart extends CI_Controller {
    
    public function 
__construct()
    {
        
parent::__construct();
        
$this->load->model('heart_model');
        
$this->load->helper('url_helper');
    }
    
    public function 
index()
    {
        
$data['heart'] = $this->heart_model->get_families();
        
$data['title'] = 'Heritage Heart';

        
$this->load->view('templates/header'$data);
        
$this->load->view('heart/index'$data);
        
$this->load->view('templates/footer');
    }
    
    public function 
view($id NULL)
    {
        
$data['family'] = $this->heart_model->get_families($id);
            
        if (empty(
$data['family']))
        {
            
show_404();
        }
        
        
$data['title'] = $data['family']['familyName'];
        
        
$this->load->view('templates/header'$data);
        
$this->load->view('heart/view'$data);
        
$this->load->view('templates/footer');
    } 

I appreciate any help. Also, any advice on how I can troubleshoot these types of problems on my own would be appreciated. Thanks!


RE: Undefined Index - dhinakar - 12-28-2015

reasons,
1.model not return value for particular index
2.table not contain that index
3.or any spelling errors are occur on script


RE: Undefined Index - pdthinh - 12-28-2015

(12-28-2015, 08:24 PM)mpar612 Wrote: Hi Everyone,

I am new to Codeigniter and am working on my first project and am getting an Undefined Index error and am not sure why. I have been modeling my code after the News example on the Codeigniter website.

My code is posted below, but it seems that the following line in my controller is not passing the $id to the query in the model, because anytime I try to reference the output of this query (e.g. $data['title'] = $data['family']['familyName']) I get:
Quote:Message: Undefined index: familyName
.
PHP Code:
$data['family'] = $this->heart_model->get_families($id); 


My model is:

PHP Code:
<?php
class Heart_model extends CI_Model {

 public function 
__construct()
 {
 
$this->load->database();
 }
 
 public function 
get_families($id FALSE)
 {
 
$this->db->order_by('familyName''ASC');
 
$query $this->db->get('families');
 return 
$query->result_array();
 } 


My Controller is:

PHP Code:
<?php
class Heart extends CI_Controller {
 
 public function 
__construct()
 {
 
parent::__construct();
 
$this->load->model('heart_model');
 
$this->load->helper('url_helper');
 }
 
 public function 
index()
 {
 
$data['heart'] = $this->heart_model->get_families();
 
$data['title'] = 'Heritage Heart';

 
$this->load->view('templates/header'$data);
 
$this->load->view('heart/index'$data);
 
$this->load->view('templates/footer');
 }
 
 public function 
view($id NULL)
 {
 
$data['family'] = $this->heart_model->get_families($id);
 
 if (empty(
$data['family']))
 {
 
show_404();
 }
 
 
$data['title'] = $data['family']['familyName'];
 
 
$this->load->view('templates/header'$data);
 
$this->load->view('heart/view'$data);
 
$this->load->view('templates/footer');
 } 

I appreciate any help. Also, any advice on how I can troubleshoot these types of problems on my own would be appreciated. Thanks!

The result_array() method will return an numeric index array that each element is a record in database. In case you want to get a specific row you should use row_array() instead. For example:

PHP Code:
public function get_families($id FALSE)
{
 
   $this->db->order_by('familyName''ASC');
 
   if ($id !== FALSE)
 
       $this->db->where('id'$id);
 
   $query $this->db->get('families');
 
   
    return $id 
!== FALSE $query->row_array() : $query->result_array();


However, I recommend you seperating get_all_families with get_family_by_id for simplicity.
PHP Code:
public function get_family_by_id($id)
{
 
   $this->db->order_by('familyName''ASC');
 
   $this->db->where('id'$id);
 
   $query $this->db->get('families');
 
   
    return $query
->row_array();




RE: Undefined Index - mpar612 - 12-29-2015

pdthinh - worked perfectly. Thank you so much!


RE: Undefined Index - pdthinh - 12-29-2015

(12-29-2015, 06:06 PM)mpar612 Wrote: pdthinh - worked perfectly. Thank you so much!

You're welcome.