TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 11-27-2010, 10:03 AM   #1 (permalink)
The Wanderer
 
Join Date: Jan 2009
Posts: 8
Thanks: 0
ttmtake is on a distinguished road
Default 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 is offline  
Reply With Quote
Old 11-29-2010, 08:53 AM   #2 (permalink)
The Wanderer
 
Join Date: Jan 2009
Posts: 8
Thanks: 0
ttmtake is on a distinguished road
Default

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'];
      }
                                }
                                   }
    } 
ttmtake is offline  
Reply With Quote
Old 11-29-2010, 09:30 AM   #3 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

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)
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 11-29-2010, 10:50 AM   #4 (permalink)
The Wanderer
 
Join Date: Jan 2009
Posts: 8
Thanks: 0
ttmtake is on a distinguished road
Default

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.)
ttmtake is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Filling an array with values from a single column over specific rows in MySQL DB gj384 Absolute Beginners 2 11-11-2010 08:45 PM
feedback on my class please frostyboy33 Advanced PHP Programming 5 05-11-2009 07:42 AM
Array mess Killswitch Absolute Beginners 4 12-14-2008 06:35 AM
array elements into variables and values Dave Absolute Beginners 7 06-20-2008 01:56 PM
Part 1: Getting Started with Array Functions Wildhoney Absolute Beginners 6 10-01-2007 10:53 AM


All times are GMT. The time now is 05:30 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design