Welcome Guest, Not a member yet? Register   Sign In
Understanding MVC: Can same views be used for all pages, no matter what?
#1

[eluser]zee[/eluser]
Hi
I am new to CI and MVC and was just developing a sample application to learn the things. I was aiming to design a simple website with static pages with title and content being stored in database for each file.
I am using a Pagemodel model as follows
Code:
class Pagemodel extends CI_Model{
    function __construct(){
        parent::__construct();
    }
    
    function fetchDb(){
        $data=array();
        $this->load->database();
        $this->db->select('pagetitle,pagetype,pagecontent');
        $this->db->from('pages');
        $this->db->where('idpages',1);
        $this->db->where('pagestatus',1);
        $q=$this->db->get();
        if($q->num_rows()>0){
            $data=$q->row_array();
        }
        return $data;
        $q->free_result();
    }
}

I have made separate controllers for all my pages.

Code:
class Home extends CI_Controller{
    function index(){
        $this->load->helper('html');
        $this->load->helper('url');
        $this->load->model('Pagemodel','',TRUE);
        $pagedata['content']=$this->Pagemodel->fetchDb();
        $this->load->view('sampleview',$pagedata);
}
}

My main aim was to utilize same view (here ‘sampleview’) to generate all pages. As far as I understood MVC approach in CI, I perceived that different controllers need to be called for different pages that fetch data from appropriate model and render it on a view.
Now in case I need to add some PHP generated content, say, a CI form on one of the pages how is it possible? Something like:

Code:
echo form_open('search');
        echo form_input();
        echo form_submit('submit','search');
        echo form_close();
I was trying to find any way if I could make a function for that in model or modify the controller else I would need to make separate views for all pages. Can I pass this code by an array to the view?
Apologies if I sound like an alien!
#2

[eluser]jblack199[/eluser]
Well, in the case of having 1 view that looks the same except for the data listed on that page you'd do it just like anything else....

in your model you'd have it obtain the information like an array or whatever....

you'd then do your echo just like you would echo any array... echo $array['title']; within the <title></title> tags and echo $array['message']; in the body...

getting it so you can do echo form_input(); type functionality you'd need one function for each in your helper.. and you'd need to passthrough the id of the page you're trying to grab so that the page data and the title data functions are grabbing from the same row...

in terms of programming it so you can echo out the 'function' as long as you have that return in there you're golden as echo function(); would be the same as echo $data; and of course $data being $data = function(); so you just bypass the middle man by echoing the function...




Theme © iAndrew 2016 - Forum software by © MyBB