Jump to content

help with array counting results


tomdchi

Recommended Posts

The query below returns a result for the previous month that contains a list of merchant id's.  The $count variable needs to return a value of how many of each $merchantid is contained in the array.

What I have returns the total rows.  Can someone help me with this please?

 

<?php

$query = "select merchantid from tblpmts where date >= date_sub(curdate(), interval 1 month) and date <= date_sub(curdate(), interval 1 day)";

$result = mysql_query($query);

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

  $merchantid = $data["merchantid"];

  $count = $data["merchantid"][mysql_num_rows];

  }

 

?>

 

Link to comment
https://forums.phpfreaks.com/topic/134386-help-with-array-counting-results/
Share on other sites

try

<?php
$query = "select merchantid, count(*) as c from tblpmts where date >= date_sub(curdate(), interval 1 month) and date <= date_sub(curdate(), interval 1 day) group by merchantid";
$result = mysql_query($query);
while ($data = mysql_fetch_array($result)) {
  $merchantid = $data["merchantid"];
  $count = $data["c"];
  }

?>

To start with,

$merchantid = $data["merchantid"];

is going to overwrite the current value of $merchantid every iteration of the loop. Is that really what you want?

 

<?php
$query = "select distinct merchantid from tblpmts where date >= date_sub(curdate(), interval 1 month) and date <= date_sub(curdate(), interval 1 day)";
$result = mysql_query($query);
$merchantid = array();
while ($data = mysql_fetch_array($result)) {
  $merchantid[] = $data["merchantid"];
}
$count = count($merchantid);

?>

might do what you want

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.