2

$query = "select * from tableitem";
$result = mysql_query($query);

$col = array();

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

 //they have computation here inside loop.
  $amount = $value;

if($row['status']>3){  // base on the status only
    if($amount>0){ //base on the computation 

        $count = $item;
        // i need to count here the item before the grouping of duplicate item below.

    if (!isset($col[$row['item']])) { // this is working for grouping the duplicate value
       echo $row['item'];  
       echo $count;   // count item 
       $col[$row['item']] = true;
      }
   }
 }}

Sample output should be like this inside while loop if possible.

Item one 2
Item two 3
Item five 4

2 Answers 2

2

You can do that with sql not necessary php;

SELECT COUNT(*) as N,item 
FROM tableitem
GROUP BY item

it will return duplicate items and you can check with n>1. And you can add more column for group.

$itemArray;
while ($row = mysql_fetch_array($result)){
    if($row['n']>1){
       itemArray[] = $row['item'];    
    }    
}
print_r($itemArray);
Sign up to request clarification or add additional context in comments.

1 Comment

tnkyou hurricane but im sorry to include that i have computation inside loop. so based on the computation and conditon thats only the time i count the item.
0

If you increment array values using the keys you are trying to count:

$check = array();
foreach($values as $key => $value) {
   if (isset($check[$key])) {
       $check[$key]++;
   } else {
       $check[$key]=1;
   }
}
var_dump($check); 

1 Comment

thnks nicholas. im trying to count the item base on the codition above

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.