Jump to content

[SOLVED] Ajax auto complete


waterssaz

Recommended Posts

Hi,

any help would be appreciayed here.

Basicallly I have devised a simple ajax complete search box for my php application,but the results I get back are not quite as expected.

When searching by name I am getting the expected results but everything is duplicated.

I am very new to Ajax but class myself as fairly competent in PHP.

The code below is the code that the javascript uses to process the request.


<?php
header("Cache-Control: no-cache, must-revalidate");
// Date in the past
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

?>
<html>
<head></head>
<body>
<?php
include("stud_data_database.php");
//********************************..........................................start of database query populated array for testing purposes
$string=preg_replace('/\s+/','',$_GET["q"]);
$q = "SELECT distinct top 100 StudentRefNo,studentid, forename, otherforenames,
  surname, forename+surname FROM ".TBL_STUDENT_DETAILS." 
  WHERE StudentRefNo like '".str_replace("'","''",$_GET["q"])."%'
  OR studentid like '".str_replace("'","''",$_GET["q"])."%'
  OR forename+surname LIKE '%".str_replace("'","''",$string)."%'
  OR surname like '".str_replace("'","''",$_GET["q"])."%'
  ORDER by surname, forename";

$result = $database->query($q);
$a=array();
while($row=mssql_fetch_array($result))
{
$student_ref = $row["studentid"];
$student_fullname= $row["forename"].' '.$row["surname"];
$student_forename = $row["forename"];
$student_othernames = $row["otherforenames"];
$student_surname = $row["surname"];
$student_fullref= $row["StudentRefNo"];


$b=array($student_ref, $student_fullname, $student_surname, $student_othernames, $student_forename,$student_fullref  );
$b=array_unique($b);
array_push($a,$b);
}

print_r($a);
echo count($a);
//********************************..........................................end of database query populated array for testing purposes
$q=trim($_GET["q"]);             //lookup all hints from array if length of q>1
if (strlen($q) >1)
{
  for($i=0; $i<count($a); $i++)
  {
  //
  for($col=0;$col<6;$col++)
  {
  
  if (strtolower($q)==strtolower(substr($a[$i][$col],0,strlen($q))))
    {
 //$student_ref=$a[$i][0];
 $student_ref=$a[$i][0];
 $student_fullname=$a[$i][1];
 $student_surname=$a[$i][2];
 $student_othernames=$a[$i][3];
 $student_forename=$a[$i][4];
 $student_fullref=$a[$i][5];

    if ($hint=="")
    {

$hint="<br><span style=\"cursor: pointer\" onclick=\"load('stud_data_index.php?id=$student_ref','content')\">".$student_ref." | ".$student_forename." ".$student_othernames." ".$student_surname."</span><br><br>";

     }
     else
    { 
 $hint=$hint."<span style=\"cursor: pointer\" onclick=\"load('stud_data_index.php?id=$student_ref','content')\">".			$student_ref." | ".$student_forename." ".$student_othernames." ".$student_surname."</span><br><br>";
 }  
    }

}
  }
}

// Set output to "No matches (Please check your spelling and/or spelling)" if no hint were found or to the correct values.
			if ($hint=="")
						{
							$response="No matches (Please check your spelling and/or your syntax)";
						}
						else
							{
								$response="Suggestions.....<br><br>".$hint;         
							}

			//output the response
			echo $response.'<br>';
?>
</body>
</html>



Link to comment
https://forums.phpfreaks.com/topic/142954-solved-ajax-auto-complete/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.