The Arcive of Official vBulletin Modifications Site.It is not a VB3 engine, just a parsed copy! |
|
#1
|
||||
|
||||
vb:each question
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:
Code:
$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? |
#2
|
|||
|
|||
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... Code:
$templater->register('referrals', $referrals); Then parse it in the template... Code:
<vb:each from="referrals" key="referrerid " value="referral"> Your template code using {vb:raw referral.whatevervalueneeded} </vb:each> |
#3
|
||||
|
||||
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.
|
#4
|
|||
|
|||
OK, try this..
Create a template called 'referrals' and put this code in it.. Code:
<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> Code:
foreach($referrals as $referral) Then create a plugin using the 'forumhome_complete' hook with this code (don't do this on a live site).. Code:
$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()); 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 [DATE]1326135002[/DATE] at [TIME]1326135002[/TIME] --------------- 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. |
#5
|
||||
|
||||
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. |
#6
|
|||
|
|||
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. Code:
$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()); |
#7
|
||||
|
||||
Maybe this has something to do with it?
Code:
$templater->register('referrals', $ref); |
#8
|
|||
|
|||
If $ref is empty (query returned nothing), yes that could be the problem.
|
#9
|
|||
|
|||
Quote:
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. |
#10
|
||||
|
||||
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.
|
|
|
X vBulletin 3.8.12 by vBS Debug Information | |
---|---|
|
|
More Information | |
Template Usage:
Phrase Groups Available:
|
Included Files:
Hooks Called:
|