MyBB Community Forums

Full Version: [SOLVED] Problem using $post['dateline'] - solved
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi!

It's probably something easy to spot but somehow I fail to do so.  Dodgy

I'm working on a plugin that updates the users table in the database with the dateline of a post - based on a list of fids.
This is the hook I use:
$plugins->add_hook("datahandler_post_insert_post", "lastippost_insert");   

and this is the function:


function lastippost_insert()
{
        global $db, $mybb, $post;

        if ($mybb->settings['lastippost'] != '')
        {
                $forenids = explode(",", trim($mybb->settings['lastippost']));
                if (in_array($post['fid'], $forenids))
                {
                        $db->update_query("users",array('lastippost' => (int)$post['dateline']),"uid = '".$post['uid']."'");
                }
        }
}

My problem there is the part with $post['dateline']. I all works fine if I fill the array with a set number or stuff like time() but as soon as I try to fill it with the value from the other array all I get is a 0 in the "lastippost" field in the database. However it seems to work just like that in the datahandler/post.php file itself.

Do you have any idea why it doesn't work?

Many thanks in advance!

Senya
Why are you accessing globalizing $post and accessing it by reference? Data is already passed as the 1st argument in your hook..
To be honest I had a look at other plugins and in one it was solved like this. But you are right, thanks for pointing that out. Smile
[I changed it in my above post now]

However that does not solve my "dateline"-issue... Any ideas on that?
Try this:
function lastippost_insert(&$post)
{
        global $db, $mybb;

        if ($mybb->settings['lastippost'] != '')
        {
                $forenids = explode(",", trim($mybb->settings['lastippost']));
                if (in_array($post['fid'], $forenids))
                {
                        $db->update_query("users",array('lastippost' => (int)$post['dateline']),"uid = '".$post['uid']."'");
                }
        }
} 

Lemme know if that helps! Haven't tested it though so no promises.
I also need to ask what are you trying to do? It seems like you're just trying to re do the basic lastippost functionality in a plugin?
Thank you for your suggestion.

However that throws an error so its's not an optipon:
Fatal error: Cannot use object of type PostDataHandler as array in .../inc/plugins/lastippost.php on line 148
I can use &$this instead of &$post but that doesn't solve the problem.

Any other ideas?

To answer your question:
It is a bit like the lastpost functionality yes, but I need it to mark when a user has posted in a preset forum (that is within $forenids). I later use this date to show in the profile when the user has last posted in the rp area of the forum. There are other things that I connect to that later on.
1. Don't use $this name.
2. It threw an error because an object != an array.. The code above tries to access non-existing indexes. As I mentioned, you need to access data and you can find how it's done in the datahandler: https://github.com/mybb/mybb/blob/featur...t.php#L834
$post = $variable_passed_by_reference->data;
@Destroy666
I spent the last 3 hours trying to understand objects which I had tried and given up on years ago Confused Allthough I now have a vague idea, I still don't understand what I have to put there. I also don't get why $post['uid'] and $post['fid'] work in that surrounding if $post['dateline'] doesn't. Shouldn't they cause the same problem?

I mean why does this work when the code from my first post doesn't?
function lastippost_insert()
{
        global $db, $mybb, $post;

        if ($mybb->settings['lastippost'] != '')
        {
                $forenids = explode(",", trim($mybb->settings['lastippost']));
                if (in_array($post['fid'], $forenids))
                {
                        $db->update_query("users",array('lastippost' => (int)$post['tid']),"uid = '".$post['uid']."'");
                }
        }
}

Could you try to explain in more detail how I can access data? As in for dummies, please? Big Grin

Ok thanks to StefanT I now know the solution and understand the "access data" bit.

In the function $datahandler needs to be passed on and whereever I used $post it has to be something like: $datahandler->data['fid']

Thank you for the help even though I didn't quite get it Big Grin