Jump to content

PHP / MySQL Search Function


Portwolf

Recommended Posts

Ok to start things off, I am doing a revamp on a custom PHP CMS and the client wants the search function to do the following:

 

Lets say a user searches for "WIX 24061" the current search function will mark that as not found and forward to the notfound page but when i search for "24061" it pulls it up.

 

So what I decided i would do is an explode to a var and use a LIKE SQL query. But it is not working. Heres a snippet of the code that will should be working but its not.

 

		$whereExtra = '';
		if(!empty($_SESSION['search']))
		{
			if(strpos($_SESSION['search'], ',') !== false)
			{
				$parts1 = explode(',', $_SESSION['search']);
				for($i = 0; $i < count($parts1); $i++){
					$whereExtra .= " AND (cp.sku LIKE '%" . $parts1[$i] . "%' OR cp.description LIKE '%" . $parts1[$i] . "%') ";
			}
		}
			else
			{
				$whereExtra = " AND (cp.sku LIKE '%" . $_SESSION['search'] . "%' OR cp.description LIKE '%" . $_SESSION['search'] . "%') ";
			}

 

I have also tried the following:

 

		$whereExtra = '';
		if(!empty($_SESSION['search']))
		{
			if(strpos($_SESSION['search'], ',') !== false)
			{
				$parts1 = explode(',', $_SESSION['search']);
				foreach($parts1 as $p)
					$whereExtra .= " AND (cp.sku LIKE '%" . $p . "%' OR cp.description LIKE '%" . $p . "%') ";
			}

			else
			{
				$whereExtra = " AND (cp.sku LIKE '%" . $_SESSION['search'] . "%' OR cp.description LIKE '%" . $_SESSION['search'] . "%') ";
			}
		}

 

Any Ideas?

Link to comment
https://forums.phpfreaks.com/topic/181238-php-mysql-search-function/
Share on other sites

Your search string "WIX 24061" does not have a comma in it, so the explode is not being executed.  Shouldn't you change the strpos() and explode() to be looking for a space?

            if(strpos($_SESSION['search'], ' ') !== false)
            {
               $parts1 = explode(' ', $_SESSION['search']);

 

Or did I misunderstand the issue?

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.