3

I'm executing the following query in the get_distribuidor() function of my model:

public function get_distribuidor()
{
    $this->load->helper('url');

    $query = $this->db->query("SELECT * FROM distribuidor where id_distribuidor='1';");

    foreach ($query->result_array() as $row)
    {
      echo $row['id_distribuidor'];
      echo $row['nome_empresa'];
      echo $row['cod_postal'];
      echo $row['localidade'];
      echo $row['telefone'];
      echo $row['fax'];
      echo $row['email'];
    }


    $res = array(
        'nome_empresa' => $row['nome_empresa'],
        'morada' => $row['morada'],
        'cod_postal' => $row['cod_postal'],
        'localidade' => $row['localidade'],
        'telefone' => $row['telefone'],
        'fax' => $row['fax'],
        'email' => $row['email']
    );

    return $res;

}

Now, returning $res to the controller, i don't know very well how to separate the multiple fields the array result contains.

I'm using this on a function of the controller:

$data['nome_produto']=$this->fichas_model->set_fichas();

$teste=$this->fichas_model->get_distribuidor();

$this->load->view('produtos/ponto1',$data, $teste);

to write on the view something like this:

<input type="input" name="morada" value="<?php echo $teste['morada'];?>" /><br />

but it's not working, can someone point me what am I doing wrong?

1
  • Of course, it is a violation of MVC design to echo anything from the model. If your intention is to return a flat array (row) from the model to the controller, then you don't need a loop -- just return the data from row_array(). Commented Apr 10, 2024 at 22:52

1 Answer 1

6
$teste=$this->fichas_model->get_distribuidor();
$this->load->view('produtos/ponto1',$data, $teste);

should be:

$data['teste'] = $this->fichas_model->get_distribuidor();
$this->load->view('produtos/ponto1',$data);

The 3rd parameter for view() is used if you want to grab the content of view() into a variable. To pass data to the view you need to add it as an array item of $data.

For Example:

$data = array(
    'title' => 'My Title',
    'heading' => 'My Heading',
    'message' => 'My Message'
);

$this->load->view('blogview', $data);
// You then access $title, $heading, $message in the view file

What you are doing with above edit is the following essentially:

$data = array(
    'teste' => $this->fichas_model->get_distribuidor()
);

$this->load->view('produtos/ponto1', $data);
// You then access $teste in the view file, 
// which will be an array so you can access $teste['morada']
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.