Jump to content

Archived

This topic is now archived and is closed to further replies.

chriscloyd

pairs

Recommended Posts

okay say i got 40 teams i wanna learn how to write the script where it randomly pairs two teams up to eachother 8 times but 8 dif teams not the same team as it paired up to b4 can someone help me? and then i want it to post into a table or file

Share this post


Link to post
Share on other sites
[!--quoteo(post=373420:date=May 13 2006, 12:13 AM:name=chriscloyd)--][div class=\'quotetop\']QUOTE(chriscloyd @ May 13 2006, 12:13 AM) [snapback]373420[/snapback][/div][div class=\'quotemain\'][!--quotec--]
okay say i got 40 teams i wanna learn how to write the script where it randomly pairs two teams up to eachother 8 times but 8 dif teams not the same team as it paired up to b4 can someone help me? and then i want it to post into a table or file
[/quote]

This will get you two random id's from a table:
[b]SELECT team_id FROM table1 WHERE ORDER BY RAND() LIMIT 2;[/b]

The rest of your question I don't understand. Try using proper centences.

Share this post


Link to post
Share on other sites
while i do understand your question, please note that we are not here to write your code for you. you need to make an effort to write your own code, and we will help you fix bugs....

HOWEVER... since I was extremely bored, I decided to do it anyways. Merry Christmas.

[code]
<?php

//generic array holding 40 values
$list = array('one','two','three','four','five',
              'six','seven','eight','nine','ten',
                            'eleven','twelve','thirteen','fourteen','fifteen',
                            'sixteen','seventeen','eighteen','nineteen','twenty',
                            'twentyone','twentytwo','twentythree','twentyfour','twentyfive',
                            'twentysix','twentyseven','twentyeight','twentynine','thirty',
                            'thirtyone','thirtytwo','thirtythree','thirtyfour','thirtyfive',
                            'thirtysix','thirtyseven','thirtyeight','thirtynine','fourty');

$max = 39; //starting amount of items (0-39 = 40)                            

// since you have 40 items you want to pair off, we will
// do the 'pair-off 20 times
for ($count = 0;$count <20; $count++) {
   //pick 2 random items from the list
     //and remove them from the list
     for ($number = 0; $number < 2; $number++) {
      $randompick = rand(0,$max);
        $numberpair[$number] = $list[$randompick];
        unset($list[$randompick]);
        $list = array_values($list);
        $max--;
   }
     //make a list of the items paired up
     $pairups[] = array('first' => $numberpair[0],
                        'second' => $numberpair[1]);
}

//generic printout of the list
$listofpairs = "<table border='1'><tr>";
$listofpairs.= "<td><b>First</b></td><td><b>Second</b></td></tr>";
for ($pairs = 0; $pairs < 20; $pairs++) {
   $listofpairs.="<tr>";
     foreach ($pairups[$pairs] as $val) {
      $listofpairs.="<td>" . $val . "</td>";
     }
     $listofpairs.="</tr>";
}
$listofpairs.="</table>";

echo $listofpairs;

?>
[/code]

if you have your items in a database then you would select them the way you normally would and replace the $list array with an array of the values pulled from the database.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.