2012-03-06 6 views
0

私は以下のコードを使って、データベースでのアクティベーションを確認しています。私が今、何もしたくないのは、私を有効にする最善の方法は、特定のアクティベーションコードにリンクされているユーザー名の最初の名前を抽出することです。userFirstName?どのようにコントローラに$data['userFirstName'] = Whatとして渡すのでしょうか。Codeigniter - >その他のデータの抽出

モデル:今コントローラ側で

// setting up the query 
$this->db->select('userID, userFirstName'); 
$this->db->from('users'); 
$this->db->where('userActiveCode', $activateCode); 

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

if($result->num_rows == 1) 
{ 
    $this->db->set('userActive', 1); 
    $this->db->where('userActiveCode', $activateCode); 
    $this->db->update('users'); 

    return $result->row()->userFirstName; 
}else{ 
    return FALSE; 
}  

confirmUser:あなたはあまりにもuserFirstNameをつかむためにあなたのselect文を更新した場合、あなたはあなたの結果でそれにアクセスする必要があります

function confirmUser($activateCode) 
{ 
    if($activateCode == '') 
    { 
     return FALSE; 
    } 
//Selects the userID where the given URI activateCode = ? 

    $this->db->select('userID'); 
    $this->db->from('users'); 
    $this->db->where('userActiveCode', $activateCode); 

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

    if($result->num_rows == 1) // If the above result is = 1 then update the userActive row else it will fail 
    { 
     $this->db->set('userActive', 1); 
     $this->db->where('userActiveCode', $activateCode); 
     $this->db->update('users'); 

     return TRUE; 
    }else{ 
     return FALSE; 
    }  

} 

答えて

1

:これは、あなたがこのような何かを行うことができます。これにより、クリーンなブール値の戻り値が可能になります。

モデル

function confirmUser($activateCode, &$userFirstName) 
{ 
    if($activateCode == '') 
     return false; 

    $this->db->select('userID, userFirstName'); 
    $this->db->from('users'); 
    $this->db->where('userActiveCode', $activateCode); 

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

    if($result->num_rows == 1) 
    { 
     $this->db->set('userActive', 1); 
     $this->db->where('userActiveCode', $activateCode); 
     $this->db->update('users'); 

     $userFirstName = $result->row()->userFirstName; 
     return true; 
    } 
    else 
     return false;  
} 

発信

$userFirstName = ''; 
if ($this->user_model->confirmUser($activate_code, $userFirstName)) 
    //userFirstName will be populated 
    $data['userFirstName'] = $userFirstName; 
else 
    //userFirstName will still be empty 

場合通過ごとの基準、通過-値によって、あなたは本質的にポインタを渡す(メモリアドレス)とは対照的に実際の値ではなく値を保持しているメモリ内の場所に移動します。したがって、メソッドの値を変更すると、呼び出し元によって参照渡しされたパラメーターも変更されます。これは、両方とも同じ正確なメモリアドレスを指しているためです。

0

ユーザ名(利用可能な場合)またはfalseを返します。あなたはまた、呼び出し元にユーザーの名を返すために、モデルのメソッドに参照パラメータを使用することができます

if ($username = $my_model->confirmUser) { 
    echo 'Welcome, ' . $username; 
} else { 
    echo 'Failed checking confirmation token'; 
} 
+0

もう一度rjzさん、「TRUE」の中でこれを行うことはできますか? &モデルは値を必要とするので、私はコントローラを介してどのように渡すでしょうか? –

+0

確かに、私はもう助けてもらえませんでした!ええ、私はそれをあなたの 'TRUE'と同じ条件に入れます。実際には、戻り値として使用することを検討することもできます。暗黙の等号( '=='ではなく '===')を使用している限り、相手側では 'true'をテストします。私の答えを更新しています... – rjz

+0

ありがとう、とにかく私は$ this-> users_model-> confirmUser($ activateCode) - > userFirstNameのようにアクセスできます。 –

関連する問題