sqlfield
tag. $value
.
You can also, if you like, reference the entire incoming row via $row
.
(This is fetched using mysql_fetch_array
, so
both numeric and associative keys are set; e.g. $row[0]
and $row['employeeID']
are both valid.)displayValue
attribute allows you to
alter what shows up on the template. For example, let's say you've got
a field in your employees table called "salary" which for some obscure
reason is stored as an integer. You want to format it to a nice dollar
amount. No problem! Here's what you do:<sqlfield name="salary" displayValue="'$' .
number_format($value / 100, 2)" />
SparseThisPage
or SparsePage
). Sparse will use eval()
to
output the computed value.<?php print $row['lastName'] . ', ' . $row['firstName']; ?>
You can use $row
anywhere inside the sqlrow tag.returnValue
to do that. Here's how it'd look:<sqlfield name="salary"
returnValue="(int)(substr($value, 1) * 100)" />
required
indicates the value can't be blank; numeric
ensures the data must be a number; min
and
max
require it to be greater or lesser than specific values. But you can
indicate any constraints you like. The easiest way to do this is to
create your own PHP function. The reason is because doing this will
allow you to "personalize" your error messages (see Errors).function employeeExists($name)
{
if (strlen($name) > 10) return 'Name is too
long!';
mysql_query('SELECT name FROM employees WHERE
name=' . $name);
if (mysql_num_rows) < 1) return 'Employee
not found!';
return true;
}
<sqlfield name="name" constraints="employeeExists($value)"
/>
constraints
tag evaluates to true,
the value will
be accepted. If it evaluates to false, a generic error message will be
displayed. If it evaluates to anything else, though, it will print out
that message. So it's a great way to define constraints without
interleaving the code with the display!
Previous: Sorting | Working With Data | Next: Errors