[REQ] Need This vBulletin Article

SpeedRazors

New Member
I'm going to try and explain the datastore system used in vBulltin. This tutorial is based around users who are already familar with PHP and MySQL.

What is Cache?
A memory area where frequently accessed data can be stored for rapid access. (View Definitions). Basicly, you can store information into the database, and grab all this information with just using 1 query. Some may question this and say: "Isn't all information in the database retrived by queires?". Yup, all information in the database is retrived using MySQL queries, though the method used is diffrent when using the datastore system in vBulletin.

I'll show you way using the datastore is a better solution for you and the users who will use your hacks.

Datastore Method
Lets say you want to use a drop down form with 3 options to choose from. Obviously you would need to create alittle script to add, delete, and edit the information; and when you make this script. Where does the info go? Well it would go into the database. Lets call the MySQL table "dropdownoptions" with 3 fields. ID, Name, and Value.

Click image for larger version Name: data1.gif Views: 111 Size: 1.1 KB ID: 44423

Now everytime you add, edit, or delete your information, it will be changed in the database. Here is an example of the database tree. We have 3 rows of information.

* ID = (1) Name = (Dog) Value(Dog)
* ID = (2) Name = (Cat) Value(Cat)
* ID = (3) Name = (Cow) Value(Cow)

Click image for larger version Name: data2.gif Views: 75 Size: 2.3 KB ID: 44424

After you insert this information into the database, the next step would be to insert this info in the datastore. You can do that like this.

PHP Code:
Code:
$query = $vbulletin->db->query_read("
        SELECT *
        FROM " . TABLE_PREFIX . "dropdownmenu
        ORDER BY id DESC
");

while ($variable = $vbulletin->db->fetch_array($query))
{
        $variable_array[] = $variable;
}

build_datastore('dropmenu', serialize($variable_array));
Let me go ahead and tell you exacly what the above does. What we did was just grab all in the information in the database from the table dropdownmenu. The while() function will loop the information from the begging to the end. While it's getting all this information, It's storing it all into the variable named $variable_array.

Then you need to use the function called build_datastore(). This function requires the following. build_datastore(1, 2). #1 is the name of your datastore item, and number two is the serialize information to store. This function will create a new datastore item with the name and all your info.

The serialize info will look like this...
Code:
Code:
a:3:{i:0;a:3:{s:2:"id";s:1:"1";s:4:"name";s:3:"Dog";s:5:"value";s:3:"Dog";}i:1;a:3:{s:2:"id";s:1:"2";s:4:"name";s:3:"Cat";s:5:"value";s:3:"Cat";}i:2;a:3:{s:2:"id";s:1:"3";s:4:"name";s:3:"Cow";s:5:"value";s:3:"Cow";}}
So now we have all our infomation stored in another area of the database, in one row. Heres a picture so you can compair it to the other ones.

Click image for larger version Name: data3.gif Views: 129 Size: 1.5 KB ID: 44425

Don't let the serialize data freak you out. You don't really NEED to read that data. Though I kinda like doing it, so I'll break it down alittle for you. Here is a tree of the serialize data.

Code:
Code:
a:3:{
        i:0; a:3:{
                s:2:"id";s:1:"1";
                s:4:"name";s:3:"Dog";
                s:5:"value";s:3:"Dog";
        }

        i:1; a:3:{
                s:2:"id";s:1:"2";
                s:4:"name";s:3:"Cat";
                s:5:"value";s:3:"Cat";
        }

        i:2; a:3:{
                s:2:"id";s:1:"3";
                s:4:"name";s:3:"Cow";
                s:5:"value";s:3:"Cow";
        }
}

In PHP it would look like this.
PHP Code:
Code:
$data = array(
        '0' => array(
                'id' => '1',
                'name' => 'Dog',
                'value' => 'Dog'
        ),
        '1' => array(
                'id' => '2',
                'name' => 'Cat',
                'value' => 'Cat'
        ),
        '2' => array(
                'id' => '3',
                'name' => 'Cow',
                'value' => 'Cow'
        ),
);

So now we have the info stored, lets get it back out in a readable formate. This is the easy part. This is the code I use.

PHP Code:
Code:
$vbulletin->dropmenu = unserialize($vbulletin->dropmenu);
foreach ($vbulletin->dropmenu AS $dropmenu)
{
        echo "(ID: "  . $dropmenu['id'] . ") (Title: " . $dropmenu['title'] . ") (Value: " . $dropmenu['title'] . ")<br />";
}

This will print the following:
(ID: 1) (Title: Dog) (Value: Dog)
(ID: 2) (Title: Cat) (Value: Cat)
(ID: 3) (Title: Cow) (Value: Cow)

What your doing is simple. The variable $vbulletin->dropmenu holds the information though it's still serialize. The reason why $vbulletin->dropmenu is the variable is because you need to tell vBulletin which datastore row to get. In this case it is dropmenu because remember we stored it in there like this: build_datastore('dropmenu', serialize($variable_array));

Now in order for vBulletin to know about that specific row, you need to add it to the $specialtemplates array. Example:

PHP Code:
Code:
$specialtemplates = array(
        'dropmenu'
);

So now that you understand where the variable is coming from, we need to unserialize the data, cause it looks all ugly and weird.

So $vbulletin->dropmenu = unserialize($vbulletin->dropmenu); baiscly gets the serialize info and unserialize's it using the function called unserialize() Once that is finished you want to loop the info using a foreach() function. foreach ($vbulletin->dropmenu AS $dropmenu). Doing that is storing the array info into $dropmenu and you can get each one by using the following vaiables. $dropmenu['id'], $dropmenu['title'], and $dropmenu['value'].

The reason why this is better is because vBulletin allready runs one global query to get all the datatore information. So instead of running a query everytime you want to get the drop down information, you just get it from the datastore and save that query. Some may think this is kinda extreme when you can just run a query, though as your site grows; you want to save as many queires as possiable.
 
Top