Jump to content

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.