douji6735 2017-01-05 20:33
浏览 113
已采纳

对循环中的SELECT语句的结果进行分组

I'm trying to design a system that can diagnose a user of a disease depending on what the user keys in. I used jquery to create dynamic textboxes in which the user keys in the symptoms.This is stored in array and passed to the php file to run a query for results.

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

That is the code in the php script..At the moment i'm get results but its not grouping by the disease name.

Disease     |       Symptoms        | No of Symptoms
 Malaria    |     fever             |    1
Diarrhea    |     fever             |    1
Malaria     |     nausea            |    1
Diarrhea    |     nausea            |    1
Malaria     |     headache          |    1

This the result am getting at the moment.. But the table below is what i expect:

Disease     |            Symptoms                   | No of Symptoms
 Malaria    |     fever,nausea,headache             |    3
Diarrhea    |     fever,nausea                      |    2

When i run the same query in phpmyadmin i get the right table... i dont know if the loop in the php script is causing it.. Can someone please help me out.

  • 写回答

1条回答 默认 最新

  • douzhang8033 2017-01-06 00:40
    关注

    The array_walk function will help you format the $values correctly, so each value is enclosed in "single quotes" and MYSQL WHERE IN () will process correctly.

    <?php
    $symp=$_POST['fields'];
    
    array_walk($symp, function(&$v,$k){
          $v = "'$v'";
     });
    
    $values=implode(",",$symp);
    
    $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 SymptomsMatching DESC, D.name ASC";
    
    $rep=mysqli_query($con,$qry);
    if($rep){
        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>";
        }
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了