I have two tables, one is called "options" and the other is called "option_values". The first one contains entries such as: "size, color, shape" while the other has a "option_id" field linking it back to the "options" table and with entries like: "small, medium, red, green, square, etc..." depending on what the option was.

Now what I am trying to do is find ever possible combination for all option values. For example with three values for each option (as listed above) one possible combination could be: "small/red/square" or "medium/blue/circle".

Now the tricky part is that I don't know how many options (ex: size or color) there are... could be just one in which case it's easy but it could be up to 3 or more in which case it gets pretty tricky.

From past experience I would think it would be a loop within a loop, so the first time it finds the first set of options/values and then calls itself again to find the next option/value and it does this until it reaches the last option at which point the previous iteration continues with the next in line option/value. I dunno if that made any sense but I am having a problem writing this function (or multiple functions). If anyone has any ideas please let me know. Also what would be the best way to store these "combinations" in a database?

Thanks in advance.

I think you know how to retrieve data from the database. So I only post the code you are asking:

<?php
// Put here the database connection

$qOptions1 = mysql_query("SELECT * FROM Options1");

foreach (mysql_fetch_row($qOptions1) as $val){
	$aOptions1[] = $val; // generate the array for you with the options
}
// Do also with the options2 and 3

$count1 = count($aOptions1);
$count2 = count($aOptions2);
$count3 = ..

// You could  this also do with a foreach..
for ($a=0; $a < $count1; $a++){
	for ($b=0; $b < $count2; $b++){
		for ($c=0; $c < $count3; $c++){.
			$options = $aOptions1[$a] . $aOptions2[$b] . $aOptions3[$c];
		}
	}
}
// The answer you asked, should be something like above. Its a little rumble, and garbage what I typed, but gives you THE idea.
?>

Thanks for the help... that definitely helps however I don't know how many options there are, there could be only one or there could be 5. And they are not limited to just size, color, or shape.

Right now I have PHP generating the following array. I do need to know the ID number of each option value. And keep in mind that the options and values can change from product to product:

Array
(
    [0] => Array
        (
            [Color] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [value] => Small
                        )

                    [1] => Array
                        (
                            [id] => 2
                            [value] => Medium
                        )

                )

        )

    [1] => Array
        (
            [Size] => Array
                (
                    [0] => Array
                        (
                            [id] => 3
                            [value] => SMall
                        )

                    [1] => Array
                        (
                            [id] => 4
                            [value] => Medium
                        )

                    [2] => Array
                        (
                            [id] => 5
                            [value] => Large
                        )

                )

        )

    [2] => Array
        (
            [Shape] => Array
                (
                    [0] => Array
                        (
                            [id] => 6
                            [value] => Square
                        )

                    [1] => Array
                        (
                            [id] => 7
                            [value] => Circle
                        )

                    [2] => Array
                        (
                            [id] => 8
                            [value] => Triangle
                        )

                )

        )
)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.