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
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"];
  }

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.