•
•
•
•
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
![]() |
•
•
Join Date: Mar 2008
Posts: 4
Reputation:
Rep Power: 0
Solved Threads: 0
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.
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.
•
•
Join Date: Jun 2008
Posts: 47
Reputation:
Rep Power: 1
Solved Threads: 2
I think you know how to retrieve data from the database. So I only post the code you are asking:
php Syntax (Toggle Plain Text)
<?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. ?>
•
•
Join Date: Mar 2008
Posts: 4
Reputation:
Rep Power: 0
Solved Threads: 0
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:
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
)
)
)
)![]() |
•
•
•
•
•
•
•
•
DaniWeb PHP Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Other Threads in the PHP Forum
- Previous Thread: Intagrate 2 scripts
- Next Thread: Can't access array in a class


Linear Mode