SQL中的条目中的PHP Tally Total - 最佳实践方法

not so much of a problem but more looking for best practice and the most efficient solution to an issue.

I have some code that reads all the lines for a userID in an sql table and can print them fine. Each row contains {Sales No. - D_No - Category1 - Category2 - Date}.

Now for each row I could have a massive number of if statements or etc to see if the category is equal to a field and then add 1 to a variable that acts as a tally as such. But I'm wanting something more efficient. Where it creates a variable of its own name and or adds to the existing variable of its name.

This is a summary of the pre-existing code:

while ($row = mysqli_fetch_assoc($result)) {
        echo "<tr>";
        foreach ($row as $field => $value) {
                foreach ($salesItemsArray as $saleItem){
                    if (strpos($value, '******CategoryNAME******') !== false){

                }
            }           
            echo "<td>" . $value . "</td>"; // I just did not use "htmlspecialchars()" function. 
        }
        echo "</tr>";
    }
    echo "</table>";

So this code would essentially (possibly with a bit of editing) work if i replace the *****CategoryNAME***** with the actual category and repeat for each category or use an equivilant switch command (if php has one?). However if i could get the program to create a variable called $category Name each time it sees a new category and then add 1 to it each other time it would work for any category name and would be much more efficient and simpler.

Is this possible and any ideas how to implement?

Willing to rephrase parts if needed if confusing.


updated attempt at code :

foreach ($row as $field => $value) { // I you want you can right this line like this: foreach($row as $value) {
                foreach ($salesItemsArray as $saleItem){
                    if (strpos($value, $saleItem) !== false){
                        $foo[$saleItem] = $foo[$saleItem] + 1;
                }
                foreach ($foo as $saleTotal){
                    echo $saleTotal."<br/>".$foo[$saleTotal];       
                }
            }           
            echo "<td>" . $value . "</td>";

Final code edit post hopefully

Thanks to comments i've gotten to this. However slight issue is it says there is 1 for every $foo[$saleItem] combination from echoing it out. As the database stands there should only be 1 item with 1 and everything else in the array and its variable 0...

while ($row = mysqli_fetch_assoc($individualResult)) {
        echo "<tr>";
        foreach ($row as $field => $value){
                foreach ($salesItemsArray as $saleItem){
                    if (strpos($value, $saleItem) !== false){
                        if(isset($foo[$saleItem])) { $foo[$saleItem] += 1; } else { $foo[$saleItem] = 1;}
                        echo $saleItem;
                }
                foreach ($salesItemsArray as $saleTotal){
                    echo $foo[$saleTotal];
                    if ($foo[$saleTotal] > 0){
                        echo $saleItem."+".$foo[$saleTotal];
                        }       
                    }
                }           
                echo "<td>" . $value . "</td>";
            echo "</tr>";
        }