Jump to content

[SOLVED] Filter SELECT search result!


simpjd

Recommended Posts

Hello, I'm trying to do a mysql select statement but incorporate something like a preg_replace or trim into the search!

 

So, for instance: $sql = "SELECT * FROM products where category=$_GET['category']"; could be something like:

$sql = "SELECT * FROM products where preg_replace('/[^a-zA-Z0-9s]/', '',category)=$_GET['category']";

 

But obviously this doesn’t work! So, for example, If $_GET="tests123ab" I want the search to pick up "TeSt's 123 aB" (I want to strip whitespace and symbols from the table entities during the search!"

 

If someone could help me with this, I would be greatly appreciative!

 

 

Jack

Link to comment
https://forums.phpfreaks.com/topic/125123-solved-filter-select-search-result/
Share on other sites

Looks like you want to make use of sql's LIKE

 

SELECT column FROM table WHERE column LIKE 'something'

 

You can use some regex withing your 'something' to filter results.  For instance, if you have a column like so:

 

Names

John

Joe

Jane

Joanna

Mary

 

and you ran this query:

 

SELECT names FROM table WHERE names LIKE 'Jo%'

 

John, Joe and Joanna would be returned.  You will pretty much have to build your '...' string the way you want it to be returned, just like with any regex operation.  So for instance, if you want "tests123ab"  to return "TeSt's 123 aB" you would have to add %'s like so: "%TeSt%s%123%aB%" That's something you're going to have to build with php and there's really no set method of doing it; it just depends on how "open ended" you want your search to be. 

 

In php you could easily explode your $_GET var and add a % between every character and it would return that data just the same, but I don't really think that's all that practical...

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.