Welcome Guest, Not a member yet? Register   Sign In
Proper way to send database query from Model to Controller to the View
#1

[eluser]Jonathan McGaha[/eluser]
Ok I already posted once about just trying to get a basic database query working. The replies helped me construct one from the controller. I then used Derek Allard's video to help me construct from the model to the controller and then having an echo in the view to display the results.
However, I'm trying to understand the "proper" way of using my active record statements in the model, to then calling that model in the controller. Then I want to be able to work with the array in the view because that's where it makes the most sense for me to work with it. Derek had his query results laid out in html in his model but that doesnt seem right to me. I'd rather in my view layout the foreach statement because it seems that my html layout should be in the view. If I am wrong in my thinking please let me know, because I want to code the proper way from the very beginning.

Model:
Code:
<?php class Scriptures extends Model {

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


function getVerse()
    {
        $this->db->select('verses.book AS vbook, verses.chapter AS chapter, verses.verse AS verse, verses.text AS text, books.book AS book');
        $this->db->where('verses.book', '48');
        $this->db->from('verses');
        $this->db->join('books', 'verses.book = books.id', 'inner');
        $this->db->orderby('vbook', 'chapter', 'verse');
        $query = $this->db->get('verses');
        return $query->result_array();
    }
}
?>

Controller:
Code:
<?php class Bible extends Controller {

    function index() {    
        parent::Controller();
        $data['title'] = "The KJV Bible";
        $this->load->model('scriptures');
        $data['book_results'] = $this->scriptures->getVerse(); *** I have no idea what to put here to send the array to the view****
        $this->load->view('bibleview', $data);
    }
}
?>

View:
I would assume the foreach goes in the view but please confirm this for me.
#2

[eluser]Tom Glover[/eluser]
The foreach would go in the view other wise you would be sending only the last of its creation to the view.
#3

[eluser]Jonathan McGaha[/eluser]
Is my controller right to send that data into the view?
#4

[eluser]Tom Glover[/eluser]
[quote author="Jaxbulls" date="1209518993"]Is my controller right to send that data into the view?[/quote]

Yes, just assign anything to the $data array. then call the array key in the view to load it. for example

controller:
Code:
$data[s1] = "some text";
$this->load->view('viewone',$data)


View file:
Code:
<?
   echo $s1;
?>
#5

[eluser]Taff[/eluser]
Code:
<?php class Bible extends Controller {

    function index() {    
        parent::Controller();
        $data['title'] = "The KJV Bible";
        $this->load->model('scriptures');
        $data['book_results'] = $this->scriptures->getVerse(); *** I have no idea what to put here to send the array to the view****
        $this->load->view('bibleview', $data);
    }
}
?>


Now I could be wrong but I think that should be
Code:
$data['book_results'] = $this->Scriptures->getVerse();

I like using the optional second parameter when loading my model so for example:

Code:
<?php class Bible extends Controller {

    function index() {    
        parent::Controller();
        $data['title'] = "The KJV Bible";
        $this->load->model('Scriptures','model');
        $data['book_results'] = $this->model->getVerse();
        $this->load->view('bibleview', $data);
    }
}
?>

That should get your data array into your bibleview

Good luck!
Taff
#6

[eluser]Jonathan McGaha[/eluser]
OK thanks you guys helped me a great deal. I checked one more thing in the User Guide to get the foreach right and now I have exactly what I was looking for.

Thanks a ton!




Theme © iAndrew 2016 - Forum software by © MyBB