Undefined Variable

Cupara

Well-known member
No matter what I have done I still get undefined variable of $pointsEarned but yet it worked before then I updated it to 1.2 and added more features now I get this error when trying to make a new thread or post a reply.

PHP:
  protected function _postSaveAfterTransaction()
   {
     $userId = $this->get('user_id');
     $options = XenForo_Application::get('options');

     if($options->xfpoints_currency_size > 0)
     {
           if (xfPoints_Check::checkPostQuality($this) && $this->get('message_state') == 'visible')
           {
                 if ($this->isInsert() || $this->getExisting('message_state') == 'moderated')
                 {
                   if ($this->get('position') == 0)
                   {
                     $pointsModel = $this->getModelFromCache('xfPoints_Model_Currency');
                     $pointsEarned = $pointsModel->assignUserPoints($userId, 'thread') + $pointsModel->assignUserPoints($userId, 'size');
                     $pointsModel->updateUserPoints($userId, $pointsEarned);
                   }
                 }
           }
       } else {
         if ($this->get('message_state') == 'visible')
           {
               // add points to this user's total
               if ($this->get('message_state') == 'visible')
               {
                 if ($this->isInsert() || $this->getExisting('message_state') == 'moderated')
                 {
                   $pointsModel = $this->getModelFromCache('xfPoints_Model_Currency');

                   // add points to this user's total
                   if ($this->get('position') == 0) // new thread
                   {
                     $pointsModel = $this->getModelFromCache('xfPoints_Model_Currency');
                     $pointsEarned = $pointsModel->assignUserPoints($userId, 'thread');
                     $pointsModel->updateUserPoints($userId, $pointsEarned);
                   }
                 }
               }
           }
       }

     return parent::_postSaveAfterTransaction();
   }
 
That code you have posted definitely wouldn't throw an undefined variable error because regardless of what path is taken through the code wherever $pointsEarned is used, it is defined just above it.

My suspicion is that variable is used elsewhere.

Of course, the error will say undefined variabe XXX on line XXX so post the full error and stack trace. It might tell us more.

EDIT: Just a minor point:

Why do you do:

PHP:
if ($something)
{
    // something
}
else
{
    if ($somethingelse)
    {
        // something else
    }
}

instead of:

PHP:
if ($something)
{
    // something
}
elseif ($somethingelse)
{
    // something else
}
 
The other line it refers to is in my points model.
PHP:
return $pointsEarned ? $pointsEarned : false;

Full error:
Undefined variable: pointsEarned
  1. XenForo_Application::handlePhpError() in xfPoints/Model/Currency.php at line 388
  2. xfPoints_Model_Currency->assignUserPoints() in xfPoints/DataWriter/DiscussionMessage/Post.php at line 21
  3. xfPoints_DataWriter_DiscussionMessage_Post->_messagePostSave() in XenForo/DataWriter/DiscussionMessage.php at line 587
  4. XenForo_DataWriter_DiscussionMessage->_postSave() in XenForo/DataWriter.php at line 1397
  5. XenForo_DataWriter->save() in XenForo/ControllerPublic/Thread.php at line 548
  6. XenForo_ControllerPublic_Thread->actionAddReply() in XenForo/FrontController.php at line 337
  7. XenForo_FrontController->dispatch() in XenForo/FrontController.php at line 134
  8. XenForo_FrontController->run() in public_html/index.php at line 13

Note sure what else to show considering my points model is very big.
 
Yeah it's nothing to do with the code you posted before.

You should always define variables as early on in your code as possible, e.g.

PHP:
public function someFunction($someCriteria)
{
    $pointsEarned = 0;

    if ($someCrtieria)
    {
        $pointsEarned = 123;
    }

    return $pointsEarned;
}

So you don't need to get PHP to do any sort of conditional, e.g.
PHP:
return $pointsEarned ? $pointsEarned : false;

In my code example $pointsEarned will always be defined as either 0 or 123.
 
Ok instead of doing if and result being $pointsEarned = so and so I am doing return so and so eliminating the use for $pointsEarned ? $pointsEarned : false;.

After that the problem lies in I am grabbing data wrong.
PHP:
Mysqli statement execute error : Column 'xfpoints_currency' cannot be null
[LIST=1]
[*][B]Zend_Db_Statement_Mysqli->_execute()[/B] in [B]Zend/Db/Statement.php[/B] at line [B]317[/B]
[*][B]Zend_Db_Statement->execute()[/B] in [B]Zend/Db/Adapter/Abstract.php[/B] at line [B]479[/B]
[*][B]Zend_Db_Adapter_Abstract->query()[/B] in [B]xfPoints/Model/Currency.php[/B] at line [B]398[/B]
[*][B]xfPoints_Model_Currency::updateUserPoints()[/B] in [B]xfPoints/DataWriter/DiscussionMessage/Post.php[/B] at line [B]24[/B]
[*][B]xfPoints_DataWriter_DiscussionMessage_Post->_messagePostSave()[/B] in [B]XenForo/DataWriter/DiscussionMessage.php[/B] at line [B]587[/B]
[*][B]XenForo_DataWriter_DiscussionMessage->_postSave()[/B] in [B]XenForo/DataWriter.php[/B] at line [B]1397[/B]
[*][B]XenForo_DataWriter->save()[/B] in [B]XenForo/ControllerPublic/Thread.php[/B] at line [B]548[/B]
[*][B]XenForo_ControllerPublic_Thread->actionAddReply()[/B] in [B]XenForo/FrontController.php[/B] at line [B]337[/B]
[*][B]XenForo_FrontController->dispatch()[/B] in [B]XenForo/FrontController.php[/B] at line [B]134[/B]
[*][B]XenForo_FrontController->run()[/B] in [B]public_html/index.php[/B] at line [B]13[/B]
[/LIST]
 
Weird I started using
PHP:
$this->get('last_post_user_id');

It posts but does not give points soooooo there lies another issue.
 
Yeah.
PHP:
return $pointsEarned ? $pointsEarned : false

Will still only work properly if $pointsEarned is actually defined in the first place. That'll be what is throwing the undefined variable error.

$var ? $var : false is just shorthand for:

PHP:
if ($var)
{
    return $var;
}
else
{
    return false;
}

So as you would expect, $var still has to be defined somewhere.

The error you're getting now suggests you're passing data incorrectly to the database. Check what your column definition is for xfpoints_currency. It sounds like you're passing NULL to it, yet the column definition is NOT NULL.

Does the column have a default value? If it's an integer field it should have a default value of 0 really. (Thus why my example used $pointsEarned = 0).
 
Oh it has a default value of 0 in the database but now it resets all points when creating a new thread and posting gives no points so the fun part is figuring out why that is.
 
@Chris Deeming

Ok here is my entire Currency.php file. Found out that the error is coming from that file. I added $pointsEarned = 0; so if you could look at the file and see if maybe you can see why it is always returning the 0 instead of assign points in each case of 'post' and 'thread'.
 
Last edited:
I can't see anything immediately wrong.

You should use various debug tools such as print_r or more preferably,

Zend_Debug::dump($pointsEarned);

At various points in your code so you can see clearly what the values are or where the value is changing to 0.
 
Thank you, I couldn't remember the dump line nor could I find it in a search. LOL

I know that $pointsEarned isn't getting passed so I'll do the dump in various locations one at a time.
 
I figured it out, I was missing a } to close off switch($type), wasn't supposed to close after all my if conditions but after cases.
 
Top Bottom