dongtaogou6226 2013-06-13 02:37
浏览 28
已采纳

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

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!

  • 写回答

1条回答 默认 最新

  • duanqinbi9029 2013-06-13 03:12
    关注

    Totally new answer. index.php remains unchanged. I've reworked the code in classes.php to use named arrays. This simplifies the processing in action.php. See below for notes on that. The code here will need to be wrapped in a suitable HTML page layout.

    Note that although the code changes are extensive I have minimised the changes to the data structures. This is not necessarily the way I'd handle the data structures if I was starting from scratch.

    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[]"><br />
       <textarea cols="100" rows="20" name="entries[]"></textarea><br />
       <br />';
       $i++; //Increase the value of the count by 1
     };
    ?>
    
    <input type="submit" /><br />
    </form>
    

    action.php

    I've moved the weighted random faults code into its own function. Since the data coming from classes.php is now already in array form I've simplified the processing, and separated it from the output. Once the processing is done and the number of faults generated, a pair of nested loops generates the output by class and entrant.

    <?php
    
    function array_rand_weighted($values) {
        $r = mt_rand(1, array_sum($values));
        foreach ($values as $item => $weight) {
            if  ($r <= $weight) return $item;
            $r -= $weight;
        }
    }
    
    // generate a random value for faults and return in sorted order, ascending.
    function generateFaults($numEntries) {
    
    $faults = array();
      while ($numEntries) {
      $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);
      $faults[] = $knockdowns + $timefaults;
      $numEntries--;
    }
    //  echo nl2br(print_r($faults, true));
    sort($faults);
    return $faults;
    }
    
    $textAr = array();
    $faults = array();
    // Iterate over the entries array, extracting the names of our entrants.
    foreach ( $_POST['entries'] as $entries) {
    
        //explode all separate lines into an array
      $tempEntries = explode("
    ", $entries);
      //shuffle the results
      shuffle($tempEntries);
    
      //trim all lines contained in the array and assign to next entry in $textAr
      $textAr[] = array_filter($tempEntries, 'trim');
      //add faults
      $faults[] = generateFaults(count($tempEntries));
    }
    //loop through the lines
    //  echo nl2br(print_r($faults, true));
    
    for ($i=0; $i<count($_POST['class']); $i++) {
      echo '<div id="output">
      <b>' . $_POST['class'][$i] . '</b><br />'; 
    
      for($j = 0; $j<count($textAr[$i]); $j++) {
      echo $j + 1,". " . $textAr[$i][$j] . " (" . $faults[$i][$j] . " Faults)<br />"; 
    }
    echo '</div>';
    }
    ?>
    

    Footnote:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?