PDA

View Full Version : Plugin Query?


Craigr
07-21-2012, 11:49 AM
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:


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

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

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

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


$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:
$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
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/main/styleref_templates_syntax (it's near the bottom).

Craigr
07-21-2012, 04:24 PM
There's a small example of how to use vb:each in the vbulletin manual: https://www.vbulletin.com/docs/html/main/styleref_templates_syntax (it's near the bottom).
I had a look at that earlier, but couldn't work out how to apply it to the above. :(

--------------- Added 1342899536 at 1342899536 ---------------

Ok one last question, i notice that the <div class="attachment"></div> appears even if nothing matches via the query, this gives a small grey box. I cannot do a conditional because i cannot use {vb:raw var} what should i use in place? I tried some various options in the PHP and the template, but i couldn't get any working. :(

Thanks. :)