Page 1 of 1

Comparing two sets

Posted: Thu Feb 14, 2008 8:11 pm
by GeXus
I posted a similar problem in the database thread, but this is different and I think it's more of a PHP thing... I have two tables,

1. Groups
- country_codes

2. Sets
- country_codes


The country codes are comma delimited (i.e, US,UK,CA would be a record set). I want to find which ones have intersecting country_codes. So, if I select country_codes from "Groups" and it returns UK,US,CA - I want to then get all "Sets" that have country_codes values of any of the three. So if a record has AU,FI,US, it would return...

So, I'm thinking of just selecting the set I want, then selecting all groups and using array_intersect(), however I can't seem to figure out how to maintain the associated array. I've tried array_intersect_assoc, but then It's comparing against the whole associated array (which includes ID, Name and country_codes), not just the country_codes.

Anyone have any ideas? Or did I just confuse the hell of you..... :)

Re: Comparing two sets

Posted: Thu Feb 14, 2008 8:14 pm
by Benjamin
Are they stored in a blob or something? They are stored comma delimited?

Re: Comparing two sets

Posted: Thu Feb 14, 2008 8:22 pm
by Benjamin
Just typed this to give you a rough idea of how I would do it.

Code: Select all

 
$search_for = explode(',', 'UK,US,CA');
 
$sets = array();
$db->query("select id, data from sets");
 
while ($x = $db->get_row())
{
    $sets[$x['id']] = array_map('trim', explode(',', $x));
}
 
$results = array();
 
foreach ($sets as $id => $codes)
{
    if (in_array($search_for, $codes))
    {
        $results[] = $id;
    }
}
 
# results contains record id's of matching sets..
 
 

Re: Comparing two sets

Posted: Thu Feb 14, 2008 8:54 pm
by Christopher
Or:

Code: Select all

$country_codes = 'UK,US,CA';   // value from Groups
$codes= explode(',', $country_codes);
 
$where = "country_codes LIKE '%" . implode("%' OR country_codes LIKE '%", $codes) . "%'";
$db->query("SELECT * FROM Sets WHERE $where");
 
while ($x = $db->get_row())
{
# results contains record id's of matching sets..
}