![](/img/trans.png)
[英]counting the blog ids and displaying the results in descending order in mysql codeigniter
[英]Displaying results in descending order of value within array variable
嗨,我想知道是否有人可以幫助我。 我創建了一個調查表,該調查表輸出了多個結果(顯示為%)。 我能夠將這些結果以及相應的名稱($ careername)和相應的鏈接($ link)顯示在results.php頁面上。 例如,我有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";
}
?>
// 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
$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
(*編寫比較函數並不像看起來那樣困難)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.