PHP

Moderators: None (Apply to moderate this forum)
Number of threads: 1847
Number of posts: 5013

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
7 number combiantions from an array Posted by nrkamlesh on 21 May 2010 at 6:17 AM
Please help me with this
<html>
: <head>
: <script>
: n = 20
: r = 5
:
: var combination = new Array(r);
:
: for(i=0; i<r; i++)
: combination[i] = 0;
:
: function getNextCombination(n, r) {
: if(combination[0]==0) { // First time here, make the initial combination
: for(i=0; i<r; i++)
: combination[i] = i+1;
: }
: else {
: // n+index-r is max value (index : 1 -> r)
: index = r;
: while(combination[index-1]==n+index-r && index>0)
: index--;
: if(index==0)
: return false;
: combination[index-1]++;
: for(index++; index<=r; index++)
: combination[index-1] = combination[index-2]+1;
: }
: return true;
: }
: function showNext() {
: val = getNextCombination(n, r);
: if(!val) // No more combinations found
: return val;
:
: txt = "";
: for(i in combination)
: txt+=combination[i]+", ";
:
: // Show the combination somewhere
: window.status = txt;
:
:
: return val;
: }
: function init() {
: for(p=0; p<50000 && showNext(); p++); // Shows the first 50000 combinations or as many as there are
: }
: </script>
: </head>
: <body onload="init()">
: </body>
: </html>


i want the same result in in php and my condition is i want to pass different king of numbers in . now this combinations goes sequence number like 1,2,3,4,5,6 and i want a combinations of different numbers from a array like i need a result of array(1,12,32,24,14) and r value is default 7 . can any one provide me a solution for this in php.

Thank in advance who ever helps me .
Report
Re: 7 number combiantions from an array Posted by CJNimes on 6 Jun 2010 at 6:26 PM
Well, replace "<script>" for "<?php", "</script>" for "?>", delete the "var" word and add a "$" sign on each variable and you will get your php code ;)
Report
Re: 7 number combiantions from an array Posted by nrkamlesh on 7 Jun 2010 at 9:40 PM
i did this before and some of the functions are not running like for(i in var).
Report
Re: 7 number combiantions from an array Posted by CJNimes on 8 Jun 2010 at 4:59 PM
I missed a few more changes you have to do:

$combination = array();
...
...

function getNextCombination
global $combination; <------------ add this as function first line
...
...


function showNext
global $combination, $n, $r; <-------- add this line
...
...


Replace this:
for(i in combination)
txt+=combination[i]+", ";
window.status = txt;

With this:
foreach ($combination as $c)
$txt.=$c.", ";
echo $txt."<br>";

Delete all html code, and the last line shoud be:
init();


I thinks now you can get it!
Report
Re: 7 number combiantions from an array Posted by nrkamlesh on 9 Jun 2010 at 9:51 PM
Thank you very much CJNimes. I got the result Hurrey..



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.