2016-01-06 29 views
1

我的模型中有一個函數檢查登錄表單中給出的憑證。它選擇email,senhatipo_pessoa。以下是功能:使用CodeIgniter中的查詢結果設置會話值3

public function find_credentials() 
{ 
    $this->db 
     ->select('email, senha, tipo_pessoa') 
     ->from($this->table) 
     ->where('email', $this->input->post('email')) 
     ->where('senha', md5($this->input->post('password'))); 

    $query = $this->db->get(); 

    if ($query->num_rows() == 1) { 
     return true; 
    } 

    return false; 
} 

無論前一個函數的結果如何,位於我的控制器中的下一個函數都會執行。它決定根據返回的結果做什麼:

public function validate_credentials() 
{ 
    if ($this->Usuarios_fisica_model->find_credentials()) { 
     $data = array(); 
     $data['email'] = $this->input->post('email'); 

     $this->session->set_userdata($data); 
     $this->session->set_flashdata('message', 'Bem-vindo!'); 

     redirect('/', 'refresh'); 
    } else { 
     $this->session->set_flashdata('message', 'Desculpe, credenciais inválidas'); 
     redirect('/'); 
    } 
} 

我需要存儲tipo_pessoa在會話來確定哪些頁面的用戶應該被重定向到。對於兩種不同類型的用戶,我有兩個不同的表格,每種都有自己的頁面。

編輯*

建議的解決方案之後,我加入到我的頭文件,下面的代碼來呼應tipo_pessoa

<div class="menu_login"> 
    <?php if ($this->session->userdata('email')) { ?> 
     <span><?php echo $this->session->userdata('email'); ?></span> 
     <span><?php echo $this->session->userdata('tipo_pessoa'); ?></span> 
     <a href="<?php echo base_url('usuario/painel'); ?>" class="bt_entre">Ir ao Painel</a> 
     <a href="<?php echo base_url('usuario/sair') ?>" class="bt_cadastre">Sair</a> 
    <?php } else { ?> 
     <span>Minha Conta</span> 
     <a href="#" id="bt_entrar" class="bt_entre">Entre</a> 
     <a href="<?php echo base_url('cadastro') ?>" class="bt_cadastre">Cadastre-se</a> 
    <?php } ?> 
</div> 
+0

'tipo_pessoa'是什麼類型的數據?詮釋?串? – DFriend

+0

在你的代碼中是'$ this-> session-> set_userdata('tipo_pessoa',$ tipo_pessoa);'? – Kisaragi

+0

@Kisaragi我的確如你所回答的那樣。 – mfgabriel92

回答

0

下面是如何從結果設置會話數據的示例查詢:

function get_something() 
{ 
    $this->db->select('something'); 
    $something = $this->db->get('table')->row(); 
    $this->session->set_userdata('something',$something); 
} 

用您的示例模型:

$this->db 
    ->select('email, senha, tipo_pessoa') 
    ->from($this->table) 
    ->where('email', $this->input->post('email')) 
    ->where('senha', md5($this->input->post('password'))); 

$query = $this->db->get(); 

if ($query->num_rows() == 1) { 
    return $query->result_array(); 
} 

return false; 

控制器:

if ($something = $this->Usuarios_fisica_model->find_credentials()) { 
    $data = array(); 
    $data['email'] = $this->input->post('email'); 
    $this->session->set_userdata('tipo_pessoa', $something['tipo_pessoa']); 
    $this->session->set_userdata($data); 
    $this->session->set_flashdata('message', 'Bem-vindo!'); 

    redirect('/', 'refresh'); 
} else { 
    $this->session->set_flashdata('message', 'Desculpe, credenciais inválidas'); 
    redirect('/'); 
} 
+0

我試圖回顯session-> userdata('tipo_pessoa'),但它什麼都沒顯示。 – mfgabriel92

+0

因爲在你的代碼中你沒有設置它。 – Kisaragi

+0

我在我的頭文件中,其中包含登錄的電子郵件。我也試圖顯示'tipo_pessoa',只是爲了測試。 '<?php echo $ this-> session-> userdata('tipo_pessoa'); ?>' – mfgabriel92

1

使用這改變了型號:

$query = $this->db->get(); 
return $query->result(); 

,使這個改變給控制器:

$data = $this->Usuarios_fisica_model->find_credentials(); 
if(!empty($data)) 
{ 
    $tipo_pessoa = $data[0]['tipo_pessoa']; //Get value of tipo_pessoa 
    $this->session->set_userdata('tipo_pessoa', $tipo_pessoa);//Set value in session 
    //your if part code 
} 
else 
{ 
    //your else part code 
} 
+0

如果我嘗試看到它,也沒有任何迴應。 <?php echo $ this-> session-> userdata('tipo_pessoa'); ?> – mfgabriel92

+0

試圖upvote這一點,不知道爲什麼它消極。 – Kisaragi

0

而不是做什麼,我打算做的,我只需在登錄頁面中輸入一個select框即可在兩種不同類型之間。現在很容易檢索價值並做出選擇。