计算每个月的负面评论数量并放入数组

时间:2015-01-28 16:51:34

标签: php arrays mysqli

我在这样的数据库中有简单的表:

id	date			feedback
0	2014–10-25		0
1	2014–10-28		1
1	2014–10-29		1
2	2014–11-14		1
3	2014–11-18		0
4	2015–01-10		2
5	2015–01-18		1

我试图找出如何从我的表中用PHP收集以下信息:

  1. 每个月0,1,2(0为负,1中性,2为正)的反馈结果总数。
  2. 例如,2014年10月有1个负面和2个中立评论。

    我想将结果存储在一个数组中。

    有什么建议吗?

    谢谢!

2 个答案:

答案 0 :(得分:0)

select date_format(date, '%b %Y'), 
case feedback when 0 then 'negative' when 1 then 'neutral' when 2 then 'positive' end case, 
count(feedback) 

from your_table 

group by date_format(date, '%b %Y'), feedback

使用此查询并在您的提取(使用PDO)中执行此操作:

fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_GROUP)

使用PDO:

  

$ sth = $ dbh-> prepare(“select date_format(date,'%b%Y'),        案例反馈当0然后'否定'当1然后'中立'当2然后'正'结束情况,        计数(反馈)“);

     

$ sth->执行(); / *第一列的组值* /
  的var_dump($ sth->使用fetchall(PDO :: FETCH_COLUMN | PDO :: FETCH_GROUP));

答案 1 :(得分:0)

嗯,我希望这不是过度杀戮!但我准备了一些类来帮助您以一种使代码可重用的方式处理您的问题。

在同一目录中我有以下文件

  1. constants.php
  2. db.php中
  3. review.php
  4. / *********** constants.php ************* /

    define('USER','root');
    define('HOST','localhost');
    define('DBNAME','dbsupd');
    define('PASS','');
    
    define('NEGATIVE',0);
    define('NEUTRAL',1);
    define('POSITIVE',2);
    

    / *********** db.php中************* /

    require_once("constants.php");
    
    class dbMySql
    {
        protected $_connection;
    
        function __construct()
        {
            $this->openConnection();
        }
    
        public function openConnection()
        {
            try
            {       
                $this->connection = new PDO('mysql:host=' .HOST.';'.'dbname='.DBNAME,USER,PASS);
            }
            catch(PDOexception $e)
            {
                $this->_connection = NULL;
                echo("error: ".$e->getMessage());
                die();
            }
        }
    
        public function doQueryPreReturn($queryIn, array $Params)
        {
            if (isset($this->connection))
            {
                $stmt = $this->connection->prepare($queryIn);
    
                $stmt->execute($Params);
    
                $resultSet = $stmt->fetchAll();
    
                return $resultSet;
            }
        }
    }
    

    / ************************ review.php ******************* ******** /

    class Review extends dbMySql
    {
        function __construct()
        {
            parent::__construct();
        }
    
        public function getReviewByType($type)
        {
            $query = "SELECT id,date,feedback FROM review where feedback = ?"; // not comfortable with the column named "date" though
            $param = array();
    
            $param = $this->getParamByType($type);
    
            $review = $this->doQueryPreReturn($query,$param);
    
            return $review;
        }
    
        public function getReviewCountByType($type)
        {
            $query = "SELECT COUNT(*) FROM review where feedback = ?"; 
            $param = array();
    
            $param = $this->getParamByType($type);
    
            $review = $this->doQueryPreReturn($query,$param);
    
            return $review;
        }
    
        private function getParamByType($type)
        {
            $param = array();
    
            switch($type)
            {
                case NEGATIVE:
                    return array(NEGATIVE);
                case NEUTRAL:
                    return array(NEUTRAL);
                case POSITIVE:
                    return array(POSITIVE);
            }
        }
    }
    

    您现在可以从单独的文件中使用这些类

    require_once("constants.php");
    require_once("db.php");
    require_once("review.php");
    
    $reviewInst = new Review();
    
    // get all neutral reviews
    $neutralRev = $reviewInst->getReviewByType(NEGATIVE);
    
    print_r($neutralRev);
    
    // get count of neutral reviews 
    
    $count = $reviewInst->getReviewCountByType(NEGATIVE);
    print_r($neutralRev);