New Sado Version 1.1 released! Easily add ORM to your applications using our Sado Library

Sado Grid Object Documenation

This Sado Grid object is used to create HTML tables that display database table data.

Using the Sado Grid object is very simple. First, begin by creating a simple Sado Grid class:// include bootstrap
require_once './lib/Sado/sado.bootstrap.php';

// create ORM class
class User extends SadoORM
{
      // nothing here
}

// create Grid class
class UserGrid extends SadoGrid
{
      // initialize the grid
      public function __construct()
      {
            // add instance of ORM class
            $this->model(new User);

            // add a border to the grid
            $this->border = 1;

            // add fields
            $this->field('fullname')
                  ->title('Name:');

            $this->field('email')
                  ->title('Email:');

            $this->field('is_active')
                  ->title('Active');
      }
}

// create instance of grid
$grid = new UserGrid;

// display grid
echo $grid->getGrid();
You can also use custom query to customize the records used in the grid (by default the grid will fetch all records. An example of this, only selecting active users: [...]
      // place this method in UserGrid class
      protected function getQuery()
      {
            // the getName method is dynamically setting the model/table name to 'users'
            return 'SELECT * FROM ' . $this->getSadoOrm()->getName() . ' WHERE is_active = 1; ';
      }
[...]
Adding pagination to a grid is very easy, here is an example: [...]
class UserGrid extends SadoGrid
{
      public function __construct()
      {
            $this->model(new User);
           
            $this->border = 1;
           
            // add pagination, 'pg' is the GET var used for page number in querystring
            // 2 is the number of records allowed per page
            $this->pagination('pg', 2);
           
            $this->showAllFields();
      }
     
      // this will be called after the records are fetched
      protected function onRecords()
      {
            // check if there is a previous page
            if($this->getSadoPagination()->getPrevPage() > 0)
            {
                  // display link for previous page
                  $this->html_foot = '<a href="?' . $this->getSadoPagination()->getPrevPageString()
                        . '">Prev Page</a> ';
            }
           
            // check if there is a next page
            if($this->getSadoPagination()->getNextPage() > 0)
            {
                  // display link for next page
                  $this->html_foot .= '<a href="?' . $this->getSadoPagination()->getNextPageString()
                        . '">Next Page</a>';
            }
      }
}
[...]
Another important part of the grid object is filters. Filters can be used to modify (or filter) data in the grid cells. Say, for example, you want to add a link to each user, with the user ID, where the user name is displayed. This can be done using a filter: class UserGrid extends SadoGrid
{
      // initialize the grid
      public function __construct()
      {
            // add instance of ORM class
            $this->model(new User);

            // add a border to the grid
            $this->border = 1;

            // add fields
            $this->field('fullname')
                  ->title('Name:')
                  ->filter('nameFilter'); // ADD FILTER NAME HERE

            $this->field('email')
                  ->title('Email:');

            $this->field('is_active')
                  ->title('Active');
      }

      // add filter here as method
      protected function nameFilter()
      {
            // now we can get data for the current row using $this->x syntax
            // this would build a link with the user's name to the show_users page and send the user ID
            return '<a href="show_user.php?id=' . $this->user_id . '">' . $this->fullname . '</a>';
      }
}


Customize Query

The default query will simply pull all records from the current model/table (if the model/table is set). You can customize the WHERE and ORDER BY clause of a query like this: [...]
class UserGrid extends SadoGrid
{
      // initialize the grid
      public function __construct()
      {
            // add instance of ORM class
            $this->model(new User);

            // set custom WHERE clause (only get active users)
            $this->query_where = 'is_active = 1';

            // set custom ORDER BY clause (order by user ID in descending order)
            $this->query_order_by = 'user_id DESC';
[...]
Or you can write a completely custom query by using the 'getQuery' method:[...]
class UserGrid extends SadoGrid
{
      // initialize the grid
      public function __construct()
      {
[...]
      // add custom query method
      protected function getQuery()
      {
            // return custom query as string
            return '
                  SELECT *
                  FROM users
                  WHERE is_active = 0
                  ORDER BY user_id DESC
            ';
      }
[...]


Customize Query

You can also customize the records that are used for the grid. To customize or return your own records use the 'getRecords' method: [...]
class UserGrid extends SadoGrid
{
      // initialize the grid
      public function __construct()
      {
[...]
      // add custom records method
      protected function getRecords()
      {
            // return custom records (this can be any type of multidimensional array)
            return $this->my_custom_records;
      }
[...]