0

I get Undefined variable: user error in my controller:

And this is my controller:

public function actionduration()
{
    if (isset($_POST['W'])&&isset($_POST['Nodelist']))
    {
        $user = Sensor::model()->Week();
    }
    else if (isset($_POST['M'])&&isset($_POST['Nodelist']))
    {
        $user = Sensor::model()->Month();
    }
    else if(isset($_POST['S'])&&isset($_POST['Nodelist']))
    {
        $user = Sensor::model()->Six();
    }
    else if(isset($_POST['Y'])&&isset($_POST['Nodelist']))
    {
        $user = Sensor::model()->Year();
    }
    //print_r($user);

    $this->layout='main2';
    //$layout='//layouts/main1';
    $this->render('edit1', array('user'=>$user));
}
5
  • It would be good to post here your complete code. And $user is undefined, because you did not define $user in your controller. Commented May 9, 2014 at 11:49
  • but i am not able put all code here , i got error , Commented May 9, 2014 at 11:51
  • user is already i had used in other controller action Commented May 9, 2014 at 11:52
  • 2
    I think there is a big lack of knowledge. You should first learn the basics. If you defined a variable in a function, it is only available in this function as long as it is not a class variable or global variable. Commented May 9, 2014 at 11:57
  • 1
    Check if your $_POST meets your if conditions. Commented May 9, 2014 at 12:05

2 Answers 2

1

Declare $user in starting.As your $user is not getting set in any if else statement.

Try this:

public function actionduration()
{
  $user=""; 
  $this->layout='main2';
 if (isset($_POST['W'])&&isset($_POST['Nodelist']))
  {
    $user = Sensor::model()->Week();
   }
else if (isset($_POST['M'])&&isset($_POST['Nodelist']))
{
    $user = Sensor::model()->Month();
}
else if(isset($_POST['S'])&&isset($_POST['Nodelist']))
{
    $user = Sensor::model()->Six();
}
else if(isset($_POST['Y'])&&isset($_POST['Nodelist']))
{
    $user = Sensor::model()->Year();
}

$this->render('edit1', array('user'=>$user));
}

Or You Can create one more else in last :

public function actionduration()
{
if (isset($_POST['W'])&&isset($_POST['Nodelist']))
{
    $user = Sensor::model()->Week();
}
else if (isset($_POST['M'])&&isset($_POST['Nodelist']))
{
    $user = Sensor::model()->Month();
}
else if(isset($_POST['S'])&&isset($_POST['Nodelist']))
{
    $user = Sensor::model()->Six();
}
else if(isset($_POST['Y'])&&isset($_POST['Nodelist']))
{
    $user = Sensor::model()->Year();
}
else
{
   $user=""; 
 }
//print_r($user);

$this->layout='main2';
//$layout='//layouts/main1';
$this->render('edit1', array('user'=>$user));
}
Sign up to request clarification or add additional context in comments.

Comments

0

This means your code is not initializing $user. Do a test to check if any of the if conditions are returning true. Looks like they all return false.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.