0

I'm sorry I have a question, I want to get data from query and send it to view.. Here's my code..

$this->load->model('expert_model');
$data = $this->expert_model->get_result($consultation_id);

$datas = $this->db->select('*')->limit(1)->order_by('CF_Combine', 'DESC')->get_where('hasil', ['id_konsultasi' => $consultation_id])->row();
        
$this->load->view('User/value', $datas);

I have tried dump the $datas and the result is all I want. But, How can I get data from $datas to the User/value's view ? I tried to use $datas but it said undefined 'datas'

I'm sorry I really bad both in english and code :(

And here's my code in view, just want to see it works or not

<?php foreach ($datas as $key) : ?>
<?php echo number_format(($key->CF_Combine * 100), 2) . " %" ?>                   
<?php endforeach ?>
Peb Mpeeb
  • 77
  • 5
  • 1
    Why not check the docs? Top result when searching for "codeigniter view". It would be faster than writing a question! :-) https://codeigniter.com/userguide3/general/views.html#adding-dynamic-data-to-the-view – Don't Panic Aug 05 '21 at 10:07
  • Does this answer your question? [Codeigniter: Passing data from controller to view](https://stackoverflow.com/questions/9446700/codeigniter-passing-data-from-controller-to-view) – Don't Panic Aug 05 '21 at 10:09

3 Answers3

0

Error Undefined variable 'data' says that you are trying to use variable data but you send in view datas, with s in the end. Try to change the variable name for the same either in view or in controller.

UPDATE You should not use datas variable directly in the view. You need to call variables that correspond to the array keys in your data. For example: if you have $datas['name'] key - in view you should use just $name. In your case just use $datas['datas'] = instead of $datas =

Dmytro Huz
  • 1,514
  • 2
  • 14
  • 22
0

I am no expert at CodeIgniter, but I can imagine where the error might be.

It seems that passing the row in the view doesn't work, because of what your view expects as data.

If inside your view code you have something as $datas->id, then your call should be $this->load->view('User/value', ['datas'=>$datas]);

0
$this->load->model('expert_model');
$datas=array();  //define $datas is array
$data = $this->expert_model->get_result($consultation_id);

$datas['datas'] = $this->db->select('*')->limit(1)->order_by('CF_Combine', 'DESC')->get_where('hasil', ['id_konsultasi' => $consultation_id])->row();
        
$this->load->view('User/value', $datas);