vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB4 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=252)
-   -   Display the result of a query in postbit (https://vborg.vbsupport.ru/showthread.php?t=319705)

Bastien 08-02-2015 08:26 AM

Display the result of a query in postbit
 
1 Attachment(s)
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 :

PHP Code:

$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 :(

Code:

<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:

kh99 08-02-2015 09:24 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:
PHP Code:

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

Quote:

Originally Posted by kh99 (Post 2551943)
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:
PHP Code:

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 [DATE]1438595546[/DATE] at [TIME]1438595546[/TIME] ---------------

It works fine :)

Code:

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

Quote:

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:

kh99 08-03-2015 08:48 AM

What I meant is something like this:

PHP Code:

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:
PHP Code:

$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.


All times are GMT. The time now is 12:27 PM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.00996 seconds
  • Memory Usage 1,747KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (2)bbcode_code_printable
  • (5)bbcode_php_printable
  • (2)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (4)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete