2013-04-22 120 views
0

我已經通過PHP創建問卷。有十個問題,每個問題都涉及特定的職業。我的代碼與用於每個職業的每個問題的用戶輸入相匹配。在調查問卷結束時,結果與用戶對10個職業的兼容性相匹配,以百分比表示匹配。我想顯示前5個結果,但我不確定要使用哪些代碼以及在哪裏使用它。任何幫助,將不勝感激。代碼如下:顯示前5條結果PHP MySQL

<?php 
//Footballer 
$career2sequence1 = mysql_result(mysql_query("SELECT `q1` FROM `careers` WHERE `careerid` = '2'"), 0); 
?> 

<?php 
$career2result1 = $question1/$career2sequence1; 
if ($career2result1>1) { 
$career2result1 = $career2sequence1/$question1 
?> 

<?php 
} 
?> 

<?php 
$careername1 = 'Nursing '; 
$careername2 = 'Footballer '; 
$careername3 = 'Dentist '; 
$careername4 = 'Hairdressing '; 
$careername5 = 'IT '; 
$careername6 = 'Trade '; 
$careername7 = 'Doctor '; 
$careername8 = 'Teacher '; 
$careername9 = 'Sales Assistant '; 
$careername10 = 'Cook/Chef '; 
?> 

<h4> 

<?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].'<-'.$item[0].'<br>';; 
} 

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 implode(' ',$item).'<br>'; 
} 
?> 

回答

0

直接在LIMIT查詢中使用它。

SELECT `q1` FROM `careers` WHERE `careerid` = '2' LIMIT 0,5 
+0

前5名是'LIMIT 0,5'(偏移,每頁) – bitWorking 2013-04-22 23:16:38

+0

但是有很多個人的查詢,作爲有職業1序列1一直到職業生涯1序列10和有10個事業。然後對每個問題的用戶結果進行測試 – KerriPower 2013-04-22 23:17:26

+0

@redreggae - 當然是!謝謝! – bestprogrammerintheworld 2013-04-22 23:18:38