TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Comparing string from array with mysql field (http://www.talkphp.com/advanced-php-programming/5645-comparing-string-array-mysql-field.html)

ttmtake 11-27-2010 11:03 AM

Comparing string from array with mysql field
 
Hi, i have one problem with my search script

i have multiple checkboxes in search form, when user check eg. 5 boxes, after form process i take array and implode that array into one string and i get eg. $implodString = 1|4|27|34|55;

So, what i need is a way to compare this with mysql field where i have just one value eg. |4| - (this is in my field).

I need something like this
SELECT FROM `TABLE` WHERE `FIELD` LIKE %$implodString%

with showen values looks like this

SELECT FROM `TABLE` WHERE `|4|` LIKE % 1|4|27|34|55 %

i need to find all fields where we have any number from imploded array, in this case i need all fields where we have number 1,4,27,34 or 55.

Thank you for help.

ttmtake 11-29-2010 09:53 AM

I got something, this checks 2 rows,

PHP Code:

$checkDB mysql_query("SELECT * FROM `table`");
while(
$row mysql_fetch_array($checkDB)) { 

      
      
$rowCat str_replace('|',''$row['cat']);
      
$rowCity str_replace('|',''$row['city']);
     
      
$cat = array($_POST['cat']); 
      
$city= array($_POST['city']); 
      foreach(
$rowCat as $values){
      foreach(
$rowCity as $valuez){      
      
          if(
in_array($cat$values) && in_array($city$valuez)) {
          echo 
$row['id'];
      }
                                }
                                   }
    } 


sketchMedia 11-29-2010 10:30 AM

Would the IN() function of MySQL be a better approach to the problem.

sql Code:
SELECT * FROM `table` WHERE `field` IN(1, 4, 27, 34, 55)

ttmtake 11-29-2010 11:50 AM

lol, tnx, i total forgot on IN(),

Firstly i was looked a way to compare two array srings eg. 2,4,6,9 compare with 5,3,2,4,7,8, but i cant find way to do that, so i add just one value in field and i just continue to write and forgot IN(), if u have any suggestion on how to compare 2 different array please write, (it will be a case when these 2 arrays wouldnt have same number of values inside, eg. 3,4,5 and 5,6,4,2 - in 1 array we have 3 values and in sec. we have 4.)


All times are GMT. The time now is 04:20 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0