Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从mysql获取结果,然后检查行和计数中的特定值_Php_Mysql - Fatal编程技术网

Php 从mysql获取结果,然后检查行和计数中的特定值

Php 从mysql获取结果,然后检查行和计数中的特定值,php,mysql,Php,Mysql,代理表格和领导提交表格 id_agent agent_name last_login 1 xxx 2014-11-28 12:15:47 2 abc 2014-12-06 12:51:45 3 cvb 2014-12-12 12:51:45 id_agent agent_name entry_date qa_details

代理
表格和
领导提交
表格

 id_agent   agent_name    last_login
 1          xxx          2014-11-28 12:15:47
 2          abc          2014-12-06 12:51:45
 3          cvb          2014-12-12 12:51:45

id_agent      agent_name    entry_date           qa_details                 SelDisposiotn
  1            xxx        2014-11-28 12:15:47    504:Yes|581:|515:No|      Complete Survey
  1            xxx        2014-11-28 12:15:47    504:Yes|581:Yes|515:No|   Complete Survey
  2            abc        2014-12-06 12:51:45    504:Yes|522:|515:No|      Complete Survey
  3            cvb        2014-12-12 12:51:45    504:Yes|532:Yes|515:No|   Partial Survey
我的查询是从
agent
表中获取
agent\u name
的列表<代码>计数(代理名称)来自
lead\u提交的
表格
计数(qa\u详细信息)
from
lead\u submission
table
SelDisposition
from
lead\u submission
table
在lead\u submission上左加入lead\u submission.id\u agent=agent.id\u agent
分组依据
lead\u submission.agent\u name

<?php
//DB connection goes here

$sql="
SELECT a.agent_name LoginAgent
     , COUNT(s.agent_name) AgentApplicationHit
     , COUNT(s.qa_details) QADetails
     , s.selDisposition Status 
  FROM agent a
  LEFT
  JOIN lead_submission s
    ON s.id_agent = a.id_agent 
 WHERE s.entry_date BETWEEN '2014-11-28 12:15:47' AND '2014-12-06 12:51:45' 
   AND s.SelDisposition = 'Complete Survey' 
 GROUP 
    BY s.agent_name;
";

$query=mysql_query($sql);

?>   
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<table border="1">
                <thead>
                  <tr>
                    <th>LoginAgent</th>
                    <th>AgentAppliccationHit</th>
                    <th>QA Details</th>
                    <th>Status</th>

                </tr>
  <?php while($row=mysql_fetch_assoc($query)) 
{?>

         <tr   style="color:red" >

        <td><?php echo $row['LoginAgent']; ?></td>
        <td><?php echo $row['AgentApplicationHit']; ?></td>  
        <td><?php echo $row['QADetails']; ?></td>   
        <td><?php echo $row['Status']; ?></td>          
        </tr>
                    <?php } ?>
到现在为止一切都很好

现在我有了另一个表
问题\u详细信息

qno  qshortcode   question
504   PPI         whatever
515   Test        whatever1
如您所见,这些来自
question\u details
的qno
504
515
表格位于
qa\u details
表格的
qa\u details
中,也位于我的结果中,其中我
计数(qa\u details)

现在,我如何从
lead\u提交的
504:Yes | 522:| 515:No |
中的每一行
qa\u详细信息
中获取具体的值计数,其中值是
504
515
lead\u提交的每一行中都有类似于

LoginAgent   AgentAppliccationHit      504(PPI)     515(Test)        Status
    xxx             2                    2            2            Complete Survey
    abc             1                    1            1            Complete Survey

首先,正如建议的那样,您应该规范化您的DB…但是。。。您可以使用当前设置获取计数。。。不过,如果问题、代理、客户回答和排列很多,您需要使用一些RAM

编辑查询以在结果中返回qa_详细信息,例如:

SELECT qa_details, a.agent_name LoginAgent ... etc
下面是:

 $stats_manager = array(); //or $stats_manager = [] ; depending on your php version.while($row=mysql_fetch_assoc($query)) 
        {    
                 //split up the answers into their respective questions

    foreach(explode('|', $row['qa_details']) as $Question)
        {

         $QnA = explode(':', $Question); //$QnA[0] gives you the $Question number, $QnA[1] is the answer
    $stats_manager[$row['LoginAgent']][$QnA[0]]++; //increment the number of times Agent has a question in his qa_details column
    $stats_manager[$row['LoginAgent']]['AgentApplicationHit'] = $row['AgentApplicationHit']; 
    $stats_manager[$row['LoginAgent']]['QADetails'] = $row['QADetails'];  //still necessary? I doubt.
    $stats_manager[$row['LoginAgent']]['Status'] = $row['Status']; 
    }
        }
现在有一个多维数组,包含所有代理,以及它们各自的Q和a计数 然后,您可以简单地循环数组(我省略了表头和html的其他非关键位)

foreach($stats_manager as $agent_name =>  $stats_data)
{
echo "<tr><td>$agent_name</td>";
//loop hrough all the fields
foreach($stats_data as $key => $value){
echo "<td>$value</td>";
}
echo "</tr>";
}
foreach($stats\u manager作为$agent\u name=>$stats\u data)
{
回显“$agent_name”;
//环游所有的田野
foreach($stats\u数据为$key=>$value){
回显“$value”;
}
回声“;
}
这里的关键是在整理数据之前避免重复任何内容。 此外,尽管这会起作用,但最好是正常化

未经测试。应该有效

编辑:之后意识到我在每个循环中清空了
$stats\u管理器
。修复
所以我不允许编辑修改后的代码,所以我已经删除了stats_manager变量的初始化,但是代码仍然可以使用“notice undefined variable”警告

Normalyze您的数据库。不要在lead_提交表中使用串联,qa_details字段使用另一个表并为每行存储1个数据-。-也就是说,您可以使用php(您标记了它)将每个|上的值拆分,然后打开:然后执行某些操作确定但如何获取每个代理的行
qa_详细信息
,然后//执行某些操作此行$row['qadatails'];包含504:Yes | 581:| 515:No |(示例),您可以在这里工作,根据拆分进行其他查询,etcok man。thnk you for should a light我建议的行包含2,是从查询中提取的。您可以执行sql查询以获取lead_提交的qa_详细信息
foreach($stats_manager as $agent_name =>  $stats_data)
{
echo "<tr><td>$agent_name</td>";
//loop hrough all the fields
foreach($stats_data as $key => $value){
echo "<td>$value</td>";
}
echo "</tr>";
}