• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Message: Undefined index: page_description

#1
[eluser]echo[/eluser]
Hello guys and girls Smile
I'm newbie in Codeigniter.

I have table: pages
And when I want to display data from table, I get this error and I don't know where is my error.

Any suggestions will help me.

Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined index: page_description

Filename: core/Loader.php(829) : eval()'d code

Line Number: 10

" />

page_view.php

Code:
<!DOCTYPE html>
&lt;html lang="en"&gt;
  &lt;head&gt;
    &lt;meta charset="utf-8"&gt;
    &lt;title&gt;&lt;?=$pages_info['page_title'];?&gt;&lt;/title&gt;
    &lt;meta name="viewport" content="width=device-width, initial-scale=1.0" /&gt;
    &lt;meta name="description" content="&lt;?=$pages_info['page_description'];?&gt;" /&gt;
    &lt;meta name="keywords" content="&lt;?=$pages_info['page_keywords'];?&gt;" /&gt;

pages_model.php

Code:
&lt;?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Pages_model extends CI_Model {

    function get_pages()
    {
        $this->db->where('page_hidden', 'no');
        $query = $this->db->get('pages');
        return $query->result_array();      
    }
    
    function get_pages_info($page_title)
    {
        $this->db->where('page_title_en', $page_title);
        $query = $this->db->get('pages');
        return $query->row_array();
    }

}

?&gt;

controllers/pages.php

Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Pages extends CI_Controller {

    function index()
    {
        redirect(base_url());
    }

    function page($page_title)
    {
        $data['pages'] = $this->pages_model->get_pages();
        $data['pages_info'] = $this->pages_model->get_pages_info($page_title);
        $name = 'page';
        $this->template->page_view($data, $name);
    }
    

}

?&gt;

#2
[eluser]PhilTem[/eluser]
You're missing the most important part: the controller's code. Please post this as well because probably no-one can see the error without looking into your controller Wink

#3
[eluser]echo[/eluser]
Thanks PhilTem for quick reply Wink

#4
[eluser]CroNiX[/eluser]
And there is a 'page_description' column in the 'pages' table?

#5
[eluser]echo[/eluser]
[quote author="CroNiX" date="1349394767"]And there is a 'page_description' column in the 'pages' table?[/quote]

Yes,

table: pages

page_ID
page_title
page_description
page_keywords
page_content

#6
[eluser]C. Jiménez[/eluser]
I'm used to check if my DB driver return anything or not before sending the data to controller.
So i can handle that excepcion without having a error page in frontend.

Example:
Code:
function get_pages_info($page_title)
    {
        $this->db->where('page_title_en', $page_title);
        $query = $this->db->get('pages');
        return $query->row_array();
    }
changed to:
Code:
function get_pages_info($page_title)
    {
        $this->db->where('page_title_en', $page_title);
        $query = $this->db->get('pages');
        if (!empty($query) && $query->num_rows() > 0) return $query->row_array();
        return false;
    }
So now in your controller can handle $variable === FALSE if your query failed or your recordset is empty.

#7
[eluser]PhilTem[/eluser]
You could make that code better readable by using

Code:
return $query->num_rows() ? $query->row_array() : FALSE;

You don't need to check for empty($query) since it will always be different from NULL if your query was ran successfully (i.e. there were no MySQL-syntax errors)


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.