Note that there are some explanatory texts on larger screens.

plurals
  1. PODisplaying top 5 results from 10 using PHP and MYSQL
    primarykey
    data
    text
    <p>I am having difficulty trying to display the top 5 results from a query using PHP with MYSQL. At the minute all the outputs are displaying in descending order but I'm looking to display the top 5 from 10 in descending order. Below is the code I am working with:</p> <pre><code>&lt;?php //Footballer $career2sequence1 = mysql_result(mysql_query("SELECT `q1` FROM `careers` WHERE `careerid` = '2'"), 0); ?&gt; &lt;?php $career2result1 = $question1/$career2sequence1; if ($career2result1&gt;1) { $career2result1 = $career2sequence1/$question1 ?&gt; &lt;?php } ?&gt; &lt;?php $careername1 = 'Nursing '; $careername2 = 'Footballer '; $careername3 = 'Dentist '; $careername4 = 'Hairdressing '; $careername5 = 'IT '; $careername6 = 'Trade '; $careername7 = 'Doctor '; $careername8 = 'Teacher '; $careername9 = 'Sales Assistant '; $careername10 = 'Cook/Chef '; ?&gt; &lt;h4&gt; &lt;?php $nursing = array("Nursing", $result1, "% ", "$link1"); $footballer = array("Footballer", $result2, "% ", "$link2"); $dentist = array("Dentist", $result3, "% ", "$link3"); $hairdresser = array("Hairdresser", $result4, "% ", "$link4"); $IT = array("IT", $result5, "% ", "$link5"); $Trade = array("Trade", $result6, "% ", "$link6"); $Doctor = array("Doctor", $result7, "% ", "$link7"); $Teacher = array("Teacher", $result8, "% ", "$link8"); $SalesAssistant = array("Sales Assistant", $result9, "% ", "$link9"); $CookChef = array("Cook/Chef", $result10, "% ", "$link10"); $items = array($nursing, $footballer, $dentist, $hairdresser, $IT, $Trade, $Doctor, $Teacher, $SalesAssistant, $CookChef); foreach ($items as $item) { $item[1].'&lt;-'.$item[0].'&lt;br&gt;';; } function compare($a, $b) { if ($a[1] == $b[1]) { return 0; } return ($a[1] &gt; $b[1]) ? -1 : 1; } usort($items, 'compare'); foreach ($items as $item) { echo implode(' ',$item).'&lt;br&gt;'; } ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload