Go Back   vb.org Archive > vBulletin 4 Discussion > vB4 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 01-09-2012, 12:39 PM
Boofo's Avatar
Boofo Boofo is offline
 
Join Date: Mar 2002
Location: Des Moines, IA (USA)
Posts: 15,776
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default 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?
Reply With Quote
  #2  
Old 01-09-2012, 03:37 PM
nhawk nhawk is offline
 
Join Date: Jan 2011
Posts: 1,604
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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);
(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...
Code:
<vb:each from="referrals" key="referrerid " value="referral">
	Your template code using {vb:raw referral.whatevervalueneeded} 
</vb:each>
Reply With Quote
  #3  
Old 01-09-2012, 04:10 PM
Boofo's Avatar
Boofo Boofo is offline
 
Join Date: Mar 2002
Location: Des Moines, IA (USA)
Posts: 15,776
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
  #4  
Old 01-09-2012, 05:24 PM
nhawk nhawk is offline
 
Join Date: Jan 2011
Posts: 1,604
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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>
What that boils down to in PHP is a...
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());
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 [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.
Reply With Quote
  #5  
Old 01-09-2012, 06:01 PM
Boofo's Avatar
Boofo Boofo is offline
 
Join Date: Mar 2002
Location: Des Moines, IA (USA)
Posts: 15,776
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
  #6  
Old 01-09-2012, 06:07 PM
nhawk nhawk is offline
 
Join Date: Jan 2011
Posts: 1,604
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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());
Reply With Quote
  #7  
Old 01-09-2012, 06:08 PM
Boofo's Avatar
Boofo Boofo is offline
 
Join Date: Mar 2002
Location: Des Moines, IA (USA)
Posts: 15,776
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Maybe this has something to do with it?

Code:
$templater->register('referrals', $ref);
Reply With Quote
  #8  
Old 01-09-2012, 06:15 PM
nhawk nhawk is offline
 
Join Date: Jan 2011
Posts: 1,604
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Boofo View Post
Maybe this has something to do with it?

Code:
$templater->register('referrals', $ref);
If $ref is empty (query returned nothing), yes that could be the problem.
Reply With Quote
  #9  
Old 01-09-2012, 09:59 PM
Yellow Slider Yellow Slider is offline
 
Join Date: Aug 2006
Posts: 249
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by nhawk View Post
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);
(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...
Code:
<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.
Reply With Quote
  #10  
Old 01-10-2012, 02:12 AM
Boofo's Avatar
Boofo Boofo is offline
 
Join Date: Mar 2002
Location: Des Moines, IA (USA)
Posts: 15,776
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 09:56 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.04991 seconds
  • Memory Usage 2,265KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (11)bbcode_code
  • (2)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (10)post_thanks_box
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (10)post_thanks_postbit_info
  • (10)postbit
  • (10)postbit_onlinestatus
  • (10)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.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/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.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
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete