Jump to content

problem with search functionality


jimfog

Recommended Posts

I have built an SQL query that when some criteria are met retrieves data from a database and present is to the user as search results....is is a search query in the words.

Consider the following when the uset types the letter in the searh box the following name is retrieved Nikos Pistolou...which makes sense as there is such name in the DB....upon pressing an ajax call is made.

And here is my problem

When the user presses shift+p (that capital p) 2 ajax calls are made and as a result the above name appears twice.......an unwanted behaviour. 

What can be causing this and how to fix it?

I am attaching an image snapshot.

 

 

search_problem_LI.jpg.61c379285e923da3acbb038b8e45d54a.jpg..

Edited by jimfog
Link to comment
Share on other sites

Sounds like a job for an if statement. Can’t tell you if you need one pre or post call without seeing your actual code. 

Link to comment
Share on other sites

23 minutes ago, niche said:

Sounds like a job for an if statement. Can’t tell you if you need one pre or post call without seeing your actual code. 

An IF statement in the sql query or in the php code? I suppose you mean in the PHP code.

More importantly what this code will do…I cannot understand…please explain. Here is the code:

$arr = array();
if ((isset($_POST['searchKeyword']))&&(!empty($_POST['searchKeyword'])))
{

    $results=search($conn,$_POST['searchKeyword']);//funcction to perform the search operation

    if($results!==false)
          {
        
        
          echo json_encode($results);//array με τα αποτελέσματα
          }
          else
          {
          echo 'false';   
          }
}

 

Edited by jimfog
Link to comment
Share on other sites

maybe the IF statement must go in the JS code...just thinking.

Link to comment
Share on other sites

probably is a JS issue...relatedd to the event handler

Link to comment
Share on other sites

What's the actual  sql query you're using? 

Else, what's in $conn?

EDIT:

if not a SQL connection, $conn maybe a string that contains both possibilities. 

Edited by niche
Link to comment
Share on other sites

16 minutes ago, niche said:

What's the actual  sql query you're using? 

Else, what's in $conn?

EDIT:

if not a SQL connection, $conn maybe a string has contains both possibilities. 

this is the sql query allthough I think the problem does not lie there...the query as you see is rather complex

The function that performs the connection to the database is passed in $conn(I can show the code but I do not think is neccesary.)

SELECT IF(u.lastname IS NULL,b.comp_name,CONCAT(u.name, " ", u.lastname)) 
AS name,u.user_ID, t.user_type, b.sched_entered FROM users u JOIN business_users b ON u.user_id=b.crid 
                      JOIN buz_usertype t ON b.bus_user_type=t.type_id 
                     WHERE u.active = 1 
                       AND b.sched_entered = 1  
                       AND (u.lastname LIKE CONCAT("%",?, "%") OR  b.comp_name LIKE CONCAT("%",?, "%"));

 

Link to comment
Share on other sites

So, what’s in the table?  Are both names there?  If so, the query is part of the problem.

Either way, a few solutions come to mind, but let us know if both names are in the table.

I’d  like to know. 
 

EDIT:

Look at the lastname col AND comp_name col

Edited by niche
Link to comment
Share on other sites

3 minutes ago, niche said:

 

Either way, a few solutions come to mind, but let us know if both names are in the table.

I’d  like to know. 

Make it more clear to me cause I am confused.

Are you asking if the name Nikos Pistolou is found twice in the DB... is this what are you asking?

Link to comment
Share on other sites

No it is not found twice in the DB.

 

Meanwhile I made a little search and what finally solved the issue is that intead of tracking keyup I used the input event...that solved it.

Follows part of the event handler(JS) so you can understand what I am talking about.

    
 $('#search').on('input', function(e) {
     e.preventDefault();
    var searchKeyword = $.trim($(this).val());
    if(searchKeyword!=='')
      {////

tell me what you think...

  • Like 1
Link to comment
Share on other sites

Then, good work!  Remember to comment completely what you just did for future ref.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...