how can i count or merge same data from the database? here is my code but this wont work..help please..

<?php
$result = mysql_query("SELECT category FROM inventory");

while($row = mysql_fetch_array($result)){
    $cat_array = array($row['category']);
    foreach ($cat_array as $value){
        if ($value<=1){
        echo $value . "</br>";
        }else{}
    }
}   
?>

and my output is this..how can i make same value echoed as one? or disregard the duplicate one..
  • speaker
    accessory
    lights
    tweeter
  • speaker

Recommended Answers

All 2 Replies

Try using an array and adding to it, that way we can check if there's a value already matching it.

<?php
$result = mysql_query("SELECT category FROM inventory");

$output = array();

while($row = mysql_fetch_array($result)){
    $cat_array = array($row['category']);
    foreach ($cat_array as $value){
        if ($value<=1 && !in_array($value,$output)){ # If $value is already in the array
            array_push($output,$value); # Add to array
        } # No need for 'else{}'
    }
}

foreach($output as $value){
    echo $value."<br/>";
}
?>

mattster..it is working :D thank you very much..i have to look for array_push though.. again thank you :)

commented: No worries :) +4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.