Several autocomplete fields

Talk about Filter (search) Settings

Moderator: alexandrleonenko

lambro
Master
Master
Posts: 102
Joined: Sep 13th, '10, 14:31
Location: Metz, France

Several autocomplete fields

Postby lambro » Jun 30th, '11, 14:22

Hi!
I read in a previous post that through the DG template form, only one autocomplete field was allowed.
My solution ? DOING IT OUTSIDE OF THE DG TEMPLATE !!
For example, there are two filtering fields that you would like to use autosuggest with, say ,
off_ff_ta_bureau_nom_bur, and off_ff_ta_pays_nom_pays.
Just write the javascript code for those fields in a <script> tag at the end of the body of your page.

Code: Select all

<script type="text/javascript">
   var options_xml = {
   script:"autosuggest/bur01.php?",
   varname:"input",
   timeout:"5000"
   };
   var as_xml = new AutoSuggest('off_ff_ta_bureau_nom_bur', options_xml);
   </script><script type="text/javascript">
   var options_xml = {
   script:"autosuggest/bur02.php?",
   varname:"input",
   timeout:"5000"
   };
   var as_xml = new AutoSuggest('off_ff_ta_pays_nom_pays', options_xml);
   </script>

As you can see, I am not accustomed to Json, so a simple xml will do the trick.
Now, in the autosuggest folder, create your needed files in accordance with the fields you use for searching.
For example :

Code: Select all

require_once("../include/mysql_Class.php");
$utilNames = new DBtab();
$sql = "select nom_bur  from ta_bureau order by nom_bur";
$utilNames->query($sql);
$aUsers = array();
for ($i=0; $i < $utilNames->num_rows(); $i++){
//$test02 .=  ",\"".$utilNames->string_record($i)."\"";
$row = $utilNames->num_record();
$aUsers[$i]= $row[0];
}
$utilNames->close();
   
   $input = strtolower( $_GET['input'] );
   $len = strlen($input);
   
   $aResults = array();
   
   if ($len)
   {
      for ($i=0;$i<count($aUsers);$i++)
      {
         // had to use utf_decode, here
         // not necessary if the results are coming from mysql
         /*old
         if (strtolower(substr(utf8_decode($aUsers[$i]),0,$len)) == $input)
            $aResults[] = array( "id"=>($i+1) ,"value"=>htmlspecialchars($aUsers[$i]), "info"=>htmlspecialchars($aInfo[$i]) );
         */
                        //new !! Let's you see parts of results (= %like%)
                        if ( strstr(strtolower(utf8_decode($aUsers[$i])),$input)!= "")
            $aResults[] = array( "id"=>($i+1) ,"value"=>htmlspecialchars($aUsers[$i]), "info"=>htmlspecialchars($aInfo[$i]) );
         //if (stripos(utf8_decode($aUsers[$i]), $input) !== false)
         //   $aResults[] = array( "id"=>($i+1) ,"value"=>htmlspecialchars($aUsers[$i]), "info"=>htmlspecialchars($aInfo[$i]) );
      }
   }

   header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
   header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
   header ("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
   header ("Pragma: no-cache"); // HTTP/1.0
   
   if (isset($_REQUEST['json']))
   {
      header("Content-Type: application/json");
   
      echo "{\"results\": [";
      $arr = array();
      for ($i=0;$i<count($aResults);$i++)
      {
         $arr[] = "{\"id\": \"".$aResults[$i]['id']."\", \"value\": \"".$aResults[$i]['value']."\", \"info\": \"\"}";
      }
      echo implode(", ", $arr);
      echo "]}";
   }
   else
   {
      header("Content-Type: text/xml");

      echo "<?xml version=\"1.0\" encoding=\"utf-8\" ?><results>";
      for ($i=0;$i<count($aResults);$i++)
      {
         echo "<rs id=\"".$aResults[$i]['id']."\" info=\"".$aResults[$i]['info']."\">".$aResults[$i]['value']."</rs>";
      }
      echo "</results>";
   }
?>

You'll get the same result as the one provided by the datagrid class. It's much heavier to put in place, though, but this way you lift the limit.
2autosugg.png
2autosugg.png (19.05 KiB) Viewed 1113 times

@+ all

Return to “Settings: Filtering (search) / Autocomplete”