2012-01-31, 01:07 PM
(This post was last modified: 2012-01-31, 01:09 PM by User 12076.)
Hello,
I think about use hooks to make "different" think - capture MyBB data, for example resource to DB query.
Why use that?
For example, we must do something for all posts in showthread page and get data from DB (for each post). Standard method is to use postbit hook and make queries in loop. Sorry, I don't like queries in loops - it isn't a good solution.
So i try this (it's only example):
And it works.
How do you think about that? Is this a good idea?
On what problems in your opinion it may encounter?
Priority - it's very important, what else?
I think about use hooks to make "different" think - capture MyBB data, for example resource to DB query.
Why use that?
For example, we must do something for all posts in showthread page and get data from DB (for each post). Standard method is to use postbit hook and make queries in loop. Sorry, I don't like queries in loops - it isn't a good solution.
So i try this (it's only example):
$plugins->add_hook("postbit", "reverse_inject");
$plugins->add_hook("showthread_linear", "reverse_return");
(...)
function reverse_inject(&$post)
{
global $db, $mybb, $lang, $query, $posts_tpl, $posts_ids;
static $injected = false;
// Reverse flag - UP!
if ($injected)
{
return;
}
$injected = true;
// Collect posts ids
$posts_ids = $posts_data = array();
$posts_ids[] = $post['pid'];
// Capture global DB resource
while ($row = $db->fetch_array($query))
{
$posts_ids[] = $row['pid'];
$posts_data[$row['pid']] = $row;
}
// Get all required data for all posts (1 query)
reverse_get_required_data($posts_ids);
// We must modify first post manualy
reverse_modify_post($post);
$posts_tpl = '';
foreach ($posts_data as &$post_single)
{
reverse_modify_post($post_single);
$posts_tpl .= build_postbit($post_single);
}
}
(...)
function reverse_return()
{
global $posts, $posts_tpl;
$posts .= $posts_tpl;
}
And it works.
How do you think about that? Is this a good idea?
On what problems in your opinion it may encounter?
Priority - it's very important, what else?