Welcome Guest, Not a member yet? Register   Sign In
[RESOLVED] Error was encountered - Undefined Index
#1

[eluser]IamPrototype[/eluser]
Hey, I got this code and I would like to display all of my entries on my view page.

browse.php (view)
Code:
<h2>Browse All Entries</h2>
<h3>&lt;?=$entry['title']?&gt;</h3>
<p>&lt;?=$entry['body']?&gt;</p>

entries.php (controller)
Code:
&lt;?php

    class Entries extends Controller {
        
        function __construct()
        {
            parent::Controller();
            
            $this->table                 = "entries";
            $this->template['module']     = "entries";
            
            $this->load->model('Entries_Model', 'entries', TRUE);
        }
        
        function _run($view)
        {
            $this->template['view'] = $view;
            
            $this->load->vars($this->template);
            $this->load->view('loader');
        }
        
        function index()
        {    
            $this->template['entry'] = $this->entries->browse();
            $this->_run('browse');
        }
        
    }

?&gt;

Entries_Model.php (model)
Code:
&lt;?php

    class Entries_Model extends Model {
        
        function __construct()
        {
            parent::Model();
            
            $this->table = "entries";
        }
        
        function browse()
        {
            $this->db->where('active', TRUE);
            $this->db->order_by('id', 'desc');
            
            $query = $this->db->get($this->table);
            
            if ($query->num_rows() == 1)
            {
                return $query->result_array();
            }
            else
            {
                return FALSE;
            }
        }
        
    }

?&gt;

When I go online I get this error.
Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined index: title

Filename: entries/browse.php

Line Number: 2

I tried to use
Code:
die(print_r($entry))
in my view and it says I got my data.. but I can't figure out how to show it. Sad Any kind soul out there?

Greetings!
#2

[eluser]pistolPete[/eluser]
What's your database layout?
Why do you limit the model to only return data if one entry was found? In your view you say "Browse All Entries"...

Code:
if ($query->num_rows() == 1)
{
    return $query->result_array();
}

If you truly only want one result, then use row_array() instead of result_array().
#3

[eluser]IamPrototype[/eluser]
That was a mistake.. Thanks!

I changed it to
Code:
if ($query->num_rows() > 0)
            {
                return $query->result_array();
            }

Anyways, would you help me fixing my view? I tried for one hour now, lol... I'm so tired!

** EDIT: I got it working, I forgot my "foreach". Smile




Theme © iAndrew 2016 - Forum software by © MyBB