0

Hi I was wondering if anyone could help me. I have created a questionnaire, there are multiple results (displayed as a %) outputted from the questionnaire. I am able to get these results displayed on a results.php page, along with the corresponding name ($careername) and corresponding link ($link). E.g. I have 5 results which each link to a corresponding career and link and these are displayed on the results.php page. However I need the result, careername and link to display in descending order of the result value. As of now they are displaying in random order. Below is the code I am working with, if anyone has any ideas I would be grateful.

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

2 Answers 2

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

output unsorted

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

sort on index 1 descending

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

output sorted

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

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

Sign up to request clarification or add additional context in comments.

6 Comments

Tried this there now and had no joy :/ I know its something simple but cant get my head around it
Thanks for that, its working with my results and now my output is 32->footballer 50->IT 53->hairdresser 67->nursing 77->dentist How would i change this to be like dentist 77% hyperlink?
in the foreach loop, $item is a 0 based indexed array (like $dentist, etc.). -> echo $item[0].' '.$item[1].' '.$item[2].' '.$item[3].'<br>'; The display order is the same as the index order so you can also use echo implode(' ', $item).'<br>';
Hmm, im not really sure what you mean, could you explain it a bit further? Fairly new to this PHP stuff. Thanks for your time
$items contains the 5 arrays ($dentist etc) now sorted by result value. In foreach ($items as $item) { ... }, $item represents the current "result" array (ie $dentist, $IT, etc..). For example, to display the link value of the current item : echo $item[3]; as link is the 4th value in each original array.
|
0
$items = array("$nursing", "$footballer", "$dentist", "$hairdresser", "$IT");

Notice that "$nursing" should be $nursing , etc.

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

If that doesn't work, you may need to write a compare function of your own and use it as the second argument of the arsort function.

For further information , read http://php.net/manual/en/function.arsort.php

(*Writing a comparing function is not as difficult as it might seems like)

1 Comment

Tried removing the "" and nothing has changed :/ Still appearing in a random order

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.