Back to Code repository sumary

Improved search in a table showing db request with joins

Explanations

This is an improved way to do searching in a table, when the table contains joins (the standard proposed way generates some troubles).

Code snippet

I propose this replacement for the “search” function to be written in your mask:

...
  function search()
  {
    // for example
    $value = $this->txtSearch->getNewValue();
 
    $where = "";
    // I assume here that you have create an array containing the names of
    // all the visible columns in your table
    foreach($this->visibleColumnsInTable as $name)
    {
      if ($this->_use_fields_aliases)
      {
        foreach($this->src->getFields() as $field_name=>$field_alias)
        {
          if ($field_alias == $nom)
          {
            $name = $field_name;
            // should break here, but I don't know enougth PHP to do it ;)
          }
        }
      }
 
      if ($where !== "")
        $where = $where . " or ";
 
      $where = $name . " like '%{$value}%'";
    }
 
    $this->src->setWhere($where);
    $this->src->firstRow();
    $num_rows = $this->src->getNumRows();
 
    if (!$num_rows)
    {
      // Say user there is no result corresponding to his search
      $this->message->setValue(__("No results were found"));
 
      // Cancel the search criteria
      $this->src->setWhere(null);
 
      // Show again all datas
      $this->src->firstRow();
    }
  }
...

User's comments

2007-01-15: Please ask question in forum, and say comments after here - thank’s - pespie

 
improved-search-in-a-table-showing-db-request-with-joins.txt · Last modified: 2007/01/15 09:54 by 213.103.112.105
 
Creative Commons License Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki