impressiveone Posted September 30, 2018 Share Posted September 30, 2018 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. Quote Link to comment Share on other sites More sharing options...
Barand Posted September 30, 2018 Share Posted September 30, 2018 An if() statement comes in useful at time lke this Quote Link to comment Share on other sites More sharing options...
ginerjm Posted October 1, 2018 Share Posted October 1, 2018 So you add a counter that checks the price during each iteration. That's not much different than getting the total of prices. But you say you want to mark it in red? What do you mean since you aren't outputting anything? Quote Link to comment Share on other sites More sharing options...
impressiveone Posted October 1, 2018 Author Share Posted October 1, 2018 (edited) 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? Edited October 1, 2018 by impressiveone Quote Link to comment Share on other sites More sharing options...
ginerjm Posted October 2, 2018 Share Posted October 2, 2018 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? Quote Link to comment Share on other sites More sharing options...
Barand Posted October 2, 2018 Share Posted October 2, 2018 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 Quote Link to comment Share on other sites More sharing options...
impressiveone Posted October 2, 2018 Author Share Posted October 2, 2018 (edited) @Barand: Sir you have made my day. I will test this and let you know. You have almost solved my problem. Actually, I want a count of all prices not Red ones. Like there are total 5 prices. Will get back to you after testing. Thanks a lot Edited October 2, 2018 by impressiveone Quote Link to comment Share on other sites More sharing options...
impressiveone Posted October 2, 2018 Author Share Posted October 2, 2018 @Barand: Actually, I want a count of all prices not Red ones. Like there are total 5 prices from your example above. Quote Link to comment Share on other sites More sharing options...
Barand Posted October 2, 2018 Share Posted October 2, 2018 Tricky - that means you have to move a line of code; Quote Link to comment Share on other sites More sharing options...
impressiveone Posted October 2, 2018 Author Share Posted October 2, 2018 @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>"; Quote Link to comment Share on other sites More sharing options...
impressiveone Posted October 2, 2018 Author Share Posted October 2, 2018 Similarly I also want to echo maximum price that is 15.5 Quote Link to comment Share on other sites More sharing options...
Barand Posted October 2, 2018 Share Posted October 2, 2018 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); 1 Quote Link to comment Share on other sites More sharing options...
maxxd Posted October 3, 2018 Share Posted October 3, 2018 As patient and helpful as @Barand and @ginerjm have been through this conversation, I feel like we're slipping into @benanamen specialty, the XY problem. What are you actually trying to accomplish? It seems like the same question has been answered several times already, only to be met with new and slightly different requirements. Quote Link to comment Share on other sites More sharing options...
impressiveone Posted October 3, 2018 Author Share Posted October 3, 2018 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. Quote Link to comment Share on other sites More sharing options...
impressiveone Posted October 3, 2018 Author Share Posted October 3, 2018 (edited) Western Union?? Edited October 3, 2018 by impressiveone Quote Link to comment Share on other sites More sharing options...
ginerjm Posted October 3, 2018 Share Posted October 3, 2018 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? Quote Link to comment Share on other sites More sharing options...
impressiveone Posted October 3, 2018 Author Share Posted October 3, 2018 @ginerjm : Many thanks. I went through the loops again and followed the post of @Barand. Special thanks to @Barand. He is a real guru who understood what I wanted and provided good help. My problem is solved now Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.