0

please, how to sort by percent? Inside foreach ?

this is how I have built the code

foreach($rows as $data) {
 $percent = 80; //this is the result I will get
 echo "<div>Product has $percent %</div>";
}

I have a percentage calculation inside

CrACK
  • 5
  • 2
  • Your question doesn't really make sense as described: if the percentage is always 80, there isn't any sorting to do; and I don't know what "sorting inside foreach" would mean. However, this reference will hopefully help you: https://stackoverflow.com/questions/17364127/how-can-i-sort-arrays-and-data-in-php – IMSoP Aug 21 '21 at 11:50
  • the value is still not 80. In foreach I have a function to calculate the percentage of ratings. – CrACK Aug 28 '21 at 09:43
  • I presumed that was the case, but the code you've given here is simplified so much that it's completely impossible to answer the question. See if the reference I linked helps you make a first attempt, and read the tips on creating a [mcve] if it doesn't work out – IMSoP Aug 28 '21 at 10:02

1 Answers1

0

I do not know anything about php but to keep it simple I would do the calculations, sort the data in the table and then display the table. However, if you want to do this on the fly you would need to have an array/list where the new elements are inserted in the correct order after each calculation. Perhaps php has such a collection available. I don’t know.

Toni78
  • 97
  • 6