2013-06-13 35 views
0

我幾乎是一個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/它是一個多類隨機數發生器,所以在第一頁你把你想隨機化的類的數量,然後下一頁你輸入類名字和馬匹輸入。

最終產品我要爲會是這個樣子:

顯示跳級

  1. 一分錢(0故障| 0故障)
  2. 謝爾頓(0故障| 4個故障)
  3. 拉吉(4個故障)
  4. 倫納德(5個故障)
  5. 霍華德(8個故障)
  6. 艾米法拉福勒(8個故障)
  7. 貝爾納黛特(9個故障)

我也會喜歡它顯示第二組故障對於那些誰擁有零開始與馬,但當然一心一意。 :) 謝謝!

回答

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>'; 
} 
?> 

腳註:

相關問題