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:

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

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题