Welcome Guest, Not a member yet? Register   Sign In
Returning A Result (Problem With Arrays I Believe)
#1

[eluser]Chris.Campbell[/eluser]
Ok so I am trying to return a result from a database. I think it's being put into a multidimensional array... I am not 100% sure. Anyways look below.

This is my model. As you can see in the get_header() function it is pulling the result from the table "header".
Code:
<?php
class Main_model extends Model {

    function Main_model()
    {
        parent::Model();
    }
    
    function get_header()
    {
        $query = $this->db->get('header');
                return $query->result();
    }

}
?>

This my controller. As you can see in the header_view() function, it's taking the result and putting it in an array. And then the index is taking the "header_view()" and putting it in another array.
Code:
<?php
class Main extends Controller {

    function Main()
    {
        parent::Controller();
    }
    
    function index()
    {
        $data['header'] = $this->header_view();                                            
        $this->load->view('main_view', $data);
    }
    
    function header_view()
    {
        $this->load->model('Main_model');
        $data['title'] = $this->Main_model->get_header();
    }

}
?>

Now I am trying to call it... I cannot figure out how!
Code:
<title><?php echo $header[2]['title']; ?></title>

Any help is great please!
#2

[eluser]Imkow@CN[/eluser]
Your search result usually is a 3-dimentional array.
use print_r($array) to show how it structured.

By following the code in your controller, code in your view should be like:
<title><?php echo $title[0]["header"]; ?></title>

Here $title is the varible you assigned in controller, [0] is the first set(row) of your query result,
#3

[eluser]座頭市[/eluser]
Basically, you need to return something from your function get_header(), right now all itConfused doing is calling the model then storing the data in an array called $data rather than returning it:
Code:
function header_view()
    {
        $this->load->model('Main_model');
        return $this->Main_model->get_header();
    }
As mentioned by Imkow@CN, you are currently returning the model call as an array, which is fine if you need it, but just from the code you posted, it might be better to use row() rather than result(), at returns a single object rather than an array of objects. Mind you, this is just a guess from your code.

In the view, you have an array of objects, not a multidimensional array, so you should be accessing it like this:
Code:
<title><?=$header[0]->title;?></title>

If you want to return a multidimensional array, use result_array() rather than result() in the model.
#4

[eluser]Chris.Campbell[/eluser]
[quote author="座頭市" date="1185427349"]Basically, you need to return something from your function get_header(), right now all itConfused doing is calling the model then storing the data in an array called $data rather than returning it:
Code:
function header_view()
    {
        $this->load->model('Main_model');
        return $this->Main_model->get_header();
    }
As mentioned by Imkow@CN, you are currently returning the model call as an array, which is fine if you need it, but just from the code you posted, it might be better to use row() rather than result(), at returns a single object rather than an array of objects. Mind you, this is just a guess from your code.

In the view, you have an array of objects, not a multidimensional array, so you should be accessing it like this:
Code:
<title><?=$header[0]->title;?></title>

If you want to return a multidimensional array, use result_array() rather than result() in the model.[/quote]

I used the suggestions you outlined such as using row() and return, I am confused about how I would output this in the header now.
#5

[eluser]座頭市[/eluser]
If you're using row() rather than result() in your model function, just call
Code:
<?=$header->title;?>
#6

[eluser]Chris.Campbell[/eluser]
[quote author="座頭市" date="1185428790"]If you're using row() rather than result() in your model function, just call
Code:
<?=$header->title;?>
[/quote]

Hmmm now I get an error: Message: Trying to get property of non-object
#7

[eluser]座頭市[/eluser]
Please post your updated code, or edit your original post to reflect the changes.
#8

[eluser]Chris.Campbell[/eluser]
Model.
Code:
<?php
class Main_model extends Model {

    function Main_model()
    {
        parent::Model();
    }
    
    function get_header()
    {
        $query = $this->db->get('header');
                return $query->row();
    }

}
?>

Controller.
Code:
<?php
class Main extends Controller {

    function Main()
    {
        parent::Controller();
    }
    
    function index()
    {
        $data['header'] = $this->header_view();                                            
        $this->load->view('main_view', $data);
    }
    
    function header_view()
    {
        $this->load->model('Main_model');
        return $this->Main_model->get_header();
    }

}
?>

View.
Code:
<title><?php echo $header->title; ?></title>
#9

[eluser]座頭市[/eluser]
Odd that - it all looks correct.

I guess the most obvious thing would be to make sure the db table has a column called title?
#10

[eluser]Chris.Campbell[/eluser]
Crap... the column is called "title" ... hmm I am not sure what is wrong.




Theme © iAndrew 2016 - Forum software by © MyBB