MyBB Community Forums

Full Version: How to get the tid of threads?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
Hello, I need to insert the tid of a thread in a table. I used the following hook:


$plugins->add_hook("datahandler_post_insert_thread", 'mynews_insert_data');

And in the "mynews_insert_data" I tried some codes:


$mybb->input['tid'] = $mybb->get_input('tid', 1);

 $tid = htmlspecialchars($mybb->input['tid'], ENT_QUOTES);

I also tried to include the "functions_post.php" but still not works:


require_once MYBB_ROOT."inc/functions_post.php";


But the tid field of the table doesn't receive nothing! Anyone could tell me how to get the tid of a thread?

-------------------

Edit:

I tried also with:

$post['tid'];
When in doubt, check the source code.

https://github.com/mybb/mybb/blob/21c269....php#L1415

As you can see, you need to use $thread['tid'].
(2014-12-14, 12:39 PM)Nathan Malcolm Wrote: [ -> ]When in doubt, check the source code.

https://github.com/mybb/mybb/blob/21c269....php#L1415

As you can see, you need to use $thread['tid'].

I used also $thread['tid'] but nothing, it doesn't work! In the function I specified also:


global $mybb, $lang, $db, $thread, $post;
(2014-12-14, 12:40 PM)Clear Wrote: [ -> ]I used also $thread['tid'] but nothing, it doesn't work! In the function I specified also:



global $mybb, $lang, $db, $thread, $post;

Because it's a class thus it doesn't make them globally available pretty much.
So you'll have to use $this that it provides.
(2014-12-14, 12:53 PM)Rakes Wrote: [ -> ]
(2014-12-14, 12:40 PM)Clear Wrote: [ -> ]I used also $thread['tid'] but nothing, it doesn't work! In the function I specified also:




global $mybb, $lang, $db, $thread, $post;

Because it's a class thus it doesn't make them globally available pretty much.
So you'll have to use $this that it provides.

In what way? Could you show me an example? I'm so confused now  Confused
(2014-12-14, 01:00 PM)Clear Wrote: [ -> ]
(2014-12-14, 12:53 PM)Rakes Wrote: [ -> ]
(2014-12-14, 12:40 PM)Clear Wrote: [ -> ]I used also $thread['tid'] but nothing, it doesn't work! In the function I specified also:





global $mybb, $lang, $db, $thread, $post;

Because it's a class thus it doesn't make them globally available pretty much.
So you'll have to use $this that it provides.

In what way? Could you show me an example? I'm so confused now  Confused

Haven't tested but should be inherited
https://github.com/mybb/mybb/blob/21c269....php#L1353


function mynews_insert_data(&$handler) {
    $tid = $handler->data['tid'];
}
(2014-12-14, 01:08 PM)Rakes Wrote: [ -> ]Haven't tested but should be inherited
https://github.com/mybb/mybb/blob/21c269....php#L1353



function mynews_insert_data(&$handler) {
    $tid = $handler->data['tid'];
}

It doens't work Confused I tried also to use $db->insert_query but now it doesn't insert also other values:

function mynews_insert_data() {

	global $mybb, $lang, $db, $thread, $post;
	
	if($mybb->input['mynews_choose'] == 1) {
		
		$subject_topic = $mybb->input['subject'];
		$mynews_cover = $mybb->input['mynews_cover'];
		
		$db->insert_query("mynews_topic", $mynews_insert_data_sql);
		
		$mynews_insert_data_sql = array(
				"topic" => $db->escape_string($subject_topic),
				"cover" => $db->escape_string($mynews_cover),
				"tid" => $thread['tid']
			);
			
	}
}
Why don't you use https://github.com/mybb/mybb/blob/21c269....php#L1718

Just wondering, also like I said you got to use $this (the handler class) not global variables that does not exist globally for you to use..
The hook you are using is nested into a function which is part of a class. This means that if the tid is not available globally or it hasn't been globalized in that function - which appears to be so - you won't be able to access it.

Anyway, datahandler_post_insert_thread is a hook by reference ($this is passed, which stores a lot of already parsed data, including tid), thus you must declare the argument in your function. The & behind it lets you edit the argument within your function, but if you don't need to edit it and just want to obtain the tid you can remove it.

function mynews_insert_data(&$handler)
{
     $tid = $handler->data['tid']; // This is the tid, do whatever you want with it
}

I'd recommend you to debug your code by creating a handy function, such as:

function my_debug($data)
{
    echo "<pre>";
    print_r($data);
    echo "</pre>";
    exit;
}

then place my_debug($variable) where you want to debug in your code and run the script by loading a page your script runs in a browser. This is useful also when you want to know if a variable is empty or not and see what data is available within an array, object or a simple plain string. This is very similar to what I use when writing down my PHP scripts.
(2014-12-14, 02:28 PM)Shade Wrote: [ -> ]The hook you are using is nested into a function which is part of a class. This means that if the tid is not available globally or it hasn't been globalized in that function - which appears to be so - you won't be able to access it.

Anyway, datahandler_post_insert_thread is a hook by reference ($this is passed, which stores a lot of already parsed data, including tid), thus you must declare the argument in your function. The & behind it lets you edit the argument within your function, but if you don't need to edit it and just want to obtain the tid you can remove it.


function mynews_insert_data(&$handler)
{
     $tid = $handler->data['tid']; // This is the tid, do whatever you want with it
}

I'd recommend you to debug your code by creating a handy function, such as:


function my_debug($data)
{
    echo "<pre>";
    print_r($data);
    echo "</pre>";
    exit;
}

then place my_debug($variable) where you want to debug in your code and run the script by loading a page your script runs in a browser. This is useful also when you want to know if a variable is empty or not and see what data is available within an array, object or a simple plain string. This is very similar to what I use when writing down my PHP scripts.

Mhhh, ok I wrote this:

function mynews_insert_data(&$handler) {
	global $mybb, $lang, $db, $thread, $post;
	
	if($mybb->input['mynews_choose'] == 1) {
		
		$subject_topic = $mybb->input['subject'];
		$mynews_cover = $mybb->input['mynews_cover'];
		
		$db->insert_query("mynews_topic", $mynews_insert_data_sql);
		
		$mynews_insert_data_sql = array(
				"topic" => $db->escape_string($subject_topic),
				"cover" => $db->escape_string($mynews_cover),
				"tid" => $handler->data['tid']
	    );
			
	}
}

But the table "mybb_mynews_topic" remains empty Confused What I'm wrong?

(2014-12-14, 02:27 PM)Rakes Wrote: [ -> ]Why don't you use https://github.com/mybb/mybb/blob/21c269....php#L1718

Just wondering, also like I said you got to use $this (the handler class) not global variables that does not exist globally for you to use..

If I use $this it tells me that there is no object defined $this!
Pages: 1 2 3