2015-07-13 59 views
0

檢查用戶類型我有我的控制器功能:中的if/else不工作笨

public function verify() 
    { 
    $token= $this->uri->segment(3); 
    $email_verification=$this->load->site_model->verifyEmailAddress($token); 

     if ($email_verification=== FALSE) 
     { 
      redirect('site/index'); 
     } 
     else 
     { 



$type=$this->load->site_model->select1($token); 
      //print_r($type); 
      if($type['user_type']=="Employer"){ 
       redirect('site/emp'); 
       } 
       else{ 
        redirect('site/seek'); 
       } 

     } 
    } 

,我的模式是:

<?php 
class Site_model extends CI_Model 
{ 
    public function __construct() 
    { 
     parent:: __construct(); 
     $this->load->database(); 

    } 
    public function insert($token) 
    { 
     $data = array(
     'name'=>$this->input->post('name'), 
     'email'=>$this->input->post('email'), 
     'phone'=>$this->input->post('phone'), 
     'user_type'=>$this->input->post('utype'), 
     'token'=>$token, 
     ); 

     $this->db->insert('tbl_user',$data); 

     $email=$this->input->post('email'); 
     $name=$this->input->post('name'); 
     $html ="http://localhost/jobs/hmvc/index.php/site/verify/".$token; 
     $config = array(
     'protocol' => 'smtp', 
     'smtp_host' => 'ssl://smtp.googlemail.com', 
     'smtp_port' => '465', 
     'smtp_user' => '[email protected]', 
     'smtp_pass' => 'something', 
     'mailtype' => 'html', 
     'starttls' => true, 
     'newline' => "\r\n" 
     ); 
     $this->load->library('email',$config); 
     $this->email->From("[email protected]"); 
     $this->email->to($email); 
     $this->email->subject('test'); 
     $this->email->message('<b>Hi '.$name.' </b><p>Welcome! You’re almost done.!Click the link to confirm your email address..</p>'.$html); 
     //$this->email->send(); 

     if($this->email->send()) { 
      echo '<script>alert("Email sent successfully")</script>'; 
      } else { 
     $this->email->print_debugger(); 
     } 

    } 


    public function verifyEmailAddress($token) 
     { 
     $data=array('email_verification'=>1); 
       $this->db->where('token',$token); 
       $this->db->update('tbl_user',$data); 
        return true; 

     } 
public function select1($token) {   
        $this->db->select('user_type'); 
        $this->db->from('tbl_user'); 
        $this->db->where('token',$token); 
        return $this->db->get()->row(); 
    } 


?> 

而EM運行錯誤顯示爲致命錯誤的代碼:第89行中的C:\ xampp \ htdocs \ jobs \ hmvc \ application \ modules \ site \ controllers \ site.php中不能使用類型爲stdClass的對象

我該如何解決這個問題? codeigniter中的這個錯誤是什麼意思?

+2

'insert()'沒有返回任何東西。 –

+0

'$ type ['user_type'] ???'你的$類型值不包含任何東西,並且總是去其他條件。 –

+0

那我該怎麼辦? @ Shaiful Islam –

回答

0
<?php 
public function insert($token) 
    { 
     $data = array(
     'name'=>$this->input->post('name'), 
     'email'=>$this->input->post('email'), 
     'phone'=>$this->input->post('phone'), 
     'user_type'=>$this->input->post('utype'), 
     'token'=>$token, 
     ); 

     $this->db->insert('tbl_user',$data); 
     $ret =$this->db->insert_id(); 


     $email=$this->input->post('email'); 
     $name=$this->input->post('name'); 
     $html ="http://localhost/jobs/hmvc/index.php/site/verify/".$token; 
     $config = array(
     'protocol' => 'smtp', 
     'smtp_host' => 'ssl://smtp.googlemail.com', 
     'smtp_port' => '465', 
     'smtp_user' => '[email protected]', 
     'smtp_pass' => 'something', 
     'mailtype' => 'html', 
     'starttls' => true, 
     'newline' => "\r\n" 
     ); 
     $this->load->library('email',$config); 
     $this->email->From("[email protected]"); 
     $this->email->to($email); 
     $this->email->subject('test'); 
     $this->email->message('<b>Hi '.$name.' </b><p>Welcome! You’re almost done.!Click the link to confirm your email address..</p>'.$html); 
     //$this->email->send(); 

     if($this->email->send()) { 
      echo '<script>alert("Email sent successfully")</script>'; 
      } else { 
     $this->email->print_debugger(); 
     } 

     if($ret){ 
     // $this->db->where('id', $ret); 
     // $query=$this->db->get('tbl_user'); 
     // $result = $query->result_array(); 
     // return $result; 
      return $this->select1($token); 
     } 

    } 
public function select1($token) { 
    $this->db->select('user_type'); $this->db->from('tbl_user'); 
    $this->db->where('token',$token); 
    return $this->db->get()->row(); 
} 

您可以嘗試插入此功能。請使用相同的型號製作此功能。

+0

請檢查我的新答案 –

0

在控制器

public function verify() 
    { 
     $token= $this->uri->segment(3); 
     $email_verification = $this->load->site_model->verifyEmailAddress($token); 

     if ($email_verification != 1) 
     { 
      redirect('site/index'); 
     } 
     else 
     { 
      $type=$this->load->site_model->select1($token); 
      //print_r($type); 
      if($type['user_type']=="Employer"){ 
       redirect('site/emp'); 
      } 
      else{ 
       redirect('site/seek'); 
      } 

     } 
    } 

在型號

public function verifyEmailAddress($token) 
    { 
     $data=array('email_verification'=>1); 
     $this->db->where('token',$token); 
     $this->db->update('tbl_user',$data); 
     return 1; 

    } 
    public function select1($token) 
    { 
     $query = $this->db->query("SELECT user_type FROM tbl_user WHERE token = '$token' "); 
     $result = $query->result_array(); 
     return $result; 
    } 
0

我知道我可能1年會遲到,但所有的答案都只是代碼,而計算器答案應該更多的指導解決問題。

你得到這個錯誤是因爲模型的選擇一函數返回一個對象的原因:return $this->db->get()->row();

而在你的控制器保存返回值

$type=$this->load->site_model->select1($token); 

再後來就,您有if-else語句,其中的條件是:

$type['user_type']=="Employer"; 

您試圖使用密鑰(索引)"user_type"(它是一個數組)獲取變量$type,而您的模型將返回該對象。

因此,解決方案是從控制器中的$type['user_type']=="Employer"更改爲$type->user_type == "Employer",該控制器實際上是指名爲「user_type」的對象的屬性。