vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB4 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=252)
-   -   Plugin Query? (https://vborg.vbsupport.ru/showthread.php?t=285799)

Craigr 07-21-2012 11:49 AM

Plugin Query?
 
At the moment i have this as a plugin, it shows the first result ok, but sometimes i have more than 1 result and the rest are not appearing, any help appreciated:

Code:

$image = $vbulletin->db->query("SELECT * FROM ".TABLE_PREFIX."dbtech_gallery_images WHERE roll_id = '$threadid'");
    while ($row = $vbulletin->db->fetch_array($image))
    {
          $id = $row['imageid'];
          $title = $row['title'];

$templater = vB_Template::create('test');
$templater->register('id', $id);
$templater->register('title', $title);
$templatevalues['my_insertvar'] .= $templater->render();
vB_Template::preRegister('SHOWTHREAD', $templatevalues);
    }

my template called test:
Code:

{vb:raw title} {vb:raw id}

cellarius 07-21-2012 12:35 PM

You're overwriting results with every while loop. Get the template stuff out of the while loop, and make sure your variable does not get overwritten (save the results into an array, for example.

Craigr 07-21-2012 01:01 PM

Quote:

Originally Posted by cellarius (Post 2350115)
You're overwriting results with every while loop. Get the template stuff out of the while loop, and make sure your variable does not get overwritten (save the results into an array, for example.

Thanks the above does seem to work, but as you said i suspected i had the coding done wrong.

Sorry i am a newbie with vBulletin coding as you can see and am trying to convert my old site so it works pretty much the same.

I have edited my my code to look like this, but nothing appears:
Code:

$image = $vbulletin->db->query("SELECT * FROM ".TABLE_PREFIX."dbtech_gallery_images WHERE roll_id = '$threadid'");
    while ($row = $vbulletin->db->fetch_array($image))
    {
$imageresults = array();
$imageresults['imageid'] = $row['imageid'];
$imageresults['title'] = $row['title'];
$imageresults['filename'] = $row['filename'];
    }

$templater = vB_Template::create('test');
$templater->register('displayres', $imageresults);
$templatevalues['my_insertvar'] .= $templater->render();
vB_Template::preRegister('SHOWTHREAD', array('$templatevalues'));

AND IN test TEMPLATE:
Code:

{vb:raw displayres.id}
{vb:raw displayres.title}
{vb:raw displayres.filename}


kh99 07-21-2012 01:09 PM

Are you trying to display one image or multiple images? If it's multiple images (Edit: and I reread your first post and I see now that's what the issue is), I think your original code was closer to being correct, you only need to move the preRegister call out of the loop.

Another way to do the same thing would be to collect up the rows in an array of arrays, then use vb:each in the template (which maybe is what cellarius was thinking). But it should work the way you have it.

Craigr 07-21-2012 01:17 PM

Quote:

Originally Posted by kh99 (Post 2350123)
Are you trying to display one image or multiple images? If it's multiple images (Edit: and I reread you first post and I see now that's what the issue is), I think your original code was closer to being correct, you only need to move the preRegsiter call out of the loop.

Another way to do the same thing would be to collect up the rows in an array of arrays, then use vb:each in the template (which maybe is what cellarius was thinking). But it should work the way you have it.

Code:

$image = $vbulletin->db->query("SELECT * FROM ".TABLE_PREFIX."dbtech_gallery_images WHERE roll_id = '$threadid'");
    while ($row = $vbulletin->db->fetch_array($image))
    {
          $id = $row['imageid'];
          $title = $row['title'];

$templater = vB_Template::create('test');
$templater->register('id', $id);
$templater->register('title', $title);
$templatevalues['my_insertvar'] .= $templater->render();
    }
vB_Template::preRegister('SHOWTHREAD', $templatevalues);

Is this better? It seems to display the results.

Not sure how i would convert the results into an array? Could you give me a short example how i would make it like that so i could follow that procedure in future, have looked at loads of examples on the site here, but cannot for the life of me get it working. :(

cellarius 07-21-2012 01:19 PM

I may be in weekend mode, but how would showing multiple images work with the first code given? $id and $tile would always end up being the values of the last element (i.e. they'd be overwritten with every loop).

For the second try in #3: Still the results are overwritten with every loop. Try something like that:
PHP Code:

$imageresults = array();
$imageresults['imageid'] = $row['imageid'];
$imageresults['imageid']['title'] = $row['title'];
$imageresults['imageid]['filename'] = $row['filename']; 

The imageid in the multidimensinal array keeps the values from being overwritten.

Then, indeed, do a vb:each in the template.

kh99 07-21-2012 01:22 PM

Quote:

Originally Posted by cellarius (Post 2350126)
I may be in weekend mode, but how would showing multiple images work with the first code given? $id and $tile would always end up being the values of the last element (i.e. they'd be overwritten with every loop).

Well, I think because the 'test' template is being rendered each time, it doesn't matter if $id and $title are reused (unless I'm missing something).

cellarius 07-21-2012 01:33 PM

Ah, yes, of course, the old fashioned way :D As I said, weekend mode...

Craigr 07-21-2012 01:47 PM

Cool thanks for all your help kh99 and cellarius, tried to like your post cellarius, but it appears i have liked too many of them already. :D

I will go ahead and use post 5 as it seems less complicated for me. Would like to work out arrays in the future. :)

kh99 07-21-2012 02:22 PM

There's a small example of how to use vb:each in the vbulletin manual: https://www.vbulletin.com/docs/html/...mplates_syntax (it's near the bottom).


All times are GMT. The time now is 10:00 AM.

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.01193 seconds
  • Memory Usage 1,749KB
  • 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
  • (5)bbcode_code_printable
  • (1)bbcode_php_printable
  • (3)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (10)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
  • pagenav_page
  • pagenav_complete
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete