Jump to content

PHP Mysql how to Count $row['price']


impressiveone

Recommended Posts

Dear Friends,

With reference to below code, I want to count the variable $total.

total = 0;
while($row = mysqli_fetch_array($result)){
$price= $row['price'];
$total += $price;
}
echo 'total: ', $total;

I can find the sum as you can see in the code but I also want to count. 

I can do this in Sql query. But want to do in While loop. It's because Sql query would require me to use CASE which I do not want to use for some reasons. First I want to create another variable of prices higher than 10 and then mark them Red. This I can do with if. But I want to count this new variable with Red prices thst is greater than 10.

Link to comment
Share on other sites

Thanks ginergm: 

I will make the code Red marks and paste here and request you for help.

 

First, I want to count below variable.

I can't get the counter code to work.

My try below:

$total = 0;
$price_counts = array();
while($row = mysqli_fetch_array($result)){
    $price= $row['price'];
    $total += $price;
    if (isset($price_counts[$price])) {
        $price_counts[$price]++;
    } else {
        $price_counts[$price] = 1;
    }
}
echo $total; //To do sum
echo "<br>";
echo $price_counts; // To count how many prices in record.

 

Can u help?

Link to comment
Share on other sites

I don't understand what you are trying to count.  You are building an array of the different prices you find and counting how many of EACH UNIQUE price value you have.  What is the purpose of counting how many $1.09 and $1.10 and $1.99 you have?   I thought you were looking for ALL the prices that exceeded a certain amount?

Link to comment
Share on other sites

I'm guessing here, but from the various bits of your posts, you want to list the prices. Those less than 10 should be in red and you want a count of them. Is that close?

$total = 0;
$belowten = 0;;
while($row = mysqli_fetch_array($result)){
    $total += $row['price'];
    if ($row['price'] < 10.00) {
        ++$belowten;
        printf("<span style='color: red'>%20.2f</span><br>", $row['price']);
    }
    else printf("%20.2f<br>", $row['price']);
}
printf("<br>Total: %13.2f<br><br>", $total); 
echo "There were $belowten prices less than 10.00: <br>"; 

Sample output

 

Capture.PNG

Link to comment
Share on other sites

@Barand

 

I tried this, but no luck:

 

$total = 0;
$belowten = 0;

$CountPrices = 0;

while($row = mysqli_fetch_array($result)){
    $total += $row['price'];
    if ($row['price'] < 10.00) {
        ++$belowten;
        printf("<span style='color: red'>%20.2f</span><br>", $row['price']);
    }
    else printf("%20.2f<br>", $row['price']);
}

++$CountPrices;

printf("<br>Total: %13.2f<br><br>", $total); 

echo "Total number of Prices in record: $CountPrices <br>"; 
Link to comment
Share on other sites

You need to accumulate the count inside the loop, not just increment it once after the loop finishes

$total = 0;
$CountPrices = 0;
$maxprice = 0;

while($row = mysqli_fetch_array($result)){
    $total += $row['price'];
    if ($row['price'] < 10.00) {
        printf("<span style='color: red'>%20.2f</span><br>", $row['price']);
    }
    else printf("%20.2f<br>", $row['price']);
    
    ++$CountPrices;                                                    // needs to be inside the loop
    $maxprice = max($maxprice, $row['price']);                         // check for max price;
}



printf("<br>Total: %13.2f<br><br>", $total); 

echo "Total number of Prices in record: $CountPrices <br>"; ?>

echo "Highest price was " . number_format($maxprice, 2);

 

Link to comment
Share on other sites

I tried to look for this solution first. It's so disappointing that someone has come up to help me and I'm receiving these comments.

Is it like I have done something illegal or committed a crime? - It is a learning platform for beginners like me and if seniors do not cooperate, then how will this learning expand to the world.

I don't know why I see too much selfishness increasing as compared to 10 years ago. People really used to be very helpful on online forums. But now, they treat beginners too harsh :(

I express my heartiest gratitude to @Barand & ginerjm for their support to a beginner like. Believe me: My PHP Development Diploma never taught me these techniques. These institutes only teach simple basics. All the technical things that we learn, come from seniors like you.

Link to comment
Share on other sites

Your last post was so un-helpful.   Even your whining isn't clear to me.  Is that a new problem you want us to address?

You were asked to step back from your 'problem' and tell us in English what you want to do.  The issue has gone from counting prices that were greater than something to counting every single different price to coloring them in Red, etc. etc. etc.

We can't solve a moving target so how about choosing one and giving us the full explanation?

Basically your issue seems to be you don't know how to use a loop to accomplish what you want.  Perhaps you need to read up loops?

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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