User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 426,592 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,667 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 341 | Replies: 2 | Solved
Reply
Join Date: Mar 2008
Posts: 4
Reputation: firthusa is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
firthusa firthusa is offline Offline
Newbie Poster

Product Option Combinations/Variations Grid

  #1  
Jun 23rd, 2008
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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2008
Posts: 47
Reputation: kvdd is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
kvdd kvdd is offline Offline
Light Poster

Re: Product Option Combinations/Variations Grid

  #2  
Jun 23rd, 2008
I think you know how to retrieve data from the database. So I only post the code you are asking:

  1. <?php
  2. // Put here the database connection
  3.  
  4. $qOptions1 = mysql_query("SELECT * FROM Options1");
  5.  
  6. foreach (mysql_fetch_row($qOptions1) as $val){
  7. $aOptions1[] = $val; // generate the array for you with the options
  8. }
  9. // Do also with the options2 and 3
  10.  
  11. $count1 = count($aOptions1);
  12. $count2 = count($aOptions2);
  13. $count3 = ..
  14.  
  15. // You could this also do with a foreach..
  16. for ($a=0; $a < $count1; $a++){
  17. for ($b=0; $b < $count2; $b++){
  18. for ($c=0; $c < $count3; $c++){.
  19. $options = $aOptions1[$a] . $aOptions2[$b] . $aOptions3[$c];
  20. }
  21. }
  22. }
  23. // The answer you asked, should be something like above. Its a little rumble, and garbage what I typed, but gives you THE idea.
  24. ?>
Reply With Quote  
Join Date: Mar 2008
Posts: 4
Reputation: firthusa is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
firthusa firthusa is offline Offline
Newbie Poster

Re: Product Option Combinations/Variations Grid

  #3  
Jun 23rd, 2008
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
                        )

                )

        )
)
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the PHP Forum

All times are GMT -4. The time now is 10:47 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC