Hi,
I am writing a script for a game that needs players to randomly be assigned a target (another player) in a loop so that each player is matched with someone other than who has them. See below:
Andy matched with Jan
Jan matched with Alex
Alex matched with Sue
Sue matched with Andy
My code retrieves name from the database in the order they sign up to play. I need to randomize the pairings for fairness.
Code: <?
$targets = array("Andy","Jan","Alex","Sue"); //names from DB in order of signup
?>
I make a copy of the array, shuffle it and check to make sure that two players are not paired with each other.
Code: <?
$targets = array("Andy","Jan","Alex","Sue"); //names from DB in order of signup
$targets_shuffled = $targets; //copy array
shuffle($targets_shuffled); //shuffle targets
?>
I am having trouble figuring out how to check for duplicate pairings and then reshuffling until it works. Originally I had looped through the arrays to check each pairing:
Code: <?
if($targets[$i] == $targets_shuffled[$i]){
echo "same!";
}
?>
Then I realized when one pair was matched incorrectly the whole pairing would need to be reshuffled. I tried to flag it, break out, and reshuffle, but could never get anything that worked.
Any ideas?
-Blake
Code currently:
Code: <?
shuffle($targets_shuffled);
echo "<br /><br />";
$retry = true;
$tries = 1;
while($retry == true){
echo "try: $tries<br />";
$retry = false;
$i = 0;
while($i < count($targets)){
if($targets[$i] == $targets_shuffled[$i]){
echo "found match!<br />";
$retry = true;
echo "breaking<br />";
break;
}
$i++;
}
$tries++;
}
?>
Is it possible to view php source code??
Hi AllI was just wondering if it's possible to view a websites php source code? I know that php is a server side scripting language and that it is processed at the server and the results are sent
PHP & MS Access - Line Break
Hey Guys!I'm pulling records from an MS Access database and listing them using PHP and I'm running into an error that I can't seem to figure out.Please see the code below:Code: if
what business processes?
Hi,
Something like an INI editor or a DelimitedText-Editor
Hi all,Am very, very, very new to PHP and not sure if I should be posting this to a Javascript or PHP forum so I posed the question on both FORUM.Basically, I am looking for something like an INI
having a small php error tha deals with the 'foreach'
Last night i was working on a script for a cart and it seemed to working good, until this morning. For some reason i'ts giving me the following error:Warning: Invalid argument supplied for foreach()
Placing and array within an array then sorting it!
I have a page that runs two large mysql queries and saves the results into arrays, in php I then perform a simple multiplication between the two arrays and the results is displayed in a dynamic html
CRM?
I think that's what they're called. Customer Management System? Anyway. Is there any good free ones I could give a try? I was going to script one but what I want it for was confusing me when I was
storing video files into mysql in php
hi i have my video files in my folder ,i have to store the path of the videos into db and the play it in the player from the database.thanking in advance
PHP code for a proxy
I was wondering which function I could use to create an online proxy tool. I was thinking cURL would work, but it just doesn't seem appropriate, for cURL is mostly used form getting data. Any ideas?
SAP Logon Failed
I tried to login to SAP through MMC.