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

Sparse - Template Types

Display Templates

The simplest type of template is one that just displays information. You don't have to worry about inserting, editing, or deleting data. All sqlfields are automatically forced to only display the data. You can still use navigational buttons to go back and forth between data.

<?php
  include('Sparse.php');
  SparseThisPage('username', 'password');
?>
<html>
  <sqltemplate type="display" tables="employees" limit="10" database="myDB">
    <table border="1">
    <sqlrow>
      <tr>
        <td><sqlfield name="employeeID" /></td>
        <td><sqlfield name="employeeName" /></td>
      </tr>
    </sqlrow>
    <tr><td colspan="2" align="center">
      <sqlaction type="prev" /><sqlaction type="next" />
    </td></tr>
    </table>
  </sqltemplate>
</html>

The results:
15 Jack Brown
17 Jane Smith
18 Terry Pratchett
23 Malcolm X
27 George Bush
100 Joan of Arc
104 Feivel Mousekowitz
150 Guy Fawkes
200 Baron Saturday
4567654 Bill Gates
 

Add Templates

This template allows the user to enter new data into a table; no data at all is displayed (other than default values).

<?php
  include('Sparse.php');
  SparseThisPage('username', 'password');
?>
<html>
  <sqltemplate type="add" table="employees" rowsToAdd="2" database="myDB">
    <table border="1">
    <sqlrow>
      <tr>
        <td><sqlfield name="employeeID" /></td>
        <td><sqlfield name="employeeName" type="textarea" rows="1" cols="10"/></td>
      </tr>
    </sqlrow>
    <tr><td colspan="2" align="center">
      <sqlaction type="add" />
    </td></tr>
    </table>
  </sqltemplate>
</html>

As you can see, the template above is quite similar to the one below. But instead of simply printing text, this one displays widgets:


Sparse can display all kinds of widgets. By default it will grab the SQL type of the result and generate an appropriate widget - text field, textarea, or select box. However, you can also force any field to print out a different sort of widget - in this case, I've made the "name" field be a textarea with 1 row and 10 columns.  (Any attributes that would normally go on the widget HTML tag can be tacked onto the sqlfield tag.) You can also make regular text fields use checkboxes, radio boxes, or select boxes by utilizing enumsets. You can even turn the field into plain old text (although that's pretty useless for add or edit templates).

Note that there are two rows added there; if the second one is left with default values, it will be ignored. The number of rows can be changed, as well as the default values of the widgets.

Remember that you have to put every field in the row in order for the addition to be successful. You can use hidden fields to fill in constant values.

Edit Templates

The most complex of the template types, this combines displaying data with entering data. Like add templates, widgets will be displayed, but instead of blank/default values, they'll be populated by the data from your query. You can update or delete any row that's displayed.

<?php
  include('Sparse.php');
  SparseThisPage('username', 'password');
?>
<html>
  <sqltemplate type="edit" table="employees" database="myDB" limit="10">
    <table border="1">
    <sqlrow>
      <tr>
        <td><sqlfield name="employeeID" /></td>
        <td><sqlfield name="employeeName" type="textarea" rows="1" cols="10"/></td>
        <td><sqlaction type="edit"/> <sqlaction type="delete" /></td>
      </tr>
    </sqlrow>
    <tr><td colspan="3">
       <sqlaction type="prev"/><sqlaction type="next"/>
     </td></tr>
    </table>
  </sqltemplate>
</html>

The result of this is as follows:


Now... the truth is that there are really two edit templates. The first is the one shown above - each row is edited separately. However, you can also use edit templates to edit the entire set of rows at once. To do this, simply place the tag
<sqlaction type="edit" />
outside the sqlrow tag. Doing this will ignore any sqlaction tags that are inside the sqlrow. You will also not be able to delete any rows using this method. Unfortunately, there is no way to allow both methods to operate at once without using Javascript, which we'd rather not do for the moment in an effort to make Sparse as universal as possible.

Previous: Templates and Queries | Template Types | Next: Advanced Queries