Go Back   vb.org Archive > vBulletin Article Depository > Read An Article > vBulletin 3 Articles
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools
Transdimensional Bitfields - Generate Bit Sets within a Bit Set
Attilitus's Avatar
Attilitus
Join Date: Mar 2005
Posts: 393

 

Show Printable Version Email this Page Subscription
Attilitus Attilitus is offline 08-07-2007, 10:00 PM

This is a fairly simple concept, but it really allows you to do alot more dynamic template work while staying firmly rooted within vBulletin's template system.

First lets talk about the basic use of Bitfields. Lets imagine that you have a list of 20 items. These items have the following 3 identifiable features:

Item Name
|| Item Type || Item Owner


Now lets say that you have 20 of these items stored in your database each with a value to go under one of the three aforementioned fields. Surely you would not want to manually enter in all 20 items directly into the database. Instead you would need to use bitfields:

PHP Code:
$items=$vbulletin->db->query_write("SELECT FROM " TABLE_PREFIX "Items name,type,owner");

while(
$item $vbulletin->db->fetch_array($items)){

//$item['name'] $item['type'] and $item['owner'] exist here now.

eval('$itembits .= "' fetch_template('itembits') . '";');

The above script queries the database to retrieve all results within table Items and then goes through each row one by one and evaluates the individual row as an array within the template.

In the template itembits we have the following code:
Code:
<div>$item[name] || $item[type] || $item[owner]</div>
And we can display all of these bits that were processed in the php script (all 20 of them displayed in the format setup in the template itembits) by adding the following variable into our templates: $itembits.

Now all this is fine and dandy and pretty basic.

But you run into a small problem if there is the potential to have an infinite number of owners stored in an array each with its own specific details. Lets imagine that we want to display all the owners of the item and each of those owner's personal details in the same list. However, we do not know how many owners there are.

Lets pretend that $item['owner'] is stored in the database as a serialized multi-dimensional array and contains the identities and personal details of 200 owners (10 for each of the 20 $items).

The array is structured like this:
PHP Code:
$owner=$item['owner']; 
$owner[0]['name']="Owner's Name" ;
$owner[0]['address']="Owner's Address" ;
$owner[0]['birthdate']"Owner's Birthdate"
There is an $owner[0] through $owner[19] for every item (20 in all). But remember we do not actually know how many there will be as new owners can be added at any time.

If we were to try to edit the template "itembits" to contain all the information of the owners we would have quite a job on our hands, and would be writing messy templates. Perhaps the best solution is to simply avoid this kind of issue all together but for those of you who want a way around it here is the solution:




PHP Code:
$items=$vbulletin->db->query_write("SELECT FROM " TABLE_PREFIX "Items name,type,owner");

//This variable will tell us which $itembit we are on.
$i=0;
//
    
while ($itembit $vbulletin->db->fetch_array($items))
{    
        
$itembit['owner']=unserialize($itembit['owner']);
        
$itembit['owner']=unserialize($itembit['owner']);

//this variable tells us which iteration of the $owner array we are on.
$m=0;
//
    
foreach($itembit['owner'] as $value){            
        
$itembit['owner'][$m]=$value;

/*here is the key, we make the bitfield variable for Owner an array with an autoincrementing pointer value so that it will output a different listing of owners depending on which item is being processed.*/
        
eval('$itemownerbits[$i] .= "' fetch_template('itemownerbits') . '";');

//autoincrement the value $m so we process all owners for this item.            
        
$m++;
        }        
//The old bit of code. The values of $item['owner'] $item['name'] and $item['type'] are still available here.
        
eval('$itembits .= "' fetch_template('itembits') . '";');

//Auto incrementing this value moves us to the next item, $m (the array pointer for owner) is then reset to 0 and we cycle through it which a new set of Owners for the new item.
        
$i++;

    } 
Is this example rather ridiculous? Is the solution rather simple? Is this solution (especially in this example) an inefficient solution to the problem?

The answer is probably "Yes" for all of the above.

However, I have come across certain specific situations that have required this kind of implementation, and I figured I would share a solution that has aided me a couple of times.

Next Article: Transdimensional Bitfields Within Transdimensional Bitfields
(Using a new object instance for each bitfield call... and two database queries...!!!!!!!111!1!!eleven)
Reply With Quote
  #2  
Old 08-09-2007, 01:32 PM
Princeton's Avatar
Princeton Princeton is offline
 
Join Date: Nov 2001
Location: Vineland, NJ
Posts: 6,693
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

thank you for sharing :up:
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 06:22 AM.


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.03712 seconds
  • Memory Usage 2,217KB
  • Queries Executed 15 (?)
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)bbcode_code
  • (3)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)modsystem_article
  • (1)navbar
  • (4)navbar_link
  • (120)option
  • (2)post_thanks_box
  • (2)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (2)post_thanks_postbit_info
  • (1)postbit
  • (2)postbit_onlinestatus
  • (2)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