Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development

Effortless Flex 4 Development: Data Paging

  • Print
  • + Share This
In this excerpt from Effortless Flex 4 Development, Larry Ullman shows you how to use data paging, where only a subset of records are fetched with each service call.
From the book

Data Paging

By default, a function like getAllProducts( ) or getEmployees( ) is set to return every record from the database. When you're just getting started, with maybe only a couple of dozen of records, this isn't a problem, even though the Flex component—a DataGrid or List, perhaps—will only display a few at a time. But as the datasets become larger, into the thousands of records, you absolutely don't want to return all of the information with each request. Instead, you can add paging to your application, where only a subset of records are fetched with each service call. To do so, two additional methods must be added to the PHP script that is the service. Then you use Flash Builder to add paging to the component that displays the records.

Updating the PHP Script

You must first define two new methods in your PHP class: count( ) and getThings_paged( ). For the latter, Things should be replaced with the class name or something else meaningful: getProducts_paged( ) or getEmployees_paged( ).

The count( ) function should just return a number indicating how many total records exist. You can achieve this using the following:

public function count( ) {
  $q = 'SELECT COUNT(*) FROM products';
  $r = mysqli_query($this->dbc, $q);
  $data = mysqli_fetch_array($r, MYSQLI_NUM);
  return $data[0];

The count( ) method is used by the Flex application to know how many total "pages" of records exist. For example, if there are 904 records in the database and 20 at a time are being fetched, then there are 46 "pages" (with only four records on the last "page").

The getThings_paged( ) method needs to take two arguments. The first is a number indicating where in the result set to start retrieving from. The second argument is the number of items to fetch:

public function getProducts_paged($start, $num) {

This function would then only differ from a getAllProducts( ) function in that its query would apply a LIMIT clause (when using MySQL):

$q = "SELECT * FROM products LIMIT $start, $num";

The complete getProducts_paged( ) method would look like the following:

public function getProducts_paged($start, $num) {
  $data = array( );
  $start = (int) $start;
  $num = (int) $num;
  $q = "SELECT * FROM products LIMIT $start, $num";
  $r = mysqli_query($this->dbc, $q);
  if (mysqli_num_rows($r) > 0) {
      while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
        $data[] = $row;
  return $data;

Updating the Flash Client

Once the PHP script has been written to support paging, you can tell the Flex application to use it. If you haven't yet added the service to the project, you would do that first. If you've already created the service in Flash Builder, you can just right+click (Windows) or Control+click (Mac OS X) the service in the Data/Services panel and select Refresh from the contextual menu. The new methods should then be recognized. You'll also need to configure the operation's return and input types (which you've seen many times over by now). Then...

  1. Right+click (Windows) or Control+click (Mac OS X) the getProducts_paged operation in the Data/Services panel.
  2. Select Enable Paging from the contextual menu.
  3. In the first Enable Paging window, make sure the id property is checked as the primary key, then click Next.
  4. In the second Enable Paging window, enter a number for the Page size (Figure 9.26).

    This is the number of items to retrieve per request. It corresponds to the second argument in the PHP script's getProducts_paged( ) method.

  5. Also in that same window, make sure the count( ) method is selected for the Count operation.
  6. Click Finish.
  7. Bind the paging method to the DataGrid. To do so, you can just drag the getProducts_paged operation onto a DataGrid. If the DataGrid is already bound to an existing operation, you'll be prompted about changing the binding.
  8. When you scroll through the DataGrid, the rows will be fetched and displayed one "page" at a time.
  • + Share This
  • 🔖 Save To Your Account