Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7438153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:30:36+00:00 2026-05-29T10:30:36+00:00

I am trying to make a search box for an ecommerce website. The search

  • 0

I am trying to make a search box for an ecommerce website.

The search works as follows

When a user searches for a product, the search value is being sent to a file called searchResults.php using post method via ajax

$searchVal=$_POST['searchVal'];

And then its being searched in the database from a table named product by the following query

$searchResult = mysql_query("SELECT * FROM products WHERE name LIKE '$searchVal'")

and the results are sent back as ajax response by the following if condition

if($searchResult){
 echo "result";

}
else{

 echo "No products found";     
  }

Above all everything works fine as expected.

lets assume an user is searching for cellphones and he/she types cell phone . But we have products only for category cellphones and not for cell phone. So it results No products found even though the records for cellphones are present.

I want to make it search regardless the white space, singular or plural . How can i do that ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T10:30:36+00:00Added an answer on May 29, 2026 at 10:30 am

    The right way to implement a search engine is to maintain a separate table of words and links to the record they appear in. Then….

     $qry="SELECT p.*, COUNT(*) 
        FROM products p
        INNER JOIN srchwords s
        ON p.id=s.product_id ";
     $searchVals=explode(' ',$_POST['searchVal']);
     foreach ($searchvals as $k=>$s) {
        $searchvals[$k]="'" . mysql_real_escape_string(trim($s)) . "'";
     }
     $qry.="WHERE s.word IN (" . implode(",",$searchvals) . ") ORDER BY COUNT(*) DESC";
    

    An ugly and innefficient hack would be:

     $qry="SELECT p.*
       FROM products p";
     $join=" WHERE "
    
     $searchVals=explode(' ',$_POST['searchVal']);
     foreach ($searchvals as $k=>$s) {
         $qry.=$join . " p.desc LIKE '%" . mysql_real_escape_string(trim($s)) . "%'
         $join=' OR ';
     }
    

    Both methods still don’t not cater for plurals (just add an additional comparison for words ending in S, removing the S). You should also clean up the string to remove multiple spaces and punctuation (/^[a-z0-9 ]/i).

    Or just use one of the many, well written off-the-shelf search engine solutions (e.g. the mnogo engine or Google’s site search service).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make a search box in my web application, and I
I am trying to make a search page where the user can search project
Hi I'm trying to make an app in which there's a search box, I've
I'm trying to make a simple search box. How can I trigger a function
I'm trying to make a search feature that will search multiple columns to find
I am trying to make a search function using the combination of DOM, PHP
im trying to make a simple search on my database from the index action
I've been trying to make a simple search inside a static HTML page using
Afternoon, I am trying to make a simple search, which is working for the
I'm trying to make a Prolog predicate iscontained/2 : iscontained(List, Search) where it returns

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.