• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pass variables from model to controller to view?

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.

class Redeem_model extends Model {

    function Redeem_model()
        // Call the Model constructor
    function getData($code)
            //Query the data table for every record and row
            $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');
                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.

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:


Then you call your Model methods from your controller using:

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

Then you pass $record to your view using:

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

This can now be used in your view using:

<?= $id?>

Hope that helps.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

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