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

Sparse - Example Template

Here's a start-to-finish example of a simple template. Let's say you have the following table in the database "myDB":

create table employees(id integer(11) not null, name varchar(255), gender enum('m','f'), salary real, notes text, primary key(id));

Let's say you want to allow an admin to edit or delete employees, ordered by name. You want to display five employees at a time, to disallow users from editing the ID, and to use radio buttons saying "Male" or "Female" to select the gender. Here's what your full template file (saved as .php or .phtml) might look like:

<?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>

  <enumset id="gender">
    <enum key="m" value="Male" />
    <enum key="f" value="Female" />
  </enumset>

  <sqltemplate database="myDB" tables="employees" limit="5" type="edit" constraints="ORDER BY name">
    <table border='1'>
    <tr>
      <th>ID</th>
      <th>Name</th>
      <th>Gender</th>
      <th>Salary</th>
      <th>Notes</th>
      <th>Action</th>
    </tr>
    <sqlrow>
      <tr>
        <td><sqlfield name="ID" type="text"/></td>
        <td><sqlfield name="name"/></td>
        <td><sqlfield name="gender" enumset="gender"/></td>
        <td>$<sqlfield name="salary" /></td>
        <td><sqlfield name="notes" /></td>
        <td><sqlaction type="edit" /><sqlaction type="delete" /></td>
      </tr>
    </sqlrow>
    <tr><td colspan="6" align="center"><sqlaction type="previous" /><sqlaction type="next" /></td></tr>
    </table>
  </sqltemplate>
</body>
</html>


Here's how your page will look:

ID Name Gender Salary Notes Action
15 $
12 $
31 $
23 $
7 $

However, if you're willing to forego the nice Male/Female select box and the non-editablity of the ID field, you can do the entire thing in one tag:

<?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>
<sqlquick database="myDB" tables="employees" limit="5" type="edit" constraints="ORDER BY name" />
</body>
</html>

Previous: HTML Tag Reference | Example Template