The datasheet model is used to define small static database tables that can be queried without the need to use an actual database. Some websites have a need to define small sets of tabular data that rarely gets updated. In such cases, the effort and overhead of defining actual database tables might not be justified. Although the datasheet model theoretically allows any number of rows and columns you will typically not use this approach if the number of required rows exceeds say 200 and the number of columns is more than about 25.

A typical use case for deploying the datasheet model might be to implement a small online store with a handful of products. Rather than hard coding product descriptions and prices within your code, a datasheet can be used to externalise this information.

If you generally load all of the data in your use cases, you should consider using the php_array_data model instead.

Sample Datasheet

Here we see a datasheet used to show information about products that can be ordered.

Simple datasheet

Notice that the first row is used to store the name of the columns in the datasheet. Also notice that some of the columns in the datasheet contain character data, while other columns contain numeric data. 

Column Definitions

The second grid in the specification is used to specify the format of each column according to the type of data that can be contained in the column.

Column definition grid

Accessing the Datasheet Columns

Datasheet columns can be queried using a simple api. The class used to access these simple tables is called load_simple_table and it is defined within the system (framework) site. Consider the following set of weights and prices for products of type roll-tarp as defined in our datasheet above.

Product type roll-tarp grid

Let's see how are products datasheet can be queried to build this HTML table.

require_once CLASS_PATH.'load_simple_table.php';
$type = $this->site->parameters->get_require_parameter('type'); // Get product type from the query string
$this->products = new \system\load_simple_table('products'); // Load the products datasheet
$this->products->select_multi('type',$type,'=='); // Select rows equal to requested type
if (empty($this->products->record)) {
	return false; // Error - invalid type
}
// Use the rows to build the html table
$table = '<table class="order_items">';
$table .= '<tr><th><b>Description</b></th><th><b>Weight (lb)</b></th><th><b>Price</b></th><th><b>Select</b></th></tr>';
foreach ($this->products->record as $keyindex => $values){
    $size = $values[$this->products->fieldname['size']];
    $product_code = $values[$this->products->fieldname['index']];
    $weight = number_format($values[$this->products->fieldname['weight']], 1, '.', ',');
    $price = '$'.number_format($values[$this->products->fieldname['price']], 2, '.', ',');
    $radio = '<input type="radio" name="product" value="'.$product_code.'" />';
    $table .= '<tr><td>'.$size.'</td><td>'.$weight.'</td><td style="text-align:right">'.
      		$price.'</td><td style="text-align:center">'.$radio.'</td></tr>';
}
$table .= '</table>';
return $table;
In the example code above we use the select_multi method to select multiple rows matching the supplied key value. In addition to the == operator, this method can be passed <, <=, >, >=, !=, and contains.
Other methods for searching rows include:
select_all()

To select all rows

select_all_by_unique_key($key_name)

Return all rows ordered by supplied unique key

select_all_by_nonunique_key($key_name)

Return all rows ordered by supplied non-unique key

select_single($key_name, $value)

Return a single row that matches the supplied column name and value

select_first()Return the first row of the table
histogram($key_name)

Return all distinct values in the supplied column

get_column_names()Return the names of the datasheet columns

Refer to the load_simple_table class located in system to learn more about fetching data rows from a datasheet.
Sample datasheet spec
🡇
Sample code used to access a datasheet