Sushivam
Sushivam

Reputation: 3107

Iterate each row and get the count php mysql

I have a row in mysql

Uid  campid  type
1    100     Type1
1    100     Type1
1    200     Type1

I need to get the count of each Type1 for each campid

ie) For campid 100 with Type1 - count = 2

For campid 200 with Type1 - count = 1

Below i am getting the total count of all campid which i dont want.

$Impressionarr = [];
$imp_qry = "select count(*) as ImpressionCount from ClicksAndImpressions where Uid = 101655 and CampaignID = 109 and Type='Impression' ;";
$impData = $this->getClicksAndImpressionsTable()->CustomQuery($imp_qry);

if($impData[0]['ImpressionCount'] != '' || $impData[0]['ImpressionCount'] !=NULL ){
                $impr_update = "UPDATE ClicksAndImpressions SET ImpressionCount = ". $impData[0]['ImpressionCount'] ." where Uid = 101655 and CampaignID =109 and Type='Impression' ;";
                $impqryexecute= $this->getClicksAndImpressionsTable()->CustomUpdate($impr_update);

                foreach($impData as $key=>$data2)
                {
                    $data2['ImpressionCount'];
                    $Impressionarr[] = $data2; 
                }

            }else{
                return array("errstr" => "success.", "success" => 0, "Campaigns" => "No Impression Counts");
            }


public function CustomQuery($id) {
  $sql = $id;
  foreach (($this->tableGateway->getAdapter()->driver->getConnection()->execute($sql)) as $row){
        $results[] = $row;
    }
    return $results;
}

Upvotes: 0

Views: 21

Answers (1)

seravee
seravee

Reputation: 434

select count(uid) as total, type from ClicksAndImpressions  group by campid  

try with the above query

Upvotes: 1

Related Questions