Lamplight API examples - work record search

Any data shown on this page is fictious - this is an example!

The example

Search for work records:

The code

As in all these examples, we're using the php Lamplight_Client class, which makes constructing requests a bit easier. First, set up the client

     $client = new Lamplight_Client('', array(
       'key'     => LAMPLIGHT_APIKEY,
       'lampid'  => LAMPLIGHT_ID,
       'project' => LAMPLIGHT_PROJECT
    ));   
    

This gives us an Http Client with the authentication parameters set (we use an include to define the LAMPLIGHT_APIKEY, LAMPLIGHT_ID and LAMPLIGHT_PROJECT constants). We don't need to pass a uri because the Lamplight_Client does it for us.

In this example, we use the client twice: the first time, to get the workareas to populate the select element; the second time, to do the search. To retrieve the workareas and create the select box is fairly straightforward, particularly as we can use a template to render the options:


  $client->fetchWorkarea()
         ->fetchAll()
         ->request();
  
  $workareaRecSet = $client->getRecordSet();

  ?>
  
    <select name="workarea" id="workarea">
      <option value=""> -- select -- </option>
      <?php 
         echo $workareaRecSet->render('<option value="{id}">{text}</option>');
      ?>
    </select>  
    
  

If we're re-using the client, we need to reset it first. This will clear out any parameters previously set, but leave the API credentials (and the previous response details).

  $client->resetClient();
  
    

Then we can build the response step-by-step:

  
  // Set up the client to fetch some work
  $client->fetchWork()
         ->fetchSome();
                        
  if ($_GET['q']) {
    $client->setParameterGet('q', urlencode($_GET['q']));
  }
  if ($_GET['workarea']) {
    $client->setParameterGet('workarea', urlencode($_GET['workarea']));
  }
  
    

And then make the request, get the record set, and render it:

    // Make the request
  $client->request();
  
  
  
  // get the recordset:
  $workRecSet = $client->getRecordSet();
                   
  if (!$workRecSet->getErrors()) {
  
    ?>
    <h3>Search results:</h3>
    <?php
    
    
    // Use a template to display the results:
    $recordTemplate = '<li><h4>{title}</h4>'
         . '<i>Start date:</i> {start_date} <br/>'
         . '<i>End date:</i> {end_date} <br/>'
         . '<a href="workdetail.php?id={id}" title="see more info on {title}">'
         . 'more...</a>'
         . '</li>';
          
    echo '<ul>' . $workRecSet->render($recordTemplate)  . '</ul>';
  
  
  
  } else {
   echo 'Error!';
  }