View Full Version : vb:each question
Boofo
01-09-2012, 12:39 PM
I'm trying to figure out the vb:each stuff in the templates and it makes sense in some areas, and so much in others not in others. I have the following query:
$referrals = $db->query_read("
SELECT username, userid
FROM " . TABLE_PREFIX . "user
WHERE referrerid = '".$userinfo['userid']."'
AND usergroupid NOT IN (3,4)
ORDER BY username
");
Currently I am using the long-hand way of doing it with the while loop, but I would like to use the vb:each in the template and I can't for the life of me get it to work from a query. Can someone point me to how to set that up properly to use the vb:each?
nhawk
01-09-2012, 03:37 PM
The template vb:each function is one of those where I always wonder why it even exists.
You need to parse your results into an array anyway so why even bother with parsing it a second time in the template? Just generate the lines in a template with each pass in PHP.
Anyway, in your PHP code get your results into an array using either a for each or a while. Then pass the results to the template. Or maybe even just pass the query results to the template...
$templater->register('referrals', $referrals);
(NOT SURE if that will work or not. I think the array with a consecutive itemid for the $key may be needed.)
Then parse it in the template...
<vb:each from="referrals" key="referrerid " value="referral">
Your template code using {vb:raw referral.whatevervalueneeded}
</vb:each>
Boofo
01-09-2012, 04:10 PM
I just can't seem to get the right results for using vb:each. I have tried numerous ways, but then it really gets confusing. I can do it from an option setting with no problem. I have never done it from a query using a loop for vb:each.
nhawk
01-09-2012, 05:24 PM
OK, try this..
Create a template called 'referrals' and put this code in it..
<vb:each from="referrals" value="referral">
, <a href="member.php?{vb:raw session.sessionurl}{vb:raw referral.userid}-{vb:raw referral.username}">{vb:raw referral.username}</a>
</vb:each>
What that boils down to in PHP is a...
foreach($referrals as $referral)
Then create a plugin using the 'forumhome_complete' hook with this code (don't do this on a live site)..
$referrals = $vbulletin->db->query_read("
SELECT username, userid
FROM " . TABLE_PREFIX . "user
WHERE referrerid = '".$userinfo['userid']."'
AND usergroupid NOT IN (3,4)
ORDER BY username
");
$ref = array();
$refkey = 0;
while ($referral = $vbulletin->db->fetch_array($referrals))
{
$ref[$refkey]['userid'] = $referral[userid];
$ref[$refkey]['username'] = $referral[username];
$refkey++;
}
$vbulletin->db->free_result($referrals);
$templater = vB_Template::create('referrals');
$templater->register('referrals', $ref);
print_output($templater->render());
The $vbulletin-> MAY or MAY NOT be needed. $db-> might be all you need.
That should output your comma separated list when you click the forum home link on the site. And, you should be able to blend that into whatever template, plugin or php code you need.
--------------- Added 1326135002 at 1326135002 ---------------
I still prefer to do the same thing in PHP with a "xxx_bits" template. Then include the raw info in my main template.
Because, as you can see, you parse the data into an array and then parse it a second time in the template.
Doing it my way with a 'xxx_bits' template, the data is only parsed once.
Boofo
01-09-2012, 06:01 PM
The above code isn't showing anything.
I am using the member_profileblock_fetch_unwrapped hook for the template render and the member_execute_start for the query as this is used in the profile. The old code I was suing worked fine in those hooks.
I did have it using a bits template but I was told that the vb:each actually does less processing, or faster processing of the list I should say, especially if you get a long enough list. I was just trying to see if I could figure it out.
nhawk
01-09-2012, 06:07 PM
I don't see why it wouldn't work. Unless the query isn't returning anything.
I tested it on a test site using a member list query. That was the only difference.
$ref = array();
$refkey = 0;
$referrals = $vbulletin->db->query_read("SELECT * FROM " . TABLE_PREFIX . "user");
while ($referral = $vbulletin->db->fetch_array($referrals)) {
$ref[$refkey]['userid'] = $referral[userid];
$ref[$refkey]['username'] = $referral[username];
$refkey++;
}
$templater = vB_Template::create('referrals');
$templater->register('referrals', $ref);
print_output($templater->render());
Boofo
01-09-2012, 06:08 PM
Maybe this has something to do with it?
$templater->register('referrals', $ref);
nhawk
01-09-2012, 06:15 PM
Maybe this has something to do with it?
$templater->register('referrals', $ref);
If $ref is empty (query returned nothing), yes that could be the problem.
Yellow Slider
01-09-2012, 09:59 PM
The template vb:each function is one of those where I always wonder why it even exists.
You need to parse your results into an array anyway so why even bother with parsing it a second time in the template? Just generate the lines in a template with each pass in PHP.
Anyway, in your PHP code get your results into an array using either a for each or a while. Then pass the results to the template. Or maybe even just pass the query results to the template...
$templater->register('referrals', $referrals);
(NOT SURE if that will work or not. I think the array with a consecutive itemid for the $key may be needed.)
Then parse it in the template...
<vb:each from="referrals" key="referrerid " value="referral">
Your template code using {vb:raw referral.whatevervalueneeded}
</vb:each>
Actually, using another template is much not inefficient.
You really should use vb:each instead of rendering another template.
The vBulletin developers are now (since 4.1.8 I believe, or was it 4.1.10?) removing most of the "bit" templates and use vb:each instead.
Boofo
01-10-2012, 02:12 AM
Yes, vb:each is nice if you understand how it works. I'm still trying to get my head around it. I can do it easily enough from a setting, but have never been able to get it working from a query.
ForumsMods
01-10-2012, 02:22 AM
$referralsquery = $vbulletin->db->query_read("
SELECT username, userid
FROM " . TABLE_PREFIX . "user
WHERE referrerid = '$userinfo[userid]'
AND usergroupid NOT IN (3, 4)
ORDER BY username
");
while ($referral = $vbulletin->db->fetch_array($referralsquery))
$referrals[$referral['userid']] = $referral['username'];
.................................................. ........
$templater->register('referrals', $referrals);
<vb:each from="referrals" key="userid" value="username">
Userid is {vb:raw userid} and Username is {vb:raw username}
</vb:each>
Boofo
01-10-2012, 02:39 AM
How would you pull the usergroup markup out of that?
ForumsMods
01-10-2012, 03:00 AM
$referralsquery = $vbulletin->db->query_read("
SELECT username, userid, displaygroupid, usergroupid
FROM " . TABLE_PREFIX . "user
WHERE referrerid = '$userinfo[userid]'
AND usergroupid NOT IN (3, 4)
ORDER BY username
");
while ($referral = $vbulletin->db->fetch_array($referralsquery))
$referrals[$referral['userid']] = fetch_musername($referral);
.................................................. ........
$templater->register('referrals', $referrals);
<vb:each from="referrals" key="userid" value="username">
Userid is {vb:raw userid} and Username is {vb:raw username}
</vb:each>
Boofo
01-10-2012, 03:07 AM
You don't need to set an array before the while statement?
ForumsMods
01-10-2012, 03:16 AM
Yes, you can declare it to clean/empty it if the variable was used before:
$referrals = array();Also you can unset/clean/empty/ free the query.
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.