我試圖設計一個系統,可以根據用戶鍵入的內容診斷疾病的用戶。我使用jquery創建動態文本框,其中用戶鍵入症狀。這存儲在數組中並傳遞給php文件以運行結果查詢。對循環中的SELECT語句的結果進行分組
<?php
$symp=$_POST['fields'];
foreach ($symp as $Values) {
$qry= "SELECT D.name, group_concat(symp SEPARATOR ',') as conca, count(*) as SymptomsMatching from linkds DS JOIN symptoms S on DS.symptomId = S.id JOIN disease D on DS.diseaseId = D.id WHERE S.symp IN ('$Values') GROUP BY D.name ORDER BY count(*) DESC, D.name ";
$rep=mysqli_query($con,$qry);
foreach ($rep as $row) {
echo"<tr>
<td class='text-left'>".$row['name']."</td>
<td id='title'class='text-left'>".$row['conca']."</td>
<td class='text-left'>".$row['SymptomsMatching']."</td>
</tr>";
}
}
這是php腳本中的代碼..目前我得到的結果,但它沒有按疾病名稱分組。
Disease | Symptoms | No of Symptoms
Malaria | fever | 1
Diarrhea | fever | 1
Malaria | nausea | 1
Diarrhea | nausea | 1
Malaria | headache | 1
這個結果我得到的那一刻。但下面的表格是我所期望的:
Disease | Symptoms | No of Symptoms
Malaria | fever,nausea,headache | 3
Diarrhea | fever,nausea | 2
當我在phpMyAdmin運行相同的查詢我得到正確的表...我不知道在PHP腳本循環是否造成它..有人可以幫助我。
評論是不適用於擴展討論;這個對話已經[轉移到聊天](http://chat.stackoverflow.com/rooms/132481/discussion-on-question-by-mcprilla79-grouping-results-of-select-statement-that-i)。 –