• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to transfer data between model & controller?

#1
[eluser]imanhunter[/eluser]
Hi,

I'm struggling to learn how to transfer data between model & controller. Yes, I'm new to both PHP & CI so you can call me NoOb. Have learn through tutorial (vids/article) but still can't get the idea.

Below is my code

Model
Code:
function sahDaftarmasuk($alnamapengguna, $alkatalaluan)
    {
        $this->db->where('namapengguna',$alnamapengguna);
        $this->db->where('katalaluan',$alkatalaluan);
        $this->db->where('keaktifan','1');
        $dapatmaklumat = $this->db->get('spkppi_pengguna');
        
        if($dapatmaklumat->num_rows() > 0)
        {
            $maklumatpengguna = array();
            $maklumatpengguna = $dapatmaklumat->row_array();
            return $maklumatpengguna;
        }
        else
        {
            $this->load->view('vsyslogin');;
        }
    }


here is the controller

Code:
function daftarmasuk()
    {
        $this->form_validation->set_rules('namapengguna', 'Nama Pengguna', 'required|trim|max_length[100]|xss_clean');
        $this->form_validation->set_rules('katalaluan', 'Kata Laluan', 'required|trim|min_length[6]|max_length[20]|xss_clean');
        
        if ($this->form_validation->run() === FALSE)
        {
            $data['pagetitle'] = "maklumat sekolah";
            $this->load->vars($data);
            $this->load->view('vsyslogin');
        }
        else
        {
            $this->load->Model('Msyslogin');
            
            $maklumatpengguna = array();

            $alnamapengguna = $this->input->post('namapengguna');
            $alkatalaluan = do_hash(do_hash($this->input->post('katalaluan')),'md5');
            
            $this->Msyslogin->sahDaftarmasuk($alnamapengguna, $alkatalaluan);
            
            if($maklumatpengguna['kelayakkan'] == 0)
            {
                $data['kelayakkan'] = $maklumatpengguna['kelayakkan'];
                $this->load->view('admin/avdashboard', $data);
            }
            else if ($maklumatpengguna['kelayakkan'] == 1)
            {
                $this->load->view('pkelas/pjdashboard');
            }
            else if ($maklumatpengguna['kelayakkan'] == 2)
            {
                $this->load->view('psyarikat/psdashboard');
            }
            else
            {
                redirect('vsyslogin');
            }
        }    
    }

The idea here is if the user log into the app, then it will get the user data (namapengguna & katalaluan) from db which after that it will also get the user rank. different rank will redirect user to different dashboard.

I'm trying to get the row into an array which will return to controller then will be extracted in it.

#2
[eluser]WanWizard[/eluser]
You shouldn't load views in your model. Model methods are strictly behind the scenes blocks of code.

Instead, do
Code:
function sahDaftarmasuk($alnamapengguna, $alkatalaluan)
    {
        $this->db->where('namapengguna',$alnamapengguna);
        $this->db->where('katalaluan',$alkatalaluan);
        $this->db->where('keaktifan','1');
        $dapatmaklumat = $this->db->get('spkppi_pengguna');
        
        if($dapatmaklumat->num_rows() > 0)
        {
            $maklumatpengguna = array();
            $maklumatpengguna = $dapatmaklumat->row_array();
            return $maklumatpengguna;
        }
        else
        {
            return FALSE;
        }
    }

Code:
function daftarmasuk()
    {
        $this->form_validation->set_rules('namapengguna', 'Nama Pengguna', 'required|trim|max_length[100]|xss_clean');
        $this->form_validation->set_rules('katalaluan', 'Kata Laluan', 'required|trim|min_length[6]|max_length[20]|xss_clean');
        
        if ($this->form_validation->run() === FALSE)
        {
            $data['pagetitle'] = "maklumat sekolah";
            $this->load->vars($data);
            $this->load->view('vsyslogin');
        }
        else
        {
            $this->load->Model('Msyslogin');
            
            $maklumatpengguna = array();

            $alnamapengguna = $this->input->post('namapengguna');
            $alkatalaluan = do_hash(do_hash($this->input->post('katalaluan')),'md5');
            
            $maklumatpengguna = $this->Msyslogin->sahDaftarmasuk($alnamapengguna, $alkatalaluan);
            
            if($maklumatpengguna === FALSE)
            {
                redirect('vsyslogin');
            }
            elseif($maklumatpengguna['kelayakkan'] == 0)
            {
                $data['kelayakkan'] = $maklumatpengguna['kelayakkan'];
                $this->load->view('admin/avdashboard', $data);
            }
            else if ($maklumatpengguna['kelayakkan'] == 1)
            {
                $this->load->view('pkelas/pjdashboard');
            }
            else if ($maklumatpengguna['kelayakkan'] == 2)
            {
                $this->load->view('psyarikat/psdashboard');
            }
            else
            {
                redirect('vsyslogin');
            }
        }    
    }

So have your model methods return the values they are supposed to retrieve, or FALSE is there is an error. Check the return value in the controller, and base your descisions on that value. Your controller should contain the application flow logic, not the model.

#3
[eluser]imanhunter[/eluser]
[quote author="WanWizard" date="1300547361"]You shouldn't load views in your model. Model methods are strictly behind the scenes blocks of code.

Instead, do


So have your model methods return the values they are supposed to retrieve, or FALSE is there is an error. Check the return value in the controller, and base your descisions on that value. Your controller should contain the application flow logic, not the model.[/quote]

Thank WanWizard for that help. CI so far is the easiest to learn but in someway I'm still stuck. Tried other framework but the end, still get back to CI.

It's working now. So I can continue. Should ask in this forum months ago. LOL.

BTW, how can I maintain the data. Can session help?

#4
[eluser]WanWizard[/eluser]
If by maintain you mean between page requests, then sessions are the way to go.

You can use normal session data (which lasts until the session expires or you delete it) or flash data, which only lasts for one page request,

#5
[eluser]CroNiX[/eluser]
Think of the controller as the assembler. It gets all data and preforms all logic that it needs for the task. The model is usually strictly database calls, which you call from a controller, to get more needed data for use in that controller. After the controller "assembles" what it needs, it shoots it off the the view for display.

#6
[eluser]imanhunter[/eluser]
Thanks to both of you. I do understand about MVC - the way CI works. Just having problem with data transfer. Big Grin

Glad to be part of CI community.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.