2013-01-02 95 views
2

我執行下面的查詢在我的模型的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; 

} 

現在,回到$資源來控制,我不知道很以及如何分離數組結果包含的多個字段。

我用這對控制器的功能:

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

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

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

上的觀點是這樣寫的:

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

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

回答

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

應該是:

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

3rd parameterview()使用,如果你想抓住的view()內容到一個變量。要將數據傳遞到視圖,您需要將其添加爲數組項$data

例如:

$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 

什麼你上面編輯做的是基本上依據:

$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

工作就像一個魅力:)謝謝了很多神祕的:P – user1511579

相關問題