Welcome Guest, Not a member yet? Register   Sign In
Getting data from the model directly to the controller (newbie question)
#1

[eluser]Juan Velandia[/eluser]
hello Guys I´ve been thinking about this, and I really could use some help, I want to pass the meta_title field from a record on the database, but I can´t make it work

The controller:
Code:
function section()
    {

        $data['meta_title'] = $this->section_content_model->get_section_content()-> $query->meta_title???? //here is where I need some ideas
      
        $data['section_content'] = $this->section_content_model->get_section_content();

        $partials = array('top_menu'=>'view_top_menu', 'main_container'=> 'view_main_section','main_footer'=> 'view_main_footer');
        $this->template->load('master_section', $partials, $data);

}

the model

Code:
<?

class Section_content_model extends Model {

    function Section_content_model()
    {
        parent::Model();
    }

    function get_section_content()
    {

        $this->db->select('name');
        $this->db->select('header');
        $this->db->select('meta_title');
        $query = $this->db->get('cms_section');
        
        return $query->result();
        
    }
}
?>

I hope you could help me, thanks in advance
#2

[eluser]sorenchr[/eluser]
Could you post the article_content_model ? Smile
#3

[eluser]Juan Velandia[/eluser]
Hello sorenchr, I did a wrong copy paste in the post, there´s no need of the article_content_model, the model used is section_content_model.

I hope you have some Ideas,
#4

[eluser]sorenchr[/eluser]
Hi Juan

I assume you are trying to select multiple columns in get_section_content() method, your syntax seems to be off. Try this instead:

Code:
$this->db->select('name, header, meta_title');

This is how you properly chain together multiple selects in the select statement. The code:
Code:
return $query->result();
returns an array, so you should handle it as an array in your controller.

Try something like:

Code:
$query_from_model = $this->section_content_model->get_section_content();
$data['meta_title'] = $query_from_model->meta_title;

Not 100% field tested !
#5

[eluser]Juan Velandia[/eluser]
Hello sorenchr, I did as you said... but here´s what happened. perhaps you have some other indication... thanks a lot!!

http://www.andaki.com/index.php?/master/...o_importar

line 45:
Code:
$query = $this->section_content_model->get_section_content();
        $data['meta_title'] = $query->meta_title;
#6

[eluser]sorenchr[/eluser]
Hi Juan

Sorry, I didn't check the documentation properly. Try changing your model and controller to this:

Code:
<?

class Section_content_model extends Model {

    function Section_content_model()
    {
        parent::Model(); //You dont need this constructor if you only want to declare
                         //the parent Model class
    }

    function get_section_content()
    {

        $this->db->select('name, header, meta_title');
        $query = $this->db->get('cms_section');
        
        return $query; //I only return the $query because you need to do some
                       //different data-manipulation in your controller
        
    }
}

?>

Code:
function section()
{

        $query        = $this->section_content_model->get_section_content();
        $query_row    = $query->row();
        $query_result = $query->result();

        $data['meta_title'] = $query_row->meta_title; //This is assuming you only retrieve one
                                                      //row from the database query!!
      
        $data['section_content'] = $query_result; //I replaced this one, because this one used to equal
                                                  //the returned $query->result() from the model.

        $partials = array('top_menu'=>'view_top_menu', 'main_container'=> 'view_main_section','main_footer'=> 'view_main_footer');
        $this->template->load('master_section', $partials, $data);

}

Hope it works Smile

The documentation for query results can be found at: http://ellislab.com/codeigniter/user-gui...sults.html
#7

[eluser]Juan Velandia[/eluser]
Thanks sorenchr, I´ll put it, and I´ll let you know!

As you may notice, I´m just starting whith CI.

Best regards!
#8

[eluser]sorenchr[/eluser]
Sorry to walk out on your question, but I'm heading off to bed. Will look at your question again in the morning !
#9

[eluser]Juan Velandia[/eluser]
I went to bed last night as well, after some coding it finally worked!, The site is hosted in godaddy.com so I really don´t know why the query doesn´t work as you suggested, so I created a new function -get_section_meta_data- within the model and it went like this

Code:
function get_section_meta_data()
    {
        $lang = $this->session->userdata('global_lang');
        $this->db->select('meta_title');       // I don´t know
        $this->db->select('meta_description'); // why this is the way
        $this->db->select('meta_keywords');    // to make it work in godaddy.com
        $this->db->where('lang',$lang);
        $this->db->where("url_section",$this->uri->segment(3));
        $query = $this->db->get('cms_section');
        return $query; // thanks for this!!
    }
}

and the controller

Code:
function section()
    {
        $query = $this->section_content_model->get_section_meta_data();
        $query_row = $query->row();

        $data['meta_title'] = $query_row->meta_title;
        $data['meta_description'] = $query_row->meta_description;
        $data['meta_keywords'] = $query_row->meta_keywords;

        $data['item_placeholder_1'] = $this->item_placeholder_model->show_item_placeholder_1();
        $data['item_placeholder_2'] = $this->item_placeholder_model->show_item_placeholder_2();
        
        $data['section_content'] = $this->section_content_model->get_section_content();
        $data['article_content'] = $this->article_content_model->get_article_content();

        $partials = array('top_logo'=>'view_top_logo','top_menu'=>'view_top_menu', 'main_container'=> 'view_main_section','right_menu'=> 'view_right_menu', 'main_footer'=> 'view_main_footer');
        $this->template->load('master_section', $partials, $data);

}

and the final result: http://www.andaki.com/, with dynamic title, description and keywords!

Thanks mate!




Theme © iAndrew 2016 - Forum software by © MyBB