我几乎是一个PHP初学者,虽然我可以复制和粘贴,就像没有人的业务一样,我通常能够分开并弄清楚发生了什么:)这是我在这里的第一篇文章,但我'得到了很多其他答案的帮助,感谢所有过去和未来的帮助!
基本上我在这里要复制的是一个马术表演跳跃事件,其中一个杆的每次击倒等于四个故障,并且在超过允许的时间内给出时间故障。所有跳跃清晰的马匹,即没有任何缺陷的马匹,都会进入跳跃轮,在那里他们可能会或可能不会出现故障。
我目前的问题是使用for和foreach循环来推进两个不同的数组。我遇到的问题是,如果我让一个阵列工作,另一个停止。其中一个数组需要洗牌,另一个需要按数字顺序排序。基本上它是一个随机发生器,它将采取一系列马匹并给它们放置,然后分配一个随机数量的重量(这是一场马匹比赛,我知道,是书呆子)。这是我正在使用的代码:
的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("\n", $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>';
}
目前,这段代码正在产生我需要的随机结果,但不是错误。当我能够让它产生全套故障时,随机马列表就会停止运行。我从来没有能够按照值(0-20 +)的顺序排序故障。我找到了另一个使用array_combine的答案,并认为可能会使用它,但我需要密钥(我想 - 也许我不是真的?)。
如果你想看到它的实际效果,这里是链接:http://www.eecreates.com/randomizer/dhjc%20randomizer/它是一个多类随机化器,所以在第一页上你放了你想要随机化的类的数量,那么下一页你输入班级名称和输入的马匹。
我要去的最终产品看起来像这样:
显示跳跃课程
我也希望它为那些零开始的马匹展示第二组故障,但当然一件事情。 :)谢谢!
答案 0 :(得分:0)
完全新答案。 index.php保持不变。我已经重新编写了classes.php中的代码来使用命名数组。这简化了action.php中的处理。请参阅下面的说明。这里的代码需要包装在合适的HTML页面布局中。
请注意,虽然代码更改很广泛但我已将对数据结构的更改最小化。如果我从头开始,这不一定是我处理数据结构的方式。
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的
我已将加权随机故障代码移动到自己的函数中。由于来自classes.php的数据现在已经是数组形式,因此我简化了处理,并将其与输出分开。处理完成后,生成的错误数量,一对嵌套循环按类和参与者生成输出。
<?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("\n", $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>';
}
?>
脚注: