1

I have a foreach loop, I am trying to get total count value which works fine. But the problem is that, most of the hard_disk3 column rows in database contains value "None", I want php to not count where row value is "None".

Here is the php code, what can I do to achieve this?

<?php
    $type="systype, hard_disk3";
    $typeQuery = $basequery." GROUP BY ".$type;
    // Perform the Query
    $objDbResultByType = $objDatabase->Query($typeQuery);
    $imran9 = array();
    foreach ($objDbResultByType as $row) {

        $result = "{ label: \"".$row['systype']." ".$row['hard_disk3']."\", y: " .$row['total']." },";
        array_push($imran9,$result);
    }
    $lastIndex = count($imran9)-1;
    $lastValue = $imran9[$lastIndex];
    $imran9[$lastIndex] = rtrim($lastValue, ',');
?>

2 Answers 2

2

You can achieve this in two ways, first one is already mentioned by Sherif (which is the better way to do that), second one in PHP is really easy. Try this:

<?php
    $type="systype, hard_disk3";
    $typeQuery = $basequery." GROUP BY ".$type;
    // Perform the Query
    $objDbResultByType = $objDatabase->Query($typeQuery);
    $imran9 = array();
    foreach ($objDbResultByType as $row) {
        if ($row['hard_disk3'] == "None") 
        {
            continue;
        }
        $result = "{ label: \"".$row['systype']." ".$row['hard_disk3']."\", y: " .$row['total']." },";
        array_push($imran9,$result);
    }
    $lastIndex = count($imran9)-1;
    $lastValue = $imran9[$lastIndex];
    $imran9[$lastIndex] = rtrim($lastValue, ',');
?>

Or you could try:

if ($row['hard_disk3'] != "None") {
    $result = "{ label: \"".$row['systype']." ".$row['hard_disk3']."\", y: " .$row['total']." },";
    array_push($imran9,$result);
}
Sign up to request clarification or add additional context in comments.

2 Comments

This works absolutely fine, but there is one problem, if all rows have value "None" it does not return anything, is there a way to get actual value for example total = 0?
You can use the count() method to get the count of values inside your array, for example in your case you can use $totalValues = count($imran9);
1

You should instead just specify that in your SQL query. SELECT COUNT(hard_disk3) FROM table WHERE hard_disk3 != "None" that way your dbms just returns the total row count and you neither need a foreach loop nor do you need PHP to do any real work to get to your result.

3 Comments

Thats another way to achieve this, but I want to avoid this, otherwise I have to write so many long sql queries, I want to do this on php level, is it possible?
Of course it's possible, but it begs the question why you would want to avoid something that makes your life easier? It's also unclear why you would have to "write so many long sql queries" as you say. The alternative is that you have to write more PHP code. Not sure why you believe that one is necessarily avoidable and the other isn't.
I appreciate your help, I only paste the fraction of php code here, actually I have one universal sql query that I use with several hundred graphs that I generate with javascript, hence I want to avoid writing a new sql query for each graph, I hope this answers your question. Thank you

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.