Php IF和ELSE都已执行--需要一些帮助来发现错误

Php IF和ELSE都已执行--需要一些帮助来发现错误,php,cakephp,cakephp-1.3,php-5.3,Php,Cakephp,Cakephp 1.3,Php 5.3,我在这篇文章中提出了这个问题,我试图按照建议解决它,但问题仍然存在。希望有人能帮我…它既做了“如果”里面的东西,又做了“其他”里面的东西 我的重定向功能有点问题, 我有一个名为“someview”的控制器函数,我还创建了一个同名的视图文件(someview.ctp) 控制器函数将执行一些操作(从模型中查询数据)。 可以简单地描述如下 function someview() { $result=$this->User->getdatafrommodel(); if(!e

我在这篇文章中提出了这个问题,我试图按照建议解决它,但问题仍然存在。希望有人能帮我…它既做了“如果”里面的东西,又做了“其他”里面的东西

我的重定向功能有点问题, 我有一个名为“someview”的控制器函数,我还创建了一个同名的视图文件(someview.ctp) 控制器函数将执行一些操作(从模型中查询数据)。 可以简单地描述如下

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

function usernotexist()
{
    $this->loadSkin();
}
我还创建了一个名为“usernotexist.ctp”的页面,我想在数据库系统中显示指定用户何时不存在的一些信息。然而,我以前的函数(someview)总是在调用后同时执行“if”和“else”。如果我删除了该函数中的“else”部分,那么它对我来说就正常工作了;将显示名为“someview.ctp”的页面。getdatafrommodel函数返回的$result值正确。 谢谢你的帮助

更新
这项工作:

    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();
}
[更新]

单击名为“folder/subfolder/someview”的视图中的链接以调用控制器方法(如上所述)后,我看到的是,“ELSE”中的页面被重定向。但在IF部分,我还包括一个“Sendmail”功能,可以向我的帐户发送电子邮件。sendmail有效。这是我想理解的奇怪的事情。很明显,如果不是这样,两个都执行了

我想做的是,在用户单击链接激活控制器方法(someview)之后,如果查询返回空记录,我将被引导到页面“usernotexist”,否则我将向他发送电子邮件

出于您的考虑,我同意发布原始源代码,我认为sendmail函数可能在某个我目前无法识别的地方出错

给你

    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);
            }
        }
    }
函数sendmail($userid、$reportcontent、$email){
//如果($this->Session->read($this->\u用户名))
{
$this->loadModel('EwtMailtemplate');
$this->loadModel('EwtUser');
$this->loadModel('EwtSetting');
$this->autoRender=false;
$date=日期(“Y-m-d”);
$userinfo=$this->EwtUser->read(null,$userid);
$fullname=$userinfo['EwtUser']['fullname'];
$lastname=$userinfo['EwtUser']['lastname'];
$mailtemp=$userinfo['EwtUser']['mailtemp'];
如果($mailtemp==0){
$mailtemp=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,$mailtemplate);
//$header=$bodymail['EwtMailtemplate']['header'];
//$footer=$bodymail['EwtMailtemplate']['footer'];
//$title=$bodymail['EwtMailtemplate']['title'];
$subject=“工作时间系统的新登录密码”;
//$subject=$lastname.“.str_替换(“[date]”,$date,$title);
//$header=str_replace(“[lastname]”,$lastname,$header);
//$header=str_replace(“[date]”,$date,$header);
//$footer=str_replace(“[lastname]”,$lastname,$footer);
//$content=''.$header.'
“$reportcontent.
“$footer.”; $content='1 “.”
“$reportcontent。”
“; $this->Email->to=$reportmail; $this->Email->charset='UTF-8'; $this->Email->from=sprintf(“%s”,$fullname,$mailuser); $this->Email->replyto=sprintf(“%s”,$fullname,$mailuser); $this->Email->subject=$subject; $this->Email->sendAs='html'; $smtp=数组( “端口”=>25, “主机”=>$mailhost, “超时”=>99, “用户名”=>$mailuser, “密码”=>$mailpass ); $this->Email->smtpOptions=$smtp; $this->Email->delivery='smtp'; 如果($this->Email->send($content)){ $this->redirect('newpassword'); }否则{ $this->redirect('userexists'); } $smtp_error=$this->Email->smtpError; 如果(strlen($smtp_错误)>0){ $this->Session->setflash($smtp\u错误); } } }
同样,它同时执行IF和ELSE分支 感谢您的关注,我非常希望听到您的任何意见或建议:-)


哇,我的帖子太长了。我很快就能画出一个进化树了-D-D

您正在
发送邮件
功能中重定向

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

$this->sendmail
功能怎么样?请尝试合并您的问题,这是关于基本相同主题的第四个问题。谢谢您,我如上所述将其包括在内,我是否会因为发表一篇长文章而获奖?我的帖子是史上最长的帖子:-)当然。投票结束并不是一个真正的问题。只是为了帮你解决问题:很明显,如果那样的话,其他人永远不会同时被处决。呼呼呼呼:哭:谢谢,我没看到=(万分感谢deceze,best!在上述源代码剪报中,您还有什么建议我不要做的吗?
if ($this->Email->send($content)) {
    $this->redirect('newpassword');         
} else {
    $this->redirect('userexists');
}