Php排行榜问题

时间:2016-10-12 09:44:40

标签: php mysql

好的,所以客户发布了对经理的反馈,他们得到了总分。我希望每个页面每个月要做的是创建经理收到的提交数量的COUNT,其总分为收到的反馈总数,

此刻的代码计算数据库中出现的$ manager名称的数量,但是它只计算一次管理器名称!我有两个针对经理A的反馈,但是计数仅显示一个例子。

创建的总分是所有经理的总分,而不是个别经理的总分。

以下是代码:

<table style="border-collapse: separate;    border-spacing: 10px;    cellspacing: 47px;    width: 100%;">
<tr>

<td>Manager</td><td>Submissions</td><td>Total Count</td>

</tr>
<?php

 //This page is maangers vs managers on feedback recieved and feedback score overall

//$m = date("m");

$m = '09';

include "dbconnect.php";

$sql = ("SELECT * FROM `clientsfeedback` WHERE MONTH(submitteddate) = '".$m."' ORDER BY `manager` ASC");

//SELECT * FROM `clientsfeedback` WHERE MONTH(submitteddate) = '09' ORDER BY `manager` ASC

            $result=mysql_query($sql); 

            while($row=mysql_fetch_array($result)){ 

            $manager = $row['manager'];

            $tc = $row['totalscore'];

            $manager = $row['manager'];

            $count = ("Select count(".$manager.") FROM clientsfeedback WHERE MONTH(submitteddate) = '".$m."'"); //Spot on

            $c = ("Select SUM(totalscore) FROM `clientsfeedback` WHERE `manager` = ".$manager." AND MONTH(submitteddate) = '".$m."' ORDER BY `manager` ASC");

            $q = mysql_query($c);
            $rt = mysql_fetch_array($q);


            $ct = count($count);

 echo "<tr><td>".$manager."</td><td>".$ct."</td><td>".$rt[0]."</td></tr>";
            }

?>
</table>

提前谢谢

示例数据      -      - 表clientsfeedback的表结构      -

CREATE TABLE IF NOT EXISTS `clientsfeedback` (
  `id` int(250) NOT NULL AUTO_INCREMENT,
  `manager` varchar(255) NOT NULL,
  `securityofficer` varchar(255) NOT NULL,
  `siteid` varchar(255) NOT NULL,
  `sitename` varchar(255) NOT NULL,
  `group` varchar(255) NOT NULL,
  `duedate` varchar(11) NOT NULL,
  `stuniform` int(1) NOT NULL,
  `stattitude` int(1) NOT NULL,
  `stcustomercare` int(1) NOT NULL,
  `docsai` int(1) NOT NULL,
  `docshealthnsafety` int(1) NOT NULL,
  `docsregisters` int(1) NOT NULL,
  `oppatrols` int(1) NOT NULL,
  `opsitetraining` int(1) NOT NULL,
  `oplicences` int(1) NOT NULL,
  `supmanagervisits` int(1) NOT NULL,
  `supcontract` int(1) NOT NULL,
  `supresponce` int(1) NOT NULL,
  `opmcomments` varchar(255) NOT NULL,
  `customerservicedeliverycmt` varchar(255) NOT NULL,
  `totalscore` int(2) NOT NULL,
  `submitteddate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `completedhow` varchar(255) NOT NULL,
  `completedby` varchar(255) NOT NULL,
  `confirmed` int(1) NOT NULL,
  `superior` int(1) NOT NULL DEFAULT '0',
   PRIMARY KEY (`id`)
   ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

--
-- Dumping data for table `clientsfeedback`
--

INSERT INTO `clientsfeedback` (`id`, `manager`, `securityofficer`, `siteid`, `sitename`, `group`, `duedate`, `stuniform`, `stattitude`, `stcustomercare`, `docsai`, `docshealthnsafety`, `docsregisters`, `oppatrols`, `opsitetraining`, `oplicences`, `supmanagervisits`, `supcontract`, `supresponce`, `opmcomments`, `customerservicedeliverycmt`, `totalscore`, `submitteddate`, `completedhow`, `completedby`, `confirmed`, `superior`) VALUES
(1, 'Bennett', 'Brian', '', '', 'ABC', '22/09/2016', 2, 0, 2, 2, 0, 2, 2, 2, 2, 0, 2, 2, 'hello', 'hello', 18, '2016-09-29 16:02:42', 'telephone', 'Dylon', 1, 0),
(2, 'Manager2', 'Pip', '', '', '', '', 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 'None', 'None', 14, '2016-09-12 09:22:57', '', 'Manager2', 1, 0),
(3, 'Bennett', 'Manager2', '', '', '', '', 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 'None', 'None', 14, '2016-09-12 09:29:08', '', '', 1, 0);

1 个答案:

答案 0 :(得分:0)

看起来你的计数sql有些不正确。

如果您想获得给定月份所有经理的所有记录的计数,那么它应该是:

$count = ("Select count(manager) FROM clientsfeedback WHERE MONTH(submitteddate) = '".$m."'");

如果您想获得特定经理在给定月份的所有记录的计数,那么您需要限制它,例如:

$count = ("Select count(manager) FROM clientsfeedback WHERE MONTH(submitteddate) = '".$m."' AND manager = '".$manager."'");

要获取唯一管理员列表,您需要在初始GROUP BY语句中添加SELECT,例如:

$sql = ("SELECT * FROM `clientsfeedback` WHERE MONTH(submitteddate) = '".$m."' GROUP BY manager ORDER BY `manager` ASC");

-----编辑-----

您可能最好使用单个sql查询获取这些值,而不是在循环中嵌套2个查询,例如

$m = '09';

include "dbconnect.php";

$sql = ("SELECT manager, COUNT(manager) as managercount, SUM(totalscore) AS totalscore FROM clientsfeedback WHERE MONTH(submitteddate) = '".$m."' GROUP BY manager ORDER BY manager ASC");

$result=mysql_query($sql); 
while($row=mysql_fetch_assoc($result)){ 

    echo "<tr><td>".$row['manager']."</td><td>".$row['managercount']."</td><td>".$row['totalscore']."</td></tr>";
}