Welcome Guest, Not a member yet? Register   Sign In
Pass variables from model to controller to view?
#1

[eluser]Unknown[/eluser]
Hi, first post, new codeigniter user and new MVC so bear with me!

I'm having a hard time understanding how to pass information from my model to my view. Before I go any further, here is my model.

Code:
<?php
class Redeem_model extends Model {

    function Redeem_model()
    {
        // Call the Model constructor
        parent::Model();
    }
    
    function getData($code)
        {
            //Query the data table for every record and row
            $this->db->select('id,referral_code,date_redeemed,redemption_ip');
            $this->db->from('ci_referral_code');
            $this->db->where('referral_code', $code);
            
            $query = $this->db->get();
            $row = $query->row();
                
            if ($query->num_rows() == 0)
            {
                show_error('That referral code was not in the database');
            }
            elseif ($row->date_redeemed != "0000-00-00")
            {
                show_error('That Referral Code has already been redeemed');
            }    
            else
            {        
                return $query->result();
            }
        }

    function updateData($code, $date)
        {
            //Query the data table for every record and row
            $data = array(
                           'date_redeemed' => $date
                        );
            
            $this->db->where('referral_code', $code);
            $this->db->update('ci_referral_code', $data);            
            $update = "success";
            return $update;
        }

}
?>

You can see I'm checking the results of the query for the existence of a referral code and then if the data is set to '0000-00-00' (that's ugly, isn't it?) and display an error for each. What I would like to do is be able to set a variable to true if either of those conditions exist and then pass that variable to the view so I can display the error how I want with a conditional rather than with the show_error() function in the model.

So, I'm sure my n00b status is showing throw and I'm sure I'm missing some obvious fundamental and I'd really appreciate it if one of you could point me in the right direction.
#2

[eluser]darrentaytay[/eluser]
I'm new to CodeIgniter aswell, however, you shouldn't be passing variables from your Model to your View.

You should be loading your Model from your Controller using:

Code:
$this->load->model('myModel');

Then you call your Model methods from your controller using:

Code:
$id = $this->myModel->getID()

Then you pass $record to your view using:

Code:
$this->load->view("myView", array ( "id" => $id));

This can now be used in your view using:

Code:
<?= $id?>

Hope that helps.




Theme © iAndrew 2016 - Forum software by © MyBB