从两个不同的阵列创建和显示信息?

I'm pretty much a PHP beginner, although I can copy and paste like no one's business and generally I'm able to pick it apart and figure out what's going on :) This is my first post here, but I've gotten lots of help from other answers, so thanks for all the past and hopefully future help!

Basically what I'm trying to replicate here is an equestrian show jumping event, in which each knockdown of a pole equals four faults, and time faults are given for going over the time allowed. All horses who jump clear, that is, with zero faults, go on to a jump-off round, where they may or may not incur faults.

My current issue is using for and foreach loops to advance two different arrays. The problem I'm having is that if I get one array working, the other stops. One of the arrays needs to be shuffled, the other needs to be sorted in numerical order. Basically it's a randomizer which will take a series of horses and give them placings and then assign a weight random number of faults (it's for a horse game, nerdy, I know). Here is the code I'm working with:

index.php

<form action="classes.php" method="post">
    How many classes do you wish to randomize?<br />
    <input type="text" name="number"><br />
    <input type="submit" /><br />
</form>

classes.php

<form action="action.php" method="post">
<?php
$number = $_POST['number']; //This is the desired value of Looping
echo '<input type="hidden" value="' . $number . '" name="number">';
$i = 1; //First we set the count to be zero
while ($i<=$number) {
    echo '

    Class: <input type="text" name="class' . $i . '"><br />
    <textarea cols="100" rows="20" name="entries' . $i . '"></textarea><br />
    <br />';
    $i++; //Increase the value of the count by 1
};
?>
<input type="submit" /><br />
</form>

action.php

$number = $_POST['number']; //This is the desired value of Looping

function array_rand_weighted($values) {
    $r = mt_rand(1, array_sum($values));
    foreach ($values as $item => $weight) {
        if  ($r <= $weight) return $item;
        $r -= $weight;
    }
}

for ($i=1; $i<=$number; $i++) {
    $class[$i] = $_POST['class'.$i];

    //trim off excess whitespace off the whole
    $text[$i] = trim($_POST['entries'.$i]);

    //explode all separate lines into an array
    $textAr[$i] = explode("
", $text[$i]);

    //trim all lines contained in the array.
    $textAr[$i] = array_filter($textAr[$i], 'trim');

    //shuffle the results
    shuffle($textAr[$i]);

    //add faults
    //loop through the lines
    echo '<div id="output">
    [b]' . $class[$i] . '[/b]<br />'; 
    foreach($textAr[$i] as $key[$i]=>$line[$i]){
        $knockdowns = array( 0 => 20, 1 => 25, 2 => 30, 3 => 10, 4 => 8, 5 => 7); // knockdowns
        $knockdowns = array_rand_weighted($knockdowns)*4;
        $timefaults = array( 0 => 75, 1 => 10, 2 => 10, 3 => 5); // time faults
        $timefaults = array_rand_weighted($timefaults);

        $faultsadded = $knockdowns + $timefaults;

        $faults[$i] = $faultsadded;
    }

    asort($faults);

    foreach($textAr[$i] as $key[$i]=>$line[$i]){
        echo $key + 1,". " . $line . " (" . $faults[$i] . " Faults)<br />"; 
    }
    echo '</div>';
}

At present, this code is producing the randomized results I need, but not the faults. When I am able to get it to produce the full set of faults, the list of random horses stops functioning. I have never been able to get the faults to sort in order of value (0-20+). I found another answer using array_combine, and thought maybe to use this, but I need the key (I think- maybe I don't really?).

If you want to see it in action, here is the link: http://www.eecreates.com/randomizer/dhjc%20randomizer/ it's a multi-class randomizer, so on the first page you put the number of classes you want to randomize, then the next page you input the class name and horses entered.

The final product I'm going for would look something like this:

Show Jumping Class

  1. penny (0 Faults | 0 Faults)
  2. sheldon (0 Faults | 4 Faults)
  3. raj (4 Faults)
  4. leonard (5 Faults)
  5. howard (8 Faults)
  6. amy farrah fowler (8 Faults)
  7. bernadette (9 Faults)

I'd also like for it to show a second set of faults for those horses who have zero to begin with, but of course one thing at a time. :) Thank you!

查看全部
dongtaogou6226
dongtaogou6226
2013/06/13 02:37
  • php
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

1个回复