从模型codeigniter传球达阵数据控制器 [英] codeigniter passing array data from model to controller

查看:122
本文介绍了从模型codeigniter传球达阵数据控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 公共职能get_distribuidor()
{
    $这个 - >负载>帮手(URL);    $查询= $这个 - > DB-GT&;查询(SELECT * FROM distribuidor,其中id_distribuidor ='1';);的foreach($查询 - > result_array()作为$行)
{
   回声$行['id_distribuidor'];
   回声$行['nome_em preSA'];
   回声$行['cod_postal'];
   回声$行['localidade'];
   回声$行['telefone'];
   回声$行['传真'];
   回声$行[电子邮件];
}
    $解析度=阵列(
        nome_em preSA'=> $行['nome_em preSA'],
        莫娜达'=> $行['莫娜达'],
        cod_postal'=> $行['cod_postal'],
        localidade'=> $行['localidade'],
        telefone'=> $行['telefone'],
        传真=> $行['传真'],
        电子邮件=> $行[电子邮件]
    );    返回$资源;}

现在,回到$资源给控制器,我不很清楚如何将多个字段的数组结果包含分离。

我用这对控制器的功能:

  $数据['nome_produto'] = $这个 - > fichas_model-> set_fichas();        $睾丸= $这个 - > fichas_model-> get_distribuidor();        $这个 - >负载>查看('produtos / ponto1',$数据,$睾丸);

要在视图上是这样写的:

 <输入类型=输入NAME =莫娜达VALUE =< PHP的echo $睾丸['莫娜达'];?>中/>< BR />

但它不工作,能有人指出我什么我做错了?

感谢您!


解决方案

  $睾丸= $这个 - > fichas_model-> get_distribuidor();
$这个 - >负载>查看('produtos / ponto1',$数据,$睾丸);

应该是:

  $数据['睾丸'] = $这个 - > fichas_model-> get_distribuidor();
$这个 - >负载>查看('produtos / ponto1',$数据);

href=\"http://ellislab.com/$c$cigniter/user-guide/general/views.html#footer\" rel=\"nofollow\">第三个参数获得<$ c中的视图()是,如果你想抓住视图()到一个变量中。将数据传递给视图你需要将其添加为 $数据数组项

例如:

  $数据=阵列(
    '标题'=&GT; 我的标题,
    '标题'=&GT; 我的标题,
    '消息'= GT; '我的信息'
);$这个 - &GT;负载&gt;查看('blogview',$数据);
//然后,您访问$标题,$标题,在视图文件$消息

你所用上面的编辑做的是本质上以下内容:

  $数据=阵列(
    '睾丸'=&GT; $这个 - &GT; fichas_model-&GT; get_distribuidor()
);$这个 - &GT;负载&gt;查看('produtos / ponto1',$数据);
//然后,您访问$睾丸中的视图文件,
//这将是一个数组,所以你可以访问$睾丸['莫娜达']

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?

Thank you!

解决方案

$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']

这篇关于从模型codeigniter传球达阵数据控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆