Welcome Guest, Not a member yet? Register   Sign In
A newbie already in trouble! - returning array data
#1

[eluser]jtotheb[/eluser]
The Model:

Code:
<?php

class Testmodel extends Model {
    
    function Testmodel()
    {
        
        parent::Model();  
    }
    
    function get_page_title()
    {
        $query = $this->db->query('SELECT page_info.page_title FROM page_info');

        $row = $query->row();
        
    }

}

?>

The Controller:

Code:
<?php

class Test_con extends Controller {
    
    function Test_con()
    {
        parent::Controller();    
    }
    
    function index()
    {
        $this->load->model('Testmodel');
        $data['page_title'] = $this->Testmodel->get_page_title();
        
        $this->load->view('selection', $data);
    
    }
}
?>

The View:

Code:
<?php echo $page_title;?>

It just returns nothing in the view. If i change it in the view to <?php echo $data;?> it returns the word "array".

What am i doing wrong here?!

The database is two columns - id, page_title and one row of data - 1, "i'm the title"

If i do a print_r ($data) i can see the page title but it is in another array within an array or something!

Any help would be great. Thank you.
#2

[eluser]Glen Swinfield[/eluser]
You are not actually returning anything from your model:

Code:
class Testmodel extends Model {
    
    function Testmodel()
    {
        
        parent::Model();  
    }
    
    function get_page_title()
    {
        $query = $this->db->query('SELECT page_info.page_title FROM page_info');

        $row = $query->row();

        // Need to return values
        return $row;
        
    }

}
}

Controller:

Code:
class Test_con extends Controller {
    
    function Test_con()
    {
        parent::Controller();    
    }
    
    function index()
    {
        $this->load->model('Testmodel');
        //$data['page_title'] will now gat values of $row in model
        $data['page_title'] = $this->Testmodel->get_page_title();
        
        $this->load->view('selection', $data);
    
    }
}
#3

[eluser]sempsteen[/eluser]
Also the view must be:
Code:
<?php echo $page_title->page_title;?>
for this example.
#4

[eluser]sempsteen[/eluser]
Or change the return value of get_page_title() method like this:
Code:
return $row->page_title;
#5

[eluser]jtotheb[/eluser]
Ah right. I did have return $row in there but it still didn't work!

It was me being silly with my model then.

Thanks for the help. Really appreciate it.




Theme © iAndrew 2016 - Forum software by © MyBB