can this search code be altered?

Discussion in 'PHP' started by cornersoyo, Jun 19, 2008.

  1. #1
    Hello

    We have a website in php and the original programmer is no longer around. Problem is, the search box on the site seems to only work as a complete phrase instead of performing an AND or OR on the words typed into it. It seems to work by collecting the search input and assigning it directly to variable 'q', and upon submittal it reloads the index.php and within index.php I found the following code:

    
    if ($_STEP >= 1) {
    		if (!isset($_dbid)) {
    			$_dbid = db_connect($_SETTINGS["name"], $_SETTINGS["host"], $_SETTINGS["user"], $_SETTINGS["password"]);
    		}
    		$_PRODUCTS = array();
    		$_RELATED_PRODUCTS = array();
    		$_query = "";
    		if (do_set_var("q")) {
    			$_STEP = 4;
    			$_CRUMBS[] = array(
    				"",
    				"",
    				"",
    				"Search Results"
    			);
    			$_query = " and (lower(name) like '%" . addslashes(strtolower($_VARS["q"])) . "%'";
    			$_query .= " or lower(sku) like '%" . addslashes(strtolower($_VARS["q"])) . "%'";
    			$_query .= " or lower(description) like '%" . addslashes(strtolower($_VARS["q"])) . "%')";
    		}
    		else {
    			if (strstr(getenv('REQUEST_URI'), "/search/")) {
    				if ($_MODREWRITE) {
    					header("Location: " . $_BASE . "" . $_XYZ);
    				}
    				else {
    					header("Location: " . $_BASE . "?" . $_XYZ);
    				}
    				exit;
    			}
    
    PHP:
    Well, I'm not a programmer, just have accumulated enough knowledge to make some minor changes/etc. to scripts we have used... but based on this it seems like it is generating a query phrase to check 3 places, (name, sku and description) against whatever was entered and assigned to 'q'...?

    If this is the case, I am wondering if there is a way to make a change within this code snippet by which the text in 'q' can be checked by the individual words entered, and not just as a sequential phrase?

    For example, if we have a product with 'Candle' in the name, and 'White' in the description, entering 'white candle' should find it... but as it is, entering 'white candle' will only find products with the exact complete phrase 'white candle' in one of the 3 areas it is checking.

    I hope I'm supplying enough information with this and someone here might be able to help... Thanks very much
     
    cornersoyo, Jun 19, 2008 IP
  2. Dannyboyonline

    Dannyboyonline Active Member

    Messages:
    602
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    60
    #2
    of course you can change that, you just need to change the query.
     
    Dannyboyonline, Jun 19, 2008 IP
  3. cornersoyo

    cornersoyo Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks - yeah, I didn't ask that right - I meant could it be done easily within the code I found above... or would it take something more involved (or in a different part of the code)...
     
    cornersoyo, Jun 19, 2008 IP