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 09-30-2012, 03:18 PM
Kiint Kiint is offline
 
Join Date: Nov 2006
Posts: 191
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default displaying results from two tables as seperate blocks

This is probably basic coding for most of you but I've been trying to find out how to do this.

I have 2 tables of data,

the first table "ranks" has 3 columns, Rank_id, Rank_desc, Rank_order
the second table "roster" has many columns but I only want to display a few of them.
roster_id, member_name, roster_rank.

it's a one to many relationship as each rank can have many members. so ranks.Rank_id and roster.roster_rank are the linking primary keys

I need a query which will select each rank and all the members that have that rank and display each rank and members in a seperate block in the template, so that the results on the page would show like this:

Rank officers
bob
harry
john

Rank corporals
mike
sally
peter

Rank Private
Tom
Rich
Harry

Could someone please let me know how to do this? I've spent several hours looking up code and can't find the solution to this.
Reply With Quote
  #2  
Old 09-30-2012, 04:17 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

You could do something like this:

Code:
$result = $vbulletin->db->query_read_slave("SELECT ranks.*, roster.roster_id, roster.member_name, roster.roster_rank
                                               FROM roster LEFT JOIN ranks ON(ranks.Rank_id = roster.roster_rank)
                                               ORDER BY Rank_order ASC, member_name ASC");

$roster = array();
while ($row = $vbulletin->db->fetch_array($result))
{
    $roster[$row['Rank_id']][] = $row;
}

Then you'd register $roster to your template (or use preRegister if it's an existing template), and in the template you can use <vb:each> to format the blocks, like:

Code:
<vb:each from="roster" value="members">
{vb:var members.0.Rank_desc}<br />
    <vb:each from="members" value="member">
{vb:var member.name}<br />
    </vb:each>
<br />
</vb:each>
Reply With Quote
  #3  
Old 09-30-2012, 08:46 PM
Kiint Kiint is offline
 
Join Date: Nov 2006
Posts: 191
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Many many thanks.

I manged to adapt your code to fit my existing template and now have a dynamic list of ranks instead of a manual hard coded set of ranks which I manually had to add code whenever I added or deleted a rank.
I did have one problem and that was when I created the array I used 'Rank_id' instead of 'rank_id' ...took me quite a while to trace that

The end result is here:
http://www.rpgguild.co.uk/vbforum/roster.php

--------

One extra question, On the old hard coded version, whenever I had members which were ranked as "inactive" (rank_id 9) I used to have this condition above the block of code so that only admins could see it:
Code:
<vb:if condition="is_member_of($vbulletin->userinfo,5,6,11,12)">
<h1 class="blockhead stat_home">Inactive - Only admins can see this</h1>
and this would hide that rank from being displayed.

Is there a way that I can add this into the <vb:each> so that when it detects an admin it is displayed and not displayed when a registered/non-registered members views the page?

Thanks again for your help, this has helped so much.
Reply With Quote
  #4  
Old 09-30-2012, 10:53 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Kiint View Post
I did have one problem and that was when I created the array I used 'Rank_id' instead of 'rank_id' ...took me quite a while to trace that
I thought the column name was 'Rank_id'. Oh well, I'm glad you were able to get it working because I didn't actually try any of that code.



Quote:
One extra question, On the old hard coded version, whenever I had members which were ranked as "inactive" (rank_id 9) I used to have this condition above the block of code so that only admins could see it:
Code:
<vb:if condition="is_member_of($vbulletin->userinfo,5,6,11,12)">
<h1 class="blockhead stat_home">Inactive - Only admins can see this</h1>
and this would hide that rank from being displayed.

Is there a way that I can add this into the <vb:each> so that when it detects an admin it is displayed and not displayed when a registered/non-registered members views the page?

Thanks again for your help, this has helped so much.

I guess it would be something like:

Code:
<vb:each from="roster" value="members">
<vb:if condition="$members[0].Rank_id != 9 OR is_member_of($vbulletin->userinfo,5,6,11,12)">
<vb:if condition="$members[0].Rank_id == 9>
<h1 class="blockhead stat_home">Inactive - Only admins can see this</h1>
<vb:else />
{vb:var members.0.Rank_desc}<br />
</vb:if>
    <vb:each from="members" value="member">
{vb:var member.name}<br />
    </vb:each>
<br />
</vb:if>
</vb:each>
Reply With Quote
  #5  
Old 10-01-2012, 08:18 AM
Kiint Kiint is offline
 
Join Date: Nov 2006
Posts: 191
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Thanks again for this.

I had to slightly modify the IF conditional to this:

<vb:if condition="$members[0]['rank_id'] != 9">

as for some reason it would not assign the value to that variable.

However I couldn't get the 2 IF's to work together, I basically had to do the following to make it work...not great I know.....

Code:
<vb:each from="roster" value="members">
<vb:if condition="$members[0]['rank_id'] != 9">

  (code here)

</vb:if>

<vb:if condition="$members[0]['rank_id'] == 9 AND is_member_of($vbulletin->userinfo,5,6,11,12)">

   (same code here)

</vb:if>
</vb:each>
This worked, but it doesn't look nice

Thanks again, you've saved me a LOT of work.
Reply With Quote
  #6  
Old 10-01-2012, 08:27 AM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Kiint View Post
Thanks again for this.

I had to slightly modify the IF conditional to this:

<vb:if condition="$members[0]['rank_id'] != 9">

as for some reason it would not assign the value to that variable.
Sorry, that was my fault - you couldn't get it to work because what I posted was wrong.

Anyway, what you did looks OK to me. Of course there are lots of ways to achieve the same thing, and since I didn't test what I posted it could have been wrong.
Reply With Quote
  #7  
Old 10-01-2012, 08:42 AM
Kiint Kiint is offline
 
Join Date: Nov 2006
Posts: 191
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by kh99 View Post
Sorry, that was my fault - you couldn't get it to work because what I posted was wrong.

Anyway, what you did looks OK to me. Of course there are lots of ways to achieve the same thing, and since I didn't test what I posted it could have been wrong.
No worries, it all worked out in the end.

Now the work begins, It's now working the way I want, but now to make it look better

Thanks for all the help you've given me. Very much appreciated.
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 02:01 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.04252 seconds
  • Memory Usage 2,227KB
  • Queries Executed 13 (?)
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
  • (6)bbcode_code
  • (4)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (7)post_thanks_box
  • (7)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (7)post_thanks_postbit_info
  • (7)postbit
  • (7)postbit_onlinestatus
  • (7)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
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete