help newbie web php code about random shuffile array

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    help newbie web php code about random shuffile array

    hi.. i have array receive data from multi checkbox.
    $arrayplayer[0] = 10 $arrayplayer[1] = 1 $arrayplayer[2] = 17 $arrayplayer[3] = 9 i want to random shuffile data like bottom. so i try to use function shuffle but it repeat data
    $arrayplayer[0]=17 $arrayplayer[1]=9 $arrayplayer[2]=10 $arrayplayer[3]=1

    #2
    this is my total code . u can see at // the probem and //end problme


    <html>
    <head>
    <meta http-equiv="Content-Language" content="th">
    <meta http-equiv="content-Type" content="text/html; charset=window-874">
    <meta http-equiv="content-Type" content="text/html; charset=tis-620">
    <title>Select Player</title>
    </head>
    <body>
    <?php

    function shuffle_assoc(&$array) {
    $keys = array_keys($array);
    shuffle($keys);
    foreach($keys as $key) {
    $new[$key] = $array[$key];
    }
    $array = $new;
    return true;
    }

    $id = $_GET["thisid"];
    if(isset($_POST['submit']))
    {
    $amountteam = $_POST['perteam'];;
    //echo "amount team=>".$amountteam;
    echo "<h2>welcome to soccer random team</h2>";
    $checked_arr = $_POST['chkplayer'];
    $count = count($checked_arr);
    $team1 = floor($count/$amountteam);
    $team2 = $count%$amountteam;
    echo "à¸ˆà¸³à¸™à¸§à¸™à¸„à¸™à¸—à¸µà¹ˆà¸¡à¸²à¹€à¸•à¸°à¸šà ¸­à¸¥".$count."<br>";
    echo "แบ่งทีมได้ = ".$team1."ทีม";
    echo "เศษ = ".$team2."คน<br>";
    //put checkbox into array1 and array 2 is tricker"
    $index = 0;
    $indey = 0;
    // the problem
    foreach($_POST['chkplayer'] as $selected)
    {
    //echo $selected."</br>";
    $arrayplayer[$index] = $selected;
    echo "BEFORE SHUFFLE----arry[".$index."]==>".$arrayplayer[$index]. "<br>" ;
    shuffle($arrayplayer);
    echo "AFTER SHUFFLE----arry[".$index."]==>".$arrayplayer[$index]. "<br>" ;
    $index = $index +1 ;
    }
    //end problem

    $index = 0;
    $indey = 0;
    $total = 0;
    //seperate team
    echo"-------------TEAM -------------<br>";
    for($index =0 ; $index<$team1 ; $index++ ) // 2 team
    {

    while(($indey<$amountteam)&&($total<$count)):
    $finalteam[$index][$indey] = $arrayplayer[$total];
    //$finalteam[$index][$indey] = $rand_array[$total];
    echo "finalteam[".$index."][".$indey."]==>".$finalteam[$index][$indey]. "<br>" ;
    $total++;
    $indey++;
    endwhile;
    echo "================= <br>";
    $indey =0 ;
    }



    }else{
    ?>
    <form name="test" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
    <input type="checkbox" name="chkplayer[]" value="หง่าว">หง่าว<br>
    <input type="checkbox" name="chkplayer[]" value="เต่า">เต่า<br>
    <input type="checkbox" name="chkplayer[]" value="วิน">วิน<br>
    <input type="checkbox" name="chkplayer[]" value="ไว">ไว<br>
    <input type="checkbox" name="chkplayer[]" value="เก้อ">เก้อ<br>
    <input type="checkbox" name="chkplayer[]" value="เสริม">เสริม<br>
    <input type="checkbox" name="chkplayer[]" value="กอลฟ">กอลฟ<br>
    <input type="checkbox" name="chkplayer[]" value="แบค">แบค<br>
    <input type="checkbox" name="chkplayer[]" value="บัง">บัง<br>
    <input type="checkbox" name="chkplayer[]" value="หมิน">หมิน<br>
    <input type="checkbox" name="chkplayer[]" value="เอก">เอก<br>
    <input type="checkbox" name="chkplayer[]" value="aaa">aaa<br>
    <input type="checkbox" name="chkplayer[]" value="bbb">bbb<br>
    <input type="checkbox" name="chkplayer[]" value="ccc">ccc<br>
    <input type="checkbox" name="chkplayer[]" value="ddd">ddd<br>
    <input type="checkbox" name="chkplayer[]" value="eee">eee<br>
    <input type="checkbox" name="chkplayer[]" value="fff">fff<br>
    <input type="checkbox" name="chkplayer[]" value="ggg">ggg<br><br>
    <select name = "perteam">
    <option value = "5">5คนต่อทีม</option>
    <option value = "6">6คนต่อทีม</option>
    <option value = "7">7คนต่อทีม</option>
    </select>
    <br><br>

    <input type="submit" name="submit" value="ส่งข้อมูล"></td></tr>
    <input name="clear" type="reset" value="เคลียร์">
    </form>
    <?php
    }
    ?>

    Comment


      #3
      you are shuffling the array many times in foreach loop so you will get weird array keys
      move the shuffle above the foreach loop

      Comment

      Working...
      X