2012-03-09 3 views
-4

このif then else to display 2 views on one function calledスレッドでこの質問をしましたが、私は提案したとおりに修正しようとしましたが、問題は残ります。誰かが私を助けてくれることを願っています... "if"の中にあるものと "else"の中にあるものの両方をしますか?IFとELSEの両方が実行されます - 間違いを発見するために何か助けが必要です

私は「someview」という名前のコントローラ機能を持つリダイレクト機能、 で少し問題を抱えていると私も同じ名前(someview.ctp)のビューファイルを作成し コントローラ機能は、いくつかのものを行います(クエリモデルからのデータ)。 私も、私が指定したユーザーがデータベース・システムに存在しないときになど、いくつかの情報を表示したいと思います「usernotexist.ctp」という名前のページを作成し

function someview() 
{ 
    $result=$this->User->getdatafrommodel(); 
    if(!empty($result)) 
    { 
     //do something 
        sendmail(.....); 
    } 
    else 
    { 
     $this->redirect(array('action'=>'usernotexist')); 
    } 
} 

function usernotexist() 
{ 
    $this->loadSkin(); 
} 

次のように簡単に説明することができます。しかし、私の以前の関数(someview)は、呼び出された後、常に "if"と "else"の両方を実行します。私がその機能の "else"部分を取り除くと、それは私のために正しく動作します。 「someview.ctp」という名前のページが表示されます。 getdatafrommodel関数から返された$ resultの値が正しいです。 ありがとうございました。

UPDATE
これは動作します:

function someview() 
{ 
    $result=$this->User->getdatafrommodel(); 
      print_r($result); 
      exit(); 
    if(!empty($result)) 
    { 
     //do something 
    } 
    else 
    { 
     $this->redirect(array('action'=>'usernotexist')); 
    } 
} 

function usernotexist() 
{ 
    $this->loadSkin(); 
} 

これは空の配列を出力します。

function someview() 
{ 
    $result=$this->User->getdatafrommodel(); 
      print_r($result); 
    if(!empty($result)) 
    { 
     //do something 
    } 
    else 
    { 
     $this->redirect(array('action'=>'usernotexist')); 
    } 
} 

function usernotexist() 
{ 
    $this->loadSkin(); 
} 

[UPDATE]

私は上記のように私は、コントローラのメソッドを呼び出すためにページを「フォルダ/サブフォルダ/ someview」と呼ばれるビュー内のリンクをクリックした後、私はSEE」 ELSE "がリダイレクトされます。しかしIF部には私のアカウントに電子メールを送信する "Sendmail"機能も含まれています。 sendmailが動作します。それは私が理解したいと思う奇妙なことです。明らかにIF then Elseが実行されます。

コントローラメソッド(someview)をアクティブにするためのリンクをユーザがクリックした直後です。クエリが空のレコードを返すと、私は "usernotexist"というページに送られます。そうでなければ電子メール。

元のソースコードを投稿することに同意します。私はsendmailの機能が現在どこかで間違いを犯している可能性があると考えています。ここで

それは

function get_and_change_user_password($email) 
    { 
     $password=$this->genRandStr(); 
     $sql=sprintf("UPDATE ewt_users SET password='%s' WHERE email='%s'",md5($password),$email); 
     $this->query($sql); 
     return $password; 
    } 

そして、ここでデータを照会する

function newpassword() 
    { 
     $this->loadSkinForAction(); 
     $result=$this->EwtUser->get_user_from_email($_POST['email']); 

     if(!empty($result)) 
     { 
      $userid = $result[0]['ewt_users']['id']; 
      $password=$this->EwtUser->get_and_change_user_password($_POST['email']);    
      $mail="Your new password is: ".$password."<br/>Please use it for next login.<br/>You are recommended to change this password again in your 'Personal Profile' section."; 
      $this->sendmail($userid,$mail,$_POST['email']); 
     } 
     else 
     { 
      //print_r($result); 
      $this->redirect(array('action'=>'userexists')); 
     } 
    } 

    function userexists() 
    { 
     $this->loadSkinForAction();  
    } 

とモデル関数であるsendmailの機能であり、

再び
function sendmail($userid, $reportcontent,$email){ 
     //if($this->Session->read($this->_userName)) 
     { 
      $this->loadModel('EwtMailtemplate'); 
      $this->loadModel('EwtUser'); 
      $this->loadModel('EwtSetting'); 
      $this->autoRender = false; 

      $date = date("Y-m-d"); 
      $userinfo = $this->EwtUser->read(null, $userid); 
      $fullname = $userinfo['EwtUser']['fullname']; 
      $lastname = $userinfo['EwtUser']['lastname']; 
      $mailtempl = $userinfo['EwtUser']['mailtempl']; 
      if ($mailtempl == 0) { 
       $mailtempl = 1; 
      } 

      $setting = $this->EwtSetting->find('first'); 
      $mailhost = $setting['EwtSetting']['mailhost']; 
      $mailuser = $setting['EwtSetting']['mailuser']; 
      $mailpass = $setting['EwtSetting']['mailpass']; 
      //$reportmail = $setting['EwtSetting']['reportmail']; 
      $reportmail=$email; 
      $bodymail = $this->EwtMailtemplate->read(null, $mailtempl); 
      //$header = $bodymail['EwtMailtemplate']['header']; 
      //$footer = $bodymail['EwtMailtemplate']['footer']; 
      //$title = $bodymail['EwtMailtemplate']['title']; 
      $subject="New login password for working time system"; 
      //$subject = $lastname . " " . str_replace("[date]", $date, $title); 
      //$header = str_replace("[lastname]", $lastname, $header); 
      //$header = str_replace("[date]", $date, $header); 
      //$footer = str_replace("[lastname]", $lastname, $footer); 

      //$content = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><html><head><meta http-equiv="Content-Type" content="text/html; charset =utf-8" /></head><body>'.$header ."<br />" . $reportcontent . "<br />" . $footer . '</body></html>'; 

      $content = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"><html><head><meta http-equiv="Content-Type" content="text/html; charset =utf-8" /></head><body>'."<br />".$reportcontent."<br />".'</body></html>'; 

      $this->Email->to = $reportmail; 
      $this->Email->charset = 'UTF-8'; 
      $this->Email->from = sprintf("%s <%s>", $fullname, $mailuser); 
      $this->Email->replyto = sprintf("%s <%s>", $fullname, $mailuser); 
      $this->Email->subject = $subject; 
      $this->Email->sendAs ='html'; 
      $smtp = array(
         'port'=>25, 
         'host'=>$mailhost, 
         'timeout'=>99, 
         'username'=>$mailuser, 
         'password'=>$mailpass 
      ); 
      $this->Email->smtpOptions = $smtp; 
      $this->Email->delivery = 'smtp'; 

      if ($this->Email->send($content)) { 
       $this->redirect('newpassword');   
      } else { 
       $this->redirect('userexists'); 
      } 

      $smtp_error = $this->Email->smtpError; 
      if (strlen($smtp_error)>0){ 
       $this->Session->setflash($smtp_errors); 
      } 
     } 
    } 

それは両方のIFを実行し、 ELSE支店 ありがとうございますあなたの懸念と私は本当にあなたからのコメントやアドバイスを聞いて欲しいです:-)

私のポストは長すぎます。私はすぐに系統樹を作ることができます! :-D-D

+0

'$ this-> sendmail'の機能は何ですか?そしてあなたの質問を統合しようとしてください、これは基本的に同じトピックについての第4です。 – deceze

+0

ありがとうございました、私は上記のように含める、 – PartialDue

+0

私は長い投稿をして賞を与えられますか?私の今はStackoverflowの歴史の中で最も長い投稿です:-) – PartialDue

答えて

3

sendmail機能の内部をリダイレクトしています。

if ($this->Email->send($content)) { 
    $this->redirect('newpassword');   
} else { 
    $this->redirect('userexists'); 
} 
+0

huhu huhu:泣いています::::::ありがとう、私はそれを見ませんでした。 :=(詐欺のために100万のおかげで、最高! – PartialDue

+0

上記のソーススナップに私に助言したりしないことは他にありますか? – PartialDue

関連する問題