Jump to content

[SOLVED] PHP: Generate All Possible Combinations Grid


firthusa

Recommended Posts

I have two tables, one is called "options" and the other is called "option_values". The first one contains entries suchs 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 dont 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 itteration 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.

Link to comment
Share on other sites

try

<?php 
$sizes = array ('small', 'medium', 'large');
$colours = array ('red', 'blue','green','yellow');
$shapes  = array ('square', 'circle', 'triangle');

foreach ($sizes as $s)
foreach ($colours as $c)
	foreach ($shapes as $h)
		echo "$s / $c / $h <br/>";

?>

 

3 db tables, size, colour and shape

[pre]

sizes                            colours                          shapes                 

+-------+---------------+        +-------+---------------+        +-------+---------------+

|  id  |    size      |        |  id  |    colour    |        |  id  |    shape      |

+-------+---------------+        +-------+---------------+        +-------+---------------+

|  1    |  small      |        |  1    |  red        |        |  1    |  square      |

|  2    |  medium      |        |  2    |  blue        |        |  2    |  circle      |

|  3    |  large      |        |  3    |  green      |        |  3    |  triangle    |

+-------+---------------+        |  4    |  yellow      |        +-------+---------------+

                                +-------+---------------+

[/pre]

$sql = "SELECT size, colour, shape FROM sizes, colours, shapes";
$res = mysql_query($sql);
while (list($s, $c, $h) = mysql_fetch_row($res))
{
echo  "$s / $c / $h <br/>"; 
}    

Link to comment
Share on other sites

Thanks that would work perfect if I knew there would only be 3 options. The size, color, and shape are just samples... the options are being created from an admin that stores them in a "product_options" db. It could be "flavor, material, etc..." so all the options have to be in one table and there can be any number of options and values. I also need to be able to store the "product variation" in the database for keeping inventory. So a SKU and Inventory number can be assigned to that specific combination of option values. But how owuld I store that in a DB?

SKU | INVENTORY | VALUE1 | VALUE2 | VALUE3 | ETC...

 

All help is greatly appreciated.

Link to comment
Share on other sites

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
                        )

                )

        )
)

Link to comment
Share on other sites

your array has redundant levels. Something like this would achieve the same thing, only easier

<?php
<?php
$options = array (
'color' => array (
             1 => 'red',
             2 => 'green',
             3 => 'blue',
             4 => 'yellow'
			),
'size'  => array (
             1 => 'small',
             2 => 'medium',
             3 => 'large'
			),
'shape' => array (
             1 => 'square',
             2 => 'circle',
             3 => 'triangle'
			) 
); 

 

to find the id of shape "circle"

 

$circleID = array_search ('circle', $options['shape']);

echo $circleID;                    // --> 2

Link to comment
Share on other sites

try

<?php
$a = Array(
Array(
	'Color' => Array(
		Array('id' => 1, 'value' => 'Small'),
		Array('id' => 2, 'value' => 'Medium')
	)
),
Array(
        'Size' => Array(
        	Array('id' => 3, 'value' => 'SMall'),
        	Array('id' => 4, 'value' => 'Medium'),
        	Array('id' => 5, 'value' => 'Large')
        )
),
Array(
	'Shape' => Array(
		Array('id' => 6, 'value' => 'Square'),
		Array('id' => 7, 'value' => 'Circle'),
		Array('id' => 8, 'value' => 'Triangle')
	)
)
);
$out = array();
foreach ($a[0] as $v) foreach ($v as $v1) $out[] = array($v1);
for ($i = 1; $i < count($a); $i++){
$tmp = $out;
$out = array();
foreach ($tmp as $t){
	foreach ($a[$i] as $z){
		foreach ($z as $z1){
			$t1 = $t;
			$t1[] = $z1;
			$out[] = $t1;
		}
	}
}
}
print_r($out);
?>

Link to comment
Share on other sites

Thanks so much to everyone who has helped!!!

 

Sasa, your solution retrieves the correct results but in the form of an array. What I need to accomplish is to print out the variations into a table. The first column contains the variations while the second and third columns have inputs for SKU# and QTY#.

 

I suppose I could run a foreach loop on the new array created by Sasa although it seems like it would be better to skip the extra array and jump right into printing it out... or does it need to go through that extra step?

 

I thought I knew PHP pretty well but this one is really kicking my butt. All your help is greatly appreciated :-)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.