Welcome to WebmasterWorld Guest from 54.224.13.210

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

# repeat for loop dynamically

## repeat for loop to create permutations

#### nil111

9:28 am on Apr 22, 2011 (gmt 0)

#### Junior Member

joined:July 8, 2008
posts: 80

I use the a code similar to following to create permutations.

here,

the no of stones are dynamically selected by user.
the stone ids are in the db.

depending on the no of stones selected, using the stone ids in db permutations should be generated.

my question is how can i do this(create a function ) for n no of stones?

``\$stones_arr = array(1,2,3,4,5,6,8,9,10,11,12,13); //12 altogether//for loop repeats 2 times coz max stones = 2for(\$i=0;\$i<sizeof(\$stones_arr);\$i++){ // for all 12 stonesfor(\$n=0;\$n<sizeof(\$stones_arr);\$n++){ // for all 12 stonesprint \$permutation = \$stones_arr[\$i].','.\$stones_arr[\$n];print '<br />';}}\$stones_arr = array(1,2,3,4,5,6,8,9,10,11,12,13); //12 altogether//for loop repeats 3 times coz max stones = 3for(\$i=0;\$i<sizeof(\$stones_arr);\$i++){ // for all 12 stonesfor(\$n=0;\$n<sizeof(\$stones_arr);\$n++){ // for all 12 stonesfor(\$m=0;\$m<sizeof(\$stones_arr);\$m++){ // for all 12 stonesprint \$permutation = \$stones_arr[\$i].','.\$stones_arr[\$n].','.\$stones_arr[\$m];print '<br />';}}}``

#### Matthew1980

9:37 am on Apr 22, 2011 (gmt 0)

#### Senior Member

joined:Feb 22, 2009
posts:1396

Hi there nil111,

There are ways to improve this code, but I can't see what your trying to achieve otherwise.

Firstly, instead of defining an array 0->12 you can just use range(0,12) to have the same effect.

Secondly, each iteration of this loop is consuming memory as you calling sizeof() on each iteration. The way to make this slightly more efficient is to assign sizeof() to a variable, and then just reference that variable name in the for loop.

Other than that, I can't see what your trying to achieve.

Cheers,
MRb

#### nil111

9:43 am on Apr 22, 2011 (gmt 0)

#### Junior Member

joined:July 8, 2008
posts: 80

#### nil111

9:46 am on Apr 22, 2011 (gmt 0)

#### Junior Member

joined:July 8, 2008
posts: 80

thanks Mathew. i'm trying to get all the permutations for a given set of stone ids depending on the no of stones per jewellery item.

So as ridiculous this may seem, it is required

#### rocknbil

5:52 pm on Apr 22, 2011 (gmt 0)

#### Senior Member

joined:Nov 28, 2004
posts:7999

trying to get all the permutations for a given set of stone ids depending on the no of stones per jewellery item

More info is required, what's the nature of your database? For example, if you have a base table

id|item_id|item_name|description

and an "options" table

id|option_id|master_item_id|option_name|option_value

you should be able to do a select on the main table and left join on the options table to get all the permutations for a given set of stone(s) - and order/group them in any way needed. This approach would eliminate the need for brain-burning puzzles in PHP.

#### quixotic

3:50 pm on Apr 23, 2011 (gmt 0)

#### New User

joined:June 21, 2005
posts: 26

There are a lot powerful tools in MySQL you can leverage... but in case you really did need a php solution, here's a rough sketch for you:

``<?PHPfunction permutation (\$n_stones, \$size) { // Initialize the array for(\$i=0;\$i<\$size;\$i++){ \$numbers[]=1; } \$n = \$size-2; \$str = ''; // Loop over each position in the array  while(\$numbers[0] <= \$n_stones) { while( \$numbers[\$size-1] <= \$n_stones) { \$str .= implode(',',\$numbers).'<br/>'; ++\$numbers[\$size-1]; } if(\$numbers[0] >= \$n_stones){break;} \$numbers[\$size-1] = 1; while(\$numbers[0] < \$n_stones) { if(\$numbers[\$n] < \$n_stones) { ++\$numbers[\$n]; break; } else { --\$n; } } } return \$str;}\$stones = 12;\$max = 3;for(\$i = 2; \$i<=\$max; ++\$i) { echo permutation(\$stones,\$i);}?>``

My while loops aren't optimized (using breaks instead of proper exit conditions etc.), but this should give you what you want.

I assume you don't really want 1,2,3,4... but a series of id's. In which case, instead of imploding the array, and returning a string, return an array of arrays, and map it to your id's, like so:

``<?PHPfunction permutation (\$n_stones, \$size) { // Initialize the array for(\$i=0;\$i<\$size;\$i++){ \$numbers[]=1; } \$n = \$size-2; \$id_array = array(); // Loop over each position in the array  while(\$numbers[0] <= \$n_stones) { while( \$numbers[\$size-1] <= \$n_stones) { \$id_array[] = \$numbers; ++\$numbers[\$size-1]; } if(\$numbers[0] >= \$n_stones){break;} \$numbers[\$size-1] = 1; while(\$numbers[0] < \$n_stones) { if(\$numbers[\$n] < \$n_stones) { ++\$numbers[\$n]; break; } else { --\$n; } } } return \$id_array;}\$stones = 5;\$max = 3;\$ids = array ('0','a','b','c','d','e');\$str = '';for(\$i = 2; \$i<=\$max; ++\$i) { \$id_set = permutation(\$stones,\$i); foreach(\$id_set as \$perm) { foreach(\$perm as \$id) { \$str .= \$ids[\$id].','; }  \$str = substr(\$str,0,-1) . "<br/>"; }}echo \$str;?>``

Again, this is pretty sloppy code. I'm sure there's a more elegant solution at the very least, you should optimize the loops. Also, in this case, it was easier to copy/paste the original code and simply put a place holder for the \$ids[0] rather than adjust the indices, or set \$stones based on the \$ids size, etc.

#### nil111

2:47 am on Apr 25, 2011 (gmt 0)

#### Junior Member

joined:July 8, 2008
posts: 80

Thanks all for the great help!