Ok, here goes:
First of all thanks drawmack for your great help, Permutations is what i am doing but i didnt know thats what its called 🙂 The Link you send about permutations helped alot, ive gotten a function that does about half the work, however when looking at the output i noticed that some permutations i was looking for were missing, i do not know why, but then someone from a other forum showed me a better, faster way to solve this , to which i will get to in a second. First here's the permutation function, as i said not perfect yet but almost does its job:
(Thanks to gamefreak64 for help on porting this from c to php)
<?php
$elements = array (2, 3, 4);
print_r (permutation($elements));
function permutation($elements) {
$length = count($elements);
// calculate the number of all permutations
$fact = 1;
for ($i=$length; $i > 1; $i--) {
$fact = $fact * $i;
}
$indizes = range(0,$length-1);
$perms = array();
// this is the first permutation
$perms[0] = $elements;
for ($i=1; $i < $fact; $i++) {
$j = count($indizes) - 2;
while ($indizes[$j] > $indizes[$j+1]) {
$j--;
}
$k = count($indizes) - 1;
while ($indizes[$j] > $indizes[$k]) {
$k--;
}
$temp = $indizes[$k];
$indizes[$k] = $indizes[$j];
$indizes[$j] = $temp;
$r = count($indizes) - 1;
$s = $j + 1;
while ($r > $s) {
$temp = $indizes[$s];
$indizes[$s] = $indizes[$r];
$indizes[$r] = $temp;
$r--;
$s++;
}
foreach($indizes as $index)
$perms[$i][$index] = $elements[$index];
}
return $perms;
}
?>
Now to the actuall and perfect working solution.
(Thanks to Theeggman for having the idea):
First your create in mysql tables with the elements you need for example
green blue red
you create the same amount of tables as the amount of elements, so in this case
3 tables:
t1, t2, t3 , each containing a field with a element per row,
then you do a simple cartesian cross query:
SELECT t1., t2.,t3.* FROM t1, t2, t3;
Mysql will return all combinations of the elements, im not really sure why it does that, if i had seen the query without knowing i would have thouight it will just deliever the elements three times in row, but it actually sorts it out correctly. Pretty cool! This solution only took me 3 minutes to implement, so its the way im going now.
Thanks to everyone for your great help!