help newbie web php code about random shuffile array

Started on the old forum in December 2014 · view the original in the archive · old links redirect here

2 replies · 114 views · started 11 years ago · 1 min read

2 participants
kimnue Newbie · 2 posts #1

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

kimnue Newbie · 2 posts #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." "; echo "แบ่งทีมได้ = ".$team1."ทีม"; echo "เศษ = ".$team2."คน "; //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]. " " ; shuffle($arrayplayer); echo "AFTER SHUFFLE----arry[".$index."]==>".$arrayplayer[$index]. " " ; $index = $index +1 ; } //end problem

$index = 0; 
$indey = 0; 
$total = 0;
//seperate team        
echo"-------------TEAM -------------

"; 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]. "

" ; $total++; $indey++; endwhile; echo "================= "; $indey =0 ; }

}else{ ?>

<form name="test" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post"> <input type="checkbox" name="chkplayer[]" value="หง่าว">หง่าว

<input type="checkbox" name="chkplayer[]" value="เต่า">เต่า

<input type="checkbox" name="chkplayer[]" value="วิน">วิน

<input type="checkbox" name="chkplayer[]" value="ไว">ไว

<input type="checkbox" name="chkplayer[]" value="เก้อ">เก้อ

<input type="checkbox" name="chkplayer[]" value="เสริม">เสริม

<input type="checkbox" name="chkplayer[]" value="กอลฟ">กอลฟ

<input type="checkbox" name="chkplayer[]" value="แบค">แบค

<input type="checkbox" name="chkplayer[]" value="บัง">บัง

<input type="checkbox" name="chkplayer[]" value="หมิน">หมิน

<input type="checkbox" name="chkplayer[]" value="เอก">เอก

<input type="checkbox" name="chkplayer[]" value="aaa">aaa

<input type="checkbox" name="chkplayer[]" value="bbb">bbb

<input type="checkbox" name="chkplayer[]" value="ccc">ccc

<input type="checkbox" name="chkplayer[]" value="ddd">ddd

<input type="checkbox" name="chkplayer[]" value="eee">eee

<input type="checkbox" name="chkplayer[]" value="fff">fff

<input type="checkbox" name="chkplayer[]" value="ggg">ggg

<select name = "perteam"> <option value = "5">5คนต่อทีม</option> <option value = "6">6คนต่อทีม</option> <option value = "7">7คนต่อทีม</option> </select>

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

</form> <?php } ?>
something else Fellow · 2,296 posts #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

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close