Pardeep
Pardeep

Reputation: 81

passing an array from model to controller

In my model, I am getting all emails in an array. I want to show this array of emails in controller, not in view.

Here my model is:

     public function cron_job(){
     $this->db->select('email');
     $this->db->from('wc_buyer');
     $query = $this->db->get();
     return $query; 
  }

and the controller is:

    public function cron_job(){
    $this->load->model('home/Home_model');
    $data['a'] = $this->Home_model->cron_job();
}

How can I show this print this array of emails in variable in controller?

Upvotes: 2

Views: 368

Answers (4)

rajeev
rajeev

Reputation: 115

Try like this

     public function cron_job(){
             $this->db->select('email');
             $this->db->from('wc_buyer');
             $query = $this->db->get();
             $result = array();
             foreach ($query->result() as $row){
                $result[] = $row;
             }
             return $result; 
        }

 public function cron_job(){
    $this->load->model('home/Home_model');
    $data['a'] = $this->Home_model->cron_job();
    print_r($data['a']);
}

Upvotes: 1

Fil
Fil

Reputation: 8863

CodeIgniter provide a method result_array() which return the resulting query into array, in your case, do it this way.

In your model

public function cron_job(){
     $this->db->select('email');
     $this->db->from('wc_buyer');
     $query = $this->db->get();
     return $query->result_array(); 
}

In your Controller,

public function cron_job(){
    $this->load->model('home/Home_model');
    $data['a'] = $this->Home_model->cron_job();
    $this->load->view('path/to/the/file', $data);
}

In your view,

<?php foreach ($a as $key => $value) : ?>
  <?php echo $value['your_table_field']; ?>
<?php endforeach; ?>

Upvotes: 0

Saty
Saty

Reputation: 22532

You echo your email in controller use result_array() as

$email=$this->Home_model->cron_job();// get data from model
foreach ($email->result_array() as $row)// fetch data form query 
{
        echo $row['email'];
}

Upvotes: 1

Vinod VT
Vinod VT

Reputation: 7149

Try like this,

$data = $this->Home_model->cron_job();
print_r($data); // To print array

Upvotes: 0

Related Questions