Sparse is a simple yet fairly
powerful way of making PHP pages that are driven by SQL. Sparse
allows you to create a simple HTML page (or even fragment) that
defines how your page
will look and interact with the data, and all the back-end stuff,
including displaying, editing,
adding, and updating data, navigation, searching, sorting, showing
error messages, etc.,
will all be
handled by Sparse. What's more, you can mix and match Sparse code with
regular form inputs and regular code, meaning that you only have to
code the really complex stuff yourself; everything else is done for you!
In addition, Sparse outputs every form in degradable
Ajax, cutting down on server-side computation and
bandwidth, and giving your user a smoother interface. If your user has
Javascript disabled, no problem: the regular functionality will take
over.
It's probably easiest to just show you an example. Here it is (assuming
you have a table "myTable" in the database "myDB" with four fields
named ID, Field1, Field2, and Field3):
<?php
include('Sparse.php');
SparseThisPage('myUsername', 'myPassword');
?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<body>
<sqltemplate
database="myDB" tables="myTable"
limit="10" type="display">
<table
border='1'>
<tr>
<th>ID</th>
<th>Field1</th>
<th>Field2</th>
<th>Field3</th>
</tr>
<sqlrow>
<tr>
<td><sqlfield
name="ID"
/></td>
<td><sqlfield
name="Field1"
/></td>
<td><sqlfield
name="Field2"
/></td>
<td><sqlfield
name="Field3"
/></td>
</tr>
</sqlrow>
</table>
</sqltemplate>
</body>
</html>
The first thing you should
notice is the
PHP code at the top. Those two lines are all you need to get Sparse
going! (Although you can replace the username and password with strings
or other PHP variables.) The rest of the page, complete with HTML and
style info, and
even other PHP code, can remain as is.
Ladies and gents, please draw your attention to the first unique
tag,
sqltemplate
.
This is the main
tag enclosing your template. Note that
you can have multiple
sqltemplates
in
a single document if you so
choose. This tag allows you to designate what kind of template it is
(in
this case, it'll simply display the data, as evidence by its
display
type) and what the associated data should be (in this case, we just
want the first 10 rows in the "myTable" table).
After that comes the
sqlrow
tag.
Everything within this tag will
be repeated for each row of the data returned from the query computed
from the
sqltemplate
tag.
Finally comes the biggie, the
sqlfield
tag.
This indicates where the
data should go. If it's an editable table, it will print an appropriate
widget (text box, textarea, radio button etc.). Otherwise it'll
simply print the data. The "name" attribute indicates what column of
the query should go there.
And voila: your table looks something like this:
ID |
Field1 |
Field2 |
Field3 |
1 |
dog |
15.32 |
2005-06-07 |
100 |
cat |
1234.54 |
2005-12-21 |
753 |
camel |
-24.6 |
2005-01-01 |
Note that although our example using SparseThisPage is probably the
easiest and most common
usage, you don't need to solely use it this way. You can save different
templates in XML files, with HTML fragments rather than full pages. You
can
display
the templates on the fly by using the
PHP
functions.
Although Sparse is simple to use, it's also quite powerful in that it
allows you a lot of control over the look and behavior of your form without a lot of work.
You can change the style of any element; You can define where the error
messages should be and what they should
look like; you can force fields to become particular widgets; you can
easily do input validation or change around the values before updating
them. Sparse is both usable and flexible.
You can even use a super-compressed version of the template, like so:
<sqlquick database="myDB" type="display"
tables="myTable" fields="ID, Field1, Field2, Field3" limit="10" />
Now that you've got a taste of it, why not dive deeper into the
features?
Introduction | Next:
Templates - Overview