2012-03-05 94 views
1

更新时间:笨:显示动态误差

RJZ:

TdjxQetc是来自于数据库,所以当我运行/确认/我应该得到对不起,你没拥有的$activateCode正确的激活码,因为我没有通过任何变种($activateCode),但是当我运行/确认/ $activateCode我应该得到感谢您的帐户现在活动,您可以登录!并与else语句

我想它应该被改变,一个新的模型功能的开发,以检查是否userActive已被设置为1,然后显示另一个$message,这样的链接才能使用。


查看:

<div class = "messages"> 
    <?php if($confirmMessage != ''): ?> 
     <?php if($confirmError): ?> 
      <p class="error"> 
       <?php echo $confirmMessage; ?> 
       </p> 
       <?php else: ?> 
        <p class="message"> 
         <?php $confirmMessage?> 
         </p> 
         <?php endif; ?> 
         <?php endif; ?> 
    </div> 

控制器:

function confirm(){ 

     $activateCode = $this->uri->segment(3); 
     $error = FALSE; 
     $message = ''; 

     if($activateCode == '') 
     { 
      $error = TRUE; 
      $message = 'Sorry you did not have a correct Activation Code.'; 
     } 
      $userConfirmed = $this->users_model->confirm_user($activateCode); 

      if($userConfirmed){ 
       $message = 'Thanks your account is now active you may login!'; 
      }else{ 
       $error = TRUE; 
       $message = 'I am sorry we do not have any details with that Activation Code'; 
      } 
      $data['companyName'] = $this->core_model->companyDetails()->coreCompanyName; 
      $data['pageTitle'] = "User Confirm"; 
      $data['confirmError'] = $error; 
      $data['confirmMessage'] = $message; 
      $this->load->view('frontend/assets/header', $data); 
      $this->load->view('frontend/user_confirm', $data); 
      $this->load->view('frontend/assets/footer'); 
    } 

我不确定为什么我没有收到确认邮件,我只是得到我的观点。该数据库更新为1

查看:

<h1><?php echo $companyName; echo nbs(1);?> - <?php echo $pageTitle; ?></h1> 

    <p>Error: <?php echo validation_errors();?></p> 

控制器:

function confirm(){ 

     $activateCode = $this->uri->segment(3); 

     if($activateCode == '') 
     { 
      $this->form_validation->set_message('userConfirmError', 'Sorry you did not have a correct Activation Code.'); 
     } 
      $userConfirmed = $this->users_model->confirm_user($activateCode); 

      if($userConfirmed){ 
       $this->form_validation->set_message('userConfirmed', 'Thanks your account is now active you may login!'); 
      }else{ 
       $this->form_validation->set_message('userRecord', 'I am sorry we do not have any details with that Activation Code'); 
      } 
      $data['companyName'] = $this->core_model->companyDetails()->coreCompanyName; 
      $data['pageTitle'] = "User Confirm"; 
      $this->load->view('frontend/assets/header', $data); 
      $this->load->view('frontend/user_confirm', $data); 
      $this->load->view('frontend/assets/footer'); 
    } 

确认功能:

function confirm_user($activateCode) 
    { 
    //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); 

      return TRUE; 
     }else{ 
      return FALSE; 
     } 

核心型号:

function companyDetails() 
    { 
     static $details; 

     if(!$details) 
     { 
      $this->db->select('coreCompanyName, coreContactName, coreContactEmail'); 
      $details = $this->db->get('core')->first_row(); 
     } 
     return $details; 
    } 
+0

可以肯定地说,你在查看时回显了表单错误..? – 2012-03-05 05:11:19

+0

@Sudhir我已经包含了我的观点。 – 2012-03-06 00:00:34

回答

1

你是种在这里的一个山痣山这里杰斯。让我们看看我们能做些什么来打扫一下:

控制器方法

function confirm() 
{ 
    $activate_code = $this->uri->segment(3); 

    if(!$this->users_model->confirm_user($activate_code)) 
     $error = true; 
    else 
     $error = false; 

    $data['companyName'] = $this->core_model->companyDetails()->coreCompanyName; 
    $data['pageTitle'] = "User Confirm"; 
    $data['confirmError'] = $error; 
    $this->load->view('frontend/assets/header', $data); 
    $this->load->view('frontend/user_confirm', $data); 
    $this->load->view('frontend/assets/footer'); 
} 

查看

<div class = "messages"> 
    <?php if($confirmError): ?> 
     <p class="error"> 
      Your activation code is invalid. 
     </p> 
    <?php else: ?> 
     <p class="message"> 
      Your account has been activated. 
     </p> 
    <?php endif; ?> 
</div> 

请将此代码添加到您的usermodel顶部:: confirm_user方法:

if($activateCode == '') 
    return false; 

我将控制器方法简化为只有两种情况 - 成功或错误。没有必要检查activate_code,因为你的模型正在为你做。

此外,我更愿意保留仅在视图中使用的视图中的字符串 - 在视图中。

+0

谢谢你,我已经尝试过,但是当我尝试/用户/确认/我得到你的帐户已被激活。有没有什么方法可以设置,抱歉,您还没有输入代码? – 2012-03-06 05:16:06

+0

如果您使用上面的代码收到该消息,则您的UserModel :: confirm_user方法对空白的激活代码返回true。 – slypete 2012-03-06 05:33:40

+0

谢谢杰西,所以我想这种方法帮助或解决了你的问题? – slypete 2012-03-06 06:07:30

0

如果您还没有准备好,你就需要调用validation_errors()助手(或笨的其他助手的错误产生的一个)在你的视图文件。

// in view file 
<form> 
    <?php echo validation_errors(); ?> 
    <!-- rest of form... --> 
</form> 

如果你调用validation_errors(),仍然没有看到输出,这可能是因为验证未运行。在此之前调用视图,你需要运行表单验证:

// in controller action function 
$this->form_validation->run(); 

随着您在上面提供的代码,仍然不会很后事 - 你还没有真正建立了验证任何规则。你会想在validation guide读了,但一般的方法是这样的:

// in controller action function 
$this->form_validation->add_rules('my_field', 'My Field Name', 'required'); 
$this->form_validation->run(); 

你可能想知道add_rules实际测试。在Codeigniter中,它通常设计用于处理来自表单的任何表单数据$_POST。要使用它直接在模型(因为它看起来就像你正在试图做的)会采取一些黑客,它可能是最简单的只使用一个标志变量和消息字符串:

function confirm($activateCode = '') { 

    $error = false; 
    $message = ''; 

    if($activateCode == '') 
    { 
    $error = true; 
    $message = 'Sorry you did not have a correct Activation Code.'; 
    } 

    $userConfirmed = $this->users_model->confirm_user($activateCode); 

    if($userConfirmed) 
    { 
    $message = 'Thanks your account is now active you may login!'; 
    } 
    else 
    { 
    $error = true; 
    $message = 'I am sorry we do not have any details with that Activation Code'; 
    } 

    $data['error'] = $error; 
    $data['message'] = $message 
} 

鉴于:

<?php if ($message != ''): ?> 
<?php if ($error): ?> 
    ERROR: <?php echo $message; ?> 
<?php else: ?> 
    SUCCESS: <?php echo $message; ?> 
<?php endif; ?> 
<?php endif; ?> 
+0

嘿,我已经包括了我的观点:) – 2012-03-06 00:00:48

+0

并更新了匹配的答案! 'form_validation'库旨在解决与您尝试在此处使用它不同的问题 - 我希望我在此处提出的替代方案能够满足您的需求。 – rjz 2012-03-06 00:24:31

+0

谢谢伙伴,我已经更新了我的问题,因为我还没有得到回应:) – 2012-03-06 01:50:32