1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Undefined Variable

Discussion in 'XenForo Development Discussions' started by Mythotical, Aug 16, 2013.

  1. Mythotical

    Mythotical 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();
       }
     
  2. Chris D

    Chris D XenForo Developer Staff Member

    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
    }
     
  3. Mythotical

    Mythotical Well-Known Member

    The other line it refers to is in my points model.
    PHP:
    return $pointsEarned $pointsEarned false;
    Full error:
    Note sure what else to show considering my points model is very big.
     
  4. Mythotical

    Mythotical Well-Known Member

    If need be I can zip up what I have and send to your email @Chris Deeming
     
  5. Chris D

    Chris D XenForo Developer Staff Member

    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.
     
  6. Mythotical

    Mythotical Well-Known Member

    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()[/Bin [B]Zend/Db/Statement.php[/Bat line [B]317[/B]
    [*][
    B]Zend_Db_Statement->execute()[/Bin [B]Zend/Db/Adapter/Abstract.php[/Bat line [B]479[/B]
    [*][
    B]Zend_Db_Adapter_Abstract->query()[/Bin [B]xfPoints/Model/Currency.php[/Bat line [B]398[/B]
    [*][
    B]xfPoints_Model_Currency::updateUserPoints()[/Bin [B]xfPoints/DataWriter/DiscussionMessage/Post.php[/Bat line [B]24[/B]
    [*][
    B]xfPoints_DataWriter_DiscussionMessage_Post->_messagePostSave()[/Bin [B]XenForo/DataWriter/DiscussionMessage.php[/Bat line [B]587[/B]
    [*][
    B]XenForo_DataWriter_DiscussionMessage->_postSave()[/Bin [B]XenForo/DataWriter.php[/Bat line [B]1397[/B]
    [*][
    B]XenForo_DataWriter->save()[/Bin [B]XenForo/ControllerPublic/Thread.php[/Bat line [B]548[/B]
    [*][
    B]XenForo_ControllerPublic_Thread->actionAddReply()[/Bin [B]XenForo/FrontController.php[/Bat line [B]337[/B]
    [*][
    B]XenForo_FrontController->dispatch()[/Bin [B]XenForo/FrontController.php[/Bat line [B]134[/B]
    [*][
    B]XenForo_FrontController->run()[/Bin [B]public_html/index.php[/Bat line [B]13[/B]
    [/LIST]
     
  7. Mythotical

    Mythotical Well-Known Member

    Weird I started using
    PHP:
    $this->get('last_post_user_id');
    It posts but does not give points soooooo there lies another issue.
     
  8. Chris D

    Chris D XenForo Developer Staff Member

    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).
     
  9. Mythotical

    Mythotical Well-Known Member

    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.
     
  10. Chris D

    Chris D XenForo Developer Staff Member

    Enjoy!
     
  11. Mythotical

    Mythotical Well-Known Member

    LOL yeah right, I had this problem once before but can't remember how I solved it.
     
  12. Mythotical

    Mythotical Well-Known Member

    @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: Aug 16, 2013
  13. Chris D

    Chris D XenForo Developer Staff Member

    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.
     
  14. Mythotical

    Mythotical Well-Known Member

    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.
     
  15. Mythotical

    Mythotical Well-Known Member

    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.
     

Share This Page