2013-04-18 23 views
0

嗨,我想知道是否有人可以幫助我。我創建了問卷,問卷中輸出了多個結果(顯示爲%)。我可以在results.php頁面上顯示這些結果以及相應的名稱($ careername)和相應的鏈接($ link)。例如。我有5個結果,每個鏈接到相應的職業和鏈接,這些結果顯示在results.php頁面上。但是,我需要結果,職業名稱和鏈接以結果值的降序顯示。截至目前,它們以隨機順序顯示。以下是我正在使用的代碼,如果任何人有任何想法,我將不勝感激。以數組變量的值降序顯示結果

<?php 
$careername1 = 'Nursing '; 
$careername2 = 'Footballer '; 
$careername3 = 'Dentist '; 
$careername4 = 'Hairdressing '; 
$careername5 = 'IT '; 
?> 

<?php 
$link1 = '<br><a href="http://www.nhscareers.nhs.uk/explore-by-career/nursing/" target="_blank">More information on Nursing</a></br></br>'; 
$link2 = '<br><a href="#" target="_blank">More information on Footballing</a></br> </br>'; 
$link3 = '<br><a href="#" target="_blank">More information on Dentistry</a></br></br>'; 
$link4 = '<br><a href="#" target="_blank">More information on Hairdressing</a></br></br>'; 
$link5 = '<br><a href="#" target="_blank">More information on IT</a></br></br>'; 
?> 
<?php 
$nursing = array($careername1, $result1, "% ", $link1); 
$footballer = array($careername2, $result2, "% ", $link2); 
$dentist = array($careername3, $result3, "% ", $link3); 
$hairdresser = array($careername4, $result4, "% ", $link4); 
$IT = array($careername5, $result5, "% ", $link5); 
?> 
<h1>Your results are listed below:</h1> 

<?php 
$items = array("$nursing", "$footballer", "$dentist", "$hairdresser", "$IT"); 
arsort($items); 
foreach ($items as $key => $val) { 
echo "$key = $val\n"; 
} 
?> 

回答

1
// some test values 
$nursing = array("nursing", 5, "% ", ""); 
$footballer = array("footballer", 15, "% ",""); 
$dentist = array("dentist", 25, "% ", ""); 
$hairdresser = array("hairdresser", 0, "% ", ""); 
$IT = array("IT", 50, "% ", ""); 

$items = array($nursing, $footballer, $dentist, $hairdresser, $IT); 

foreach ($items as $item) { 
echo $item[1].'->'.$item[0].'<br>';; 
} 

輸出未排序

5->nursing 
15->footballer 
25->dentist 
0->hairdresser 
50->IT 

索引1上排序降

function compare($a, $b) { 
    if ($a[1] == $b[1]) { 
     return 0; 
    } 
    return ($a[1] < $b[1]) ? -1 : 1; 
} 

usort($items, 'compare'); 

foreach ($items as $item) { 
    echo $item[1].'->'.$item[0].'<br>';; 
} 

輸出排序

0->hairdresser 
5->nursing 
15->footballer 
25->dentist 
50->IT 

http://www.php.net/manual/fr/function.usort.php

+0

,也沒有喜悅:/我知道它的簡單的東西,但不能讓我的頭靠近它 –

+1

我完成了我的答案,它適用於測試值。 – RafH

+0

感謝的是,它與我的結果工作,現在我的輸出是32->足球 50-> IT 53->美髮 67->護理 77->牙醫 如何將我改變這個要像牙醫77%的超鏈接? –

0
$items = array("$nursing", "$footballer", "$dentist", "$hairdresser", "$IT"); 

注意"$nursing"$nursing

$items = array($nursing, $footballer,$dentist, $hairdresser,$IT); 

如果不工作,你可能需要編寫自己的compare功能,並把它作爲第二參數arsort函數。

欲瞭解更多信息,請閱讀http://php.net/manual/en/function.arsort.php

(*寫一個比較函數並不困難,因爲它可能看起來像)

+0

試圖消除「」並沒有什麼改變:/仍然會出現在現在想這有一個隨機的順序 –