SourceForge.net Logo
Home | SourceForge | Documentation | FAQ | What's New | To-Do List | Forums | Download | Contact

Sparse - PHP Reference

Most of Sparse's functionality takes place internally, so you don't have to worry about most of it. But you can use Sparse in a variety of ways. The useful functions are as follows:

SparseThisPage([$username, [$password, [$host]]])
Executes Sparse on the current page. The PHP fragment that contains the SparseThisPage function call will be ignored; all other HTML and PHP code will be parsed. Note that Sparse can use the "global" MySQL connection, so if you already have one up, you can skip the arguments. If the "host" argument is skipped, "localhost" will be used.
This function call must be done before any output is printed - in other words, it must be enclosed in a PHP tag at the very top of the document, before the <HTML> tag. It will call exit() when it finishes, so make sure to do any further processing before you call it. You can do more processing in a separate PHP tag if you wish, though; only the current PHP tag will be ignored after SparseThisPage is called.

SparseThisPage($connection)
Same as the above function; however, if there is only one argument, it will be taken as a MySQL connection resource.

SparsePage($page, [$username, [$password, [$host]]])
This allows you to execute Sparse on an arbitrary file. The first argument is the file path (relative or absolute), and the other three arguments are treated as in SparseThisPage.

SparsePage($page, $connection)
This allows you to execute Sparse on an arbitrary file. The first argument is the file path (relative or absolute), the second is a MySQL connection resource.

Using PHP In Templates

You can use PHP code anywhere in an SQL template - it will be stored along with the cached code and executed each time the page is called. You can include any kind of code you want, including leaving off a tag with an open { bracket to conditionally print out HTML code. In fact, you can even use it to conditionally parse Sparse tags like sqlfield! For example, let's say you only want to include a particular field if the user viewing it is logged in:

<!-- first part of template goes here,
  including sqltemplate and sqlrow tags -->
<?php if (isset($_COOKIE['username'])) { ?>
  <td><sqlfield name="password" /></td>
<?php } ?>

Remember that your code will be executed inside a function (inside an object), so if you want to access a previously defined variable, make sure that it's a global variable and access it by $_GLOBALS. In addition, you can use the following variables (please READ ONLY, do not write to them!)

The First Tag - SparseThisPage()

If you're using SparseThisPage() as your function call (allowing you to put all your Sparse and PHP code in a single page), the first PHP tag (from <?php to ?>) on the page is unique. The following things are true about it:

Previous: HTML Tag Reference | PHP Reference | Next: Example Template