I made this question in this if then else to display 2 views on one function called thread and i have tried to fix it as what I was suggested but the problem remains. Hope someone could help me...It does both of what has inside the "if" and what has inside the "else" ?
i have got a little problem with redirect function, I have a controller function named "someview" and I also created a view file of the same name (someview.ctp) The controller function will do some stuff(query data from the model). it can be simply described as follows
function someview()
{
$result=$this->User->getdatafrommodel();
if(!empty($result))
{
//do something
sendmail(.....);
}
else
{
$this->redirect(array('action'=>'usernotexist'));
}
}
function usernotexist()
{
$this->loadSkin();
}
I also created a page named "usernotexist.ctp" which I would like to display some information as to when the specified user doesn't exist in the database system. However, my previous function (someview) always execute "if" and "else" both after it is called. If I eliminate the "else" part in that function, it then works correctly for me; the page named "someview.ctp" is displayed. The $result value returned from the getdatafrommodel function is correct. Thank you for any help.
UPDATE
this works:
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();
}
this prints an empty Array.
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]
What I SEE after I click a link in a view called "folder/subfolder/someview" to call the controller method as I described above, the page in the "ELSE" is redirected. BUT in the IF part I also include a "Sendmail" function to send an email to my account. The sendmail WORKS. That is the odd thing I would like to understand. Clearly IF then Else both are executed.
What I would like to do is right after the user click the link to activate the controller method (someview), if the query returns an empty record, I will be directed to the page "usernotexist" or I will send him an email otherwise.
For your concern I agree to post the original source code, I am thinking the sendmail function might be making some mistake somewhere I can not at present recognize its place.
Here is it
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();
}
and the model function to query data
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;
}
And here is the sendmail function,
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);
}
}
}
Again, it executes both IF and ELSE branches Thank you for your concern and I'd really love to hear any comments or advice from you :-)
WOW my post is too long. I soon can make a phylogenetic tree! :-D-D