Search mysql LIKE using php array

Hello,

I have a small table with just few columns, one of them is storing the information about the Category as an INT (just a simple number) eg.


id | name                  | category

11 | one file              | 1
15 | some other file       | 2

When displaying the data accross the site, I simply convert the category’s number with the value stored in the PHP array:

 $swm_array = array(
 '1' => 'Electrical',
 '2' => 'HVAC',
 '3' => 'Plumbing',
 );

When searching the table using LIKE operator, I can easily find the category if I type a number. What I need is to actually type in the category’s value, and not its key and then find it against the entries in the table. I’ve been trying to include the array to the search using completely weird stuff, but to no vail.

Found here, in these forums, a lead: implode INSIDE the query but I was not able to pull it.

If you guys could help me out here, I’d be eternally grateful :slight_smile:

My SQL query:

SELECT DISTINCT
    `swm_id`,
    `name`,
    `desc1`,
    `category`,
    `file`
FROM
    `swims`

WHERE
  (
       `name` LIKE '%".$q."%'
    OR `desc1` LIKE '%".$q."%'
    OR `file` LIKE '%".$q."%'
    OR `swm_id` LIKE '%".$q."%'
    >>  OR `category` LIKE '%".$q."%'  <<
  )
  AND `active` = '1'

Thanks, a lot!

Regards,
Greg

Can you post your table structure for both tables? Are you using 2 tables or is this all one table? Either way, the full table structure will be useful.

Hey, thanks for you input.
Here’s the structure of the table:

CREATE TABLE IF NOT EXISTS `swims` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `swm_id` int(4) NOT NULL,
  `name` varchar(150) NOT NULL,
  `desc1` text NOT NULL,
  `category` int(3) NOT NULL,
  `file` varchar(150) NOT NULL,
  `submitted_on` datetime NOT NULL,
  `active` enum('0','1') NOT NULL DEFAULT '1',
  PRIMARY KEY (`id`),
  KEY `id` (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

Basically what need to achieve is when I type something in the search box, eg. “Plu”, or “Plumb”, or “Plumbing” I need that to be understood by script as if I was looking for Category 3. At the same time “Elec”, or “Electric” to be translated into Category 1 cus there are only numeric values in that column.

My brain must be looking now like a potato soup, I am completely out of ideas.

Anyways,
Thanks!

Regards,
Greg :slight_smile:

Hello,

Is there a second table that holds all of the categories?

Hi,

no, there is no another table - that’s why I am having soooo many problems with that bloody thing :slight_smile:
The categories are being hold in an array:


 $swm_array = array(
 '1' => 'Electrical',
 '2' => 'HVAC',
 '3' => 'Plumbing',
 );  

Thanks for checking in, much appreciated.

Regards,
Greg

THe solution is to set up that second table to hold the category names - then you can do a join to do the search you want.

Agreed. That would be the ideal and if not too much work it would be a very good idea to make that change now.

Else you’ll need to use PHP more than should be and put together the query appending the OR and LIKE lines inside a loop.

Hi, I know the ideal solution would be to set up another db (table) and I agree that’d be the best solution. However, for few reasons that cannot be done for this project so I guess I will have to drop the idea of searching for the Category.
Thank you all for your input!

Best,
Greg

Another table for those column names would be ideal, but any case, maybe you could use something like this.

<?php 
//Testing search value//
$_POST['search'] = "el";

$swm_array = array(
 '1' => 'Electrical',
 '2' => 'HVAC',
 '3' => 'Plumbing',
 ); 
 
if(isset($_POST['search'])):
	if (strlen($_POST['search'])>1):
		$search = strtolower($_POST['search']);	
		foreach($swm_array as $k => $v):
			$match = stristr($v,$search);
			if(!empty($match)){
				$cat = array_search($match,$swm_array);
				break;
			}else{
				$cat = 0;
			}
		endforeach;
	endif;
endif;

	echo "$cat<br />";	
?>

Looks like jquery auto-complete when I see $q… Slight change to use $q.

&lt;?php
//Testing search value//
$q = "hv";

$swm_array = array(
 '1' =&gt; 'Electrical',
 '2' =&gt; 'HVAC',
 '3' =&gt; 'Plumbing',
 );

if(isset($q)):
	if (strlen($q)&gt;1):
		$search = strtolower($q);	
		foreach($swm_array as $k =&gt; $v):
			$match = stristr($v,$search);
			if(!empty($match)){
				$cat = array_search($match,$swm_array);
				break;
			}else{
				$cat = 0;
			}
		endforeach;
	endif;
endif;

	
$sql = "SELECT DISTINCT
    `swm_id`,
    `name`,
    `desc1`,
    `category`,
    `file`
FROM
    `swims`

WHERE
  (
       `name` LIKE '%".$q."%'
    OR `desc1` LIKE '%".$q."%'
    OR `file` LIKE '%".$q."%'
    OR `swm_id` LIKE '%".$q."%'
    OR `category` = '".$cat."'
  )
  AND `active` = '1'"; 	


	echo "$sql&lt;br /&gt;";	
?&gt;

Not sure what other checks you have for $q but you might need to define $cat=0; before running foreach check just in case $q is only one character in length and thus would be undefined in query.

 &lt;?php

$swm_array = array(
 '1' =&gt; 'Electrical',
 '2' =&gt; 'HVAC',
 '3' =&gt; 'Plumbing',
 );

$cat = 0;

if(isset($q)):
    if (strlen($q)&gt;1):
        $search = strtolower($q);
        foreach($swm_array as $k =&gt; $v):
            $match = stristr($v,$search);
            if(!empty($match)){
                $cat = array_search($match,$swm_array);
                break;
            }else{
                $cat = 0;
            }
        endforeach;
    endif;
endif;


$sql = "SELECT DISTINCT
    `swm_id`,
    `name`,
    `desc1`,
    `category`,
    `file`
FROM
    `swims`

WHERE
  (
       `name` LIKE '%".$q."%'
    OR `desc1` LIKE '%".$q."%'
    OR `file` LIKE '%".$q."%'
    OR `swm_id` LIKE '%".$q."%'
    OR `category` = '".$cat."'
  )
  AND `active` = '1'";


    echo "$sql&lt;br /&gt;";
?&gt;