2

Hy, can anyone help me, i have problem with my script..

if I input 4324 in input field nmber, i want the result like this :

    4324
    4342
    4234
    4243
    4432
    4423
    3424
    3442
    3244
    2434
    2443
    2344

this is my script :

<form name="a" method="POST" action="">
    <table border="1" width="100%">
    <tbody><tr>
    <td height="38" align="center"><b>Number</b>&nbsp;&nbsp;
        <input name="nmber" size="8.5" maxlength="4"  type="text" value="<?php echo $_POST['nmber']; ?>">&nbsp;&nbsp;<b>Buy</b>&nbsp;&nbsp;
        <input name="buy" size="6" type="text" value="<?php echo $_POST['buy']; ?>">&nbsp;<font color="#000000" size="2"><b>(x 1000)</b></font>&nbsp;&nbsp;
        <input name="save" style="padding:7px;" value="Submit" type="submit">
    </td>
    </tr>
    </tbody></table>
</form>

And this is my php script :

<?php
    if(isset($_POST['save']))
    {
        $dataangka=$_POST['nmber'];
        $databetnya=$_POST['buy'];
        $rupiahkali=$databetnya*1000;

        $dataangkasplit=str_split($dataangka);
        $angka1=$dataangkasplit[0];     
        $angka2=$dataangkasplit[1];     
        $angka3=$dataangkasplit[2];     
        $angka4=$dataangkasplit[3];

        $no=1;
        $n=24;
        for($i=1;$i<=$n;$i++)
        {
?>
<tr align="center">
    <td><?=$no?></td>
    <td><input name="cek[<?=$i?>]" value="1" checked="checked" type="checkbox"></td>
    <td><?php echo substr(str_shuffle("$dataangka"),0,$n); ?>
        <input size="2" name="res[<?=$i?>]" value="<?php echo substr(str_shuffle("$dataangka"),0,$angka4); ?>" type="hidden">
    </td>
    <td><?=$rupiahkali?></b>&nbsp;<input size="2" name="bet[<?=$i?>]" value="<?=$rupiahkali?>" type="hidden"></td>
</tr>
<?php
            $no++;
        }
    }
?>

I have already try with substr and str_shuffle but result not like what i want..

Please help me.. :(

Thank you so much..

2 Answers2

0

You are trying to generate all permutation of length 4 using the string 4324. The easiest way to generate all permutation (imho) is recursion. But it you can do it in iterative method too.

I would suggest you study the algorithm first and get a grip on recursion. A quick google search returned the following results

Community
  • 1
  • 1
ZubaiR
  • 607
  • 4
  • 7
0

This does what you need:

function getCombinations(array $a)
{

  switch (TRUE)
  {

    case !isset($a[1]):
      return $a;

    case !isset($a[2]):
      return array(implode($a), implode(array_reverse($a)));

    default:

      $return = [];

      foreach ($a as $k => $e)
      {

        $c = $a;

        array_splice($c, $k, 1);

        foreach (getCombinations($c) as $r)
        {
          $return[] = $e . $r;
        }

      }

      return array_unique($return);

  }

}

$s = '4324';

echo implode('<br>', getCombinations(str_split($s)));
Michael
  • 11,912
  • 6
  • 49
  • 64