View Full Version : [tip] Use PAPI if you're just starting out with phpinclude

19 Dec 2002, 22:13
Note: this is not meant to be a plug for my mod.

PAPI (pronounced "pappy") is a mod I released: http://www.vbulletintemplates.com/mods/showthread.php?s=&threadid=2361 . Escentially it is a phpinclude swiss army knife although the name means phpinclude API. It is a set of wrapper functions designed to drastically simplify coding mods for use in the phpinclude template.

Going back a step: the phpinclude template is a template that is special in the sense that it can contain raw PHP code. It is the only template that can; the others can only contain PHP variables.

Standard vB pages are constructed in this order:
1. Execute the .php file
2. Execute the contents of phpinclude
3. Compile the final HTML to display

Therefore phpinclude can only access variables that are within a global scope and only gets executed when a template is generated.

Remember though that phpinclude is executed in any page, so you have to start accounting for that. Also you have to use vB's database reference methods to access the MySQL database. This is where PAPI starts to become useful.

Say you want to run code that only executes on forumhome (index.php). Normally you'd put this in phpinclude:

if (strpos($_SERVER['PHP_SELF'], "index.php") != false)

To those new to PHP, that tests whether the current page's path and filename contains the string "index.php".

With PAPI, this is simplified to:

if (thisis("index"))


if (thisis("index.php"))

PAPI offers a plethora of functions, but some general ones to note: actionis() returns true depending on the value of the "action" variable (i.e., index.php?action=something; actionis("something") would return true. mysqlqueryhandle (1.0.3 and later) returns the result from a MySQL query (i.e., returns $DB_site->query(query).

These may seem trivial, but it goes so far to implement mysqlmultiplequeries which can execute several queries in succession with one line of code. So

$DB_site->query("DELETE FROM session");
$DB_site->query("DELETE FROM word");
$DB_site->query("INSERT INTO word (wordid) VALUES (1)");

can be simplified to:

mysqlmultiplequeries("DELETE FROM session; DELETE FROM word; INSERT INTO word (wordid) VALUES (1)");

Also, starting with PAPI 1.0.3, an internal query counter is maintained, so every time a query is executed via PAPI's wrapper functions a counter is incremented; putting $papiquerycount in your footer will display how many queries PAPI's wrapper functions have executed.

There are far more features than this, however, so check out PAPI's thread for a near-full list, or at least the features from 1.0.0. Enjoy.

19 Dec 2002, 22:48
thanks again for making this! due to my lack of time...i havent tried it yet - sorry:)

Tony G
20 Dec 2002, 00:23
I've taken your advice, although I wanted to learn the hard way so PAPI would be easier. :p

Stay tuned for somewhat of a PAPI mod by me. :)

20 Dec 2002, 00:29
Just be sure to use PAPI's wrappers for the MySQL stuff wherever possible because the soon-to-be-uploaded 1.0.3 includes an internal counter.

Tony G
20 Dec 2002, 00:35
I'll wait for that version then. :)