homepage Welcome to WebmasterWorld Guest from 54.237.95.6
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Website
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
repeat for loop dynamically
repeat for loop to create permutations
nil111




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

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 = 2
for($i=0;$i<sizeof($stones_arr);$i++){ // for all 12 stones
for($n=0;$n<sizeof($stones_arr);$n++){ // for all 12 stones
print $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 = 3
for($i=0;$i<sizeof($stones_arr);$i++){ // for all 12 stones
for($n=0;$n<sizeof($stones_arr);$n++){ // for all 12 stones
for($m=0;$m<sizeof($stones_arr);$m++){ // for all 12 stones

print $permutation = $stones_arr[$i].','.$stones_arr[$n].','.$stones_arr[$m];
print '<br />';
}
}
}

 

Matthew1980




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

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




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

thanks in advance

nil111




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

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




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

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




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

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:


<?PHP

function 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:


<?PHP

function 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




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

Thanks all for the great help!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved