View Full Version : Display the result of a query in postbit
Bastien
08-02-2015, 08:26 AM
Hi all,
I'm wanting to display in the postbit, the result of a query but that doesn't work and I don't understand why.
So, this is my table fournisseurs : SC1
Here is where I want to display its phone number (alt of the img) : SC2
This is the hook I added for showpost_start :
$fournisseur = $vbulletin->db->query_first("
SELECT telephone
FROM devis_fournisseurs
WHERE userid=$post[userid]
");
$telephone="$fournisseur[telephone]";
return $telephone;
And this is the little part that displays the phone picture and should display the number, but it doesn't :(
<vb:if condition="$post[usergroupid]==10 || $post[usergroupid]==11">
<img src="images/smilies/telephone.png" alt="test {vb:raw telephone}"></vb:if>
Could someone give me some help ?
Thank you:up:
It's kind of tricky selecting hooks just based on the name. Anyway, I would use postbit_display_complete instead of showpost_start.
You don't want to return from your plugin, because that could keep other plugins on the same hook from running. What you want to do is register your variable to the postbit template. Here's an article on rendering templates: https://vborg.vbsupport.ru/showthread.php?t=228078
But your code would be something like:
global $vbulletin;
$fournisseur = $vbulletin->db->query_first("
SELECT telephone
FROM devis_fournisseurs
WHERE userid=$post[userid]
");
vB_Template::preRegister('postbit', array('telephone' => $fournisseur[telephone]));
You might also want to keep an array of userid => telephone and check that before doing a query, that way you won't have to query the same user more than once. Note that postbit_display_complete is called from inside a function, so you need to use a 'global' statement for any global variables you want to use.
Bastien
08-03-2015, 07:05 AM
It's kind of tricky selecting hooks just based on the name. Anyway, I would use postbit_display_complete instead of showpost_start.
You don't want to return from your plugin, because that could keep other plugins on the same hook from running. What you want to do is register your variable to the postbit template. Here's an article on rendering templates: https://vborg.vbsupport.ru/showthread.php?t=228078
But your code would be something like:
global $vbulletin;
$fournisseur = $vbulletin->db->query_first("
SELECT telephone
FROM devis_fournisseurs
WHERE userid=$post[userid]
");
vB_Template::preRegister('postbit', array('telephone' => $fournisseur[telephone]));
You might also want to keep an array of userid => telephone and check that before doing a query, that way you won't have to query the same user more than once. Note that postbit_display_complete is called from inside a function, so you need to use a 'global' statement for any global variables you want to use.
Thank you kh, great article ! :)
I had tried with preregister function, but had no success, so I tried the return with no more ...
I try again :up:
--------------- Added 1438595546 at 1438595546 ---------------
It works fine :)
global $vbulletin;
$fournisseur = $vbulletin->db->query_first("
SELECT telephone
FROM devis_fournisseurs
WHERE userid=$post[userid]
");
$telephone = $fournisseur[telephone];
vB_Template::preRegister('postbit_legacy', array('telephone' => $telephone));
But you said
that way you won't have to query the same user more than once
Could ou help me with it ?
I tried recording in an array and check them with in_array but no success :confused:
What I meant is something like this:
global $vbulletin, $cache_telephone;
if (!isset($cache_telephone[$post['userid']]))
{
$fournisseur = $vbulletin->db->query_first("
SELECT telephone
FROM devis_fournisseurs
WHERE userid=$post[userid]
");
if (is_array($fournisseur))
{
$cache_telephone[$post['userid']] = $fournisseur[telephone];
}
}
if (isset($cache_telephone[$post['userid']]))
{
vB_Template::preRegister('postbit_legacy', array('telephone' => $cache_telephone[$post['userid']]));
}
But I remembered a better way that might work without adding a query. You can add on to the existing post query in showthread.php using a plugin on hook showthread_query_postids and code like this:
$calc_found_rows .= ', devis_fournisseurs.telephone ';
$hook_query_joins .= ' LEFT JOIN devis_fournisseurs ON (devis_fournisseurs.userid = post.userid) ';
That should put the telephone field in $post without the other plugin.
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.