codeigniter将数组数据从模型传递到控制器

时间:2022-11-24 13:31:01

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

我正在我的模型的get_distribuidor()函数中执行以下查询:

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.

现在,将$res返回给控制器,我不太清楚如何将数组结果包含的多个字段分开。

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 个解决方案

#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.

如果要将view()的内容抓取到一个变量中,则使用view()的第三个参数。要将数据传递给视图,需要将其添加为$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']

#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.

如果要将view()的内容抓取到一个变量中,则使用view()的第三个参数。要将数据传递给视图,需要将其添加为$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']