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
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):
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
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
This is the main
tag enclosing your template. Note that
you can have multiple
a single document if you so
choose. This tag allows you to designate what kind of template it is
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
Everything within this tag will
be repeated for each row of the data returned from the query computed
Finally comes the biggie, the
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:
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
the templates on the fly by using the PHP
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
Introduction | Next:
Templates - Overview