Page 1 of 1

Combining arrays then breaking each combo for a query...

Posted: Thu Jul 02, 2009 12:39 am
by annnthony
So I'm trying to execute a database query that takes the combination of two arrays (1,2,3) and (cat,dog,mouse,elephant) and combine them into all possible combinations: 1cat, 1dog, 1mouse, 1elephant, 2cat, 2dog, 2mouse, 2elephant etc.

I can get the possible combinations printed, but I need to do more than print. Taking the first character of the new element and separating it from the rest of the element, I have two variables that are supposed to connect with the other stuff within the script and execute the msyql query. This doesn't happen and results vary. Any suggestions as to what I'm doing wrong (code below) and how it can be fixed?

Code: Select all

 
function showCombinations($string, $autoshows, $i)
    {
        if ($i >= count($autoshows))
        {
            echo trim($string) . "<br>";
        }
        else
        {
            foreach ($autoshows[$i] as $autoshow)
                showCombinations("$string$autoshow", $autoshows, $i + 1);
                
$type = substr($autoshows,1);
$level = substr($autoshow,0,1);
                
--MYSQL QUERY INSERT--
$showID = mysql_insert_id();
 
    $amount1 = 150;
    foreach(range(0, $amount1-1) as $value)
{
    --MYSQL QUERY INSERT--
    --MYSQL QUERY INSERT--
}
    
        }
    }
    
$autoshows =
    array (
        array(1,2,3),
        array(dog,cat,mouse,elephant)
        );
    
    showCombinations('', $autoshows, 0);
    
    
    echo "<b>Made!</b>";
 
I've omitted the parts of the code that I don't feel are completely necessary in solving the problem. Essentially what I need to do is have an element for each combination of the two arrays and then be able to break each element into two pieces and run a mysql query that uses the substr()'ed variables.

Re: Combining arrays then breaking each combo for a query...

Posted: Thu Jul 02, 2009 11:47 am
by annnthony
That's excellent--and far far more simpler than what I was trying to do. Thanks for the help!