Welcome Guest, Not a member yet? Register   Sign In
Query result error undefined variable
#1

Hi im not quite sure whats going on with the query i done but i get an error when trying to retrieve information for a single page. it works perfectly if i get the information by print_r but cannot parse it to the page using

A PHP Error was encountered
Severity: Notice
Message: Undefined variable: data
Filename: views/view.php
Line Number: 6

// View
echo '<h2>' . $data['title'] . '</h2>';
echo $page_content['content'];
print_r($page_content);

// Even tried
echo '<h2>' . $data->title . '</h2>';
echo $page_content->content;
print_r($page_content);


// Heres the result if i print_r
Array ( [0] => Array ( [page_id] => 1 [title] => My New Title [content] => lots of content here... [created] => 0000-00-00 00:00:00 ) )

// Controller
public function view()
{
$data['page_content'] = $this->page_model->get($id);

if (empty($data['page_content']))
{
redirect();
}

$this->load->view('template/header');
$this->load->view('view', $data);
$this->load->view('template/footer');
}


// Model
public function get($id = null, $order_by = null)
{
if(is_numeric($id)){
$this->db->where($this->_primary_key, $id);
}
if(is_array($id)){
foreach ($id as $_key => $_value){
$this->db->where($_key, $_value);
}
}
$q = $this->db->get($this->_table);
return $q->result_array();
}


Any ideas on what ive done wrong.

Cheers
Chris
Reply
#2

(08-23-2015, 02:29 AM)skoobi Wrote: A PHP Error was encountered

Severity: Notice
Message: Undefined variable: data
Filename: views/view.php
Line Number: 6

// Controller
public function view()
   {
       $data['page_content'] = $this->page_model->get($id);

       if (empty($data['page_content']))
       {
               redirect();
       }

       $this->load->view('template/header');
$this->load->view('view', $data);
$this->load->view('template/footer');
   }

When you pass $data to the view, a variable is created for each item of the array. So $data is not available from within the view. You'll end up with a variable named $page_content.

PHP Code:
// Controller
$data['title'] = "My Real Title";
$data['heading'] = "My Real Heading";
$this->load->view('blogview'$data);

// View
<head>
 
       <title><?php echo $title;?></title>
</head>
<body>
        <h1><?php echo $heading;?></h1>
</body> 


More info: http://www.codeigniter.com/userguide3/ge...o-the-view
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#3

Hi thanks for your reply...

Sorry that was my misprint in the view it is....
echo $page_content['title'];

but it displays and error!!!

Im not sure whats going on as ive done this several times on other projects...
Reply
#4

$page_content is an array, you should use row_array() instead of result_array() if you have only one row and you wan to access $page_content['title']. The way it is now, you would need to write $page_content[0]['title'].


If you really have multiple row, you need to loop through it with a foreach().
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#5

Excellent stuff i could see the problem after you explained it...


The model should look like this:

PHP Code:
public function get($id null$order_by null)
    {
        
        if(
is_numeric($id)){
            
$this->db->where($this->_primary_key$id);
            
$q $this->db->get($this->_table);
            return 
$q->row_array();
        }

        if(
is_array($id)){
            foreach (
$id as $_key => $_value){
                
$this->db->where($_key$_value);

            }
            
        }
        
$q $this->db->get($this->_table);
        return 
$q->result_array();
        
    } 
Reply




Theme © iAndrew 2016 - Forum software by © MyBB