for($i = 0; $i <= @mysql_num_rows($query_agents); $i++) { $data_query_agents = mysql_fetch_array($query_agents); $agent_id = $data_query_agents['id']; $agent_rate_plus_q = $data_query_agents['agent_rate_plus']; $agent_rate_minus_q = $data_query_agents['agent_rate_minus']; $agent_rate_summ = ceil($agent_rate_plus_q/$agent_rate_minus_q); echo "ID: $agent_id | Π Π΅ΠΉΡ‚ΠΈΠ½Π³: $agent_rate_summ"; } 

Generally how to make sorting in a loop in ascending order, based on $ agent_rate_summ?

    1 answer 1

    Why such a perversion? Why can't I sort directly in the query?

     SELECT id, CEILING(agent_rate_plus/agent_rate_minus) as agent_rate_summ FROM имя_Ρ‚Π°Π±Π»ΠΈΡ†Ρ‹ ORDER BY agent_rate_summ ASC 

    UPDATE Crutch may well be like this:

     $a = array(); while($data_query_agents = mysql_fetch_array($query_agents)) { $agent_rate_plus_q = $data_query_agents['agent_rate_plus']; $agent_rate_minus_q = $data_query_agents['agent_rate_minus']; $a[$data_query_agents['id']] = ceil($agent_rate_plus_q/$agent_rate_minus_q); } asort($a); foreach($a as $agent_id => $agent_rate_summ) { echo "ID: $agent_id | Π Π΅ΠΉΡ‚ΠΈΠ½Π³: $agent_rate_summ"; } 
    • @ Indifferent, it is just a perversion. Your way is not useful to me. - ModaL
    • Added perversion. It will work provided that the id is unique, and agent_rate_minus always non-zero. - Indifferent
    • @Indifferent. The foreach loop uses $ agent_rate_summ, but in your code, where the while loop is, there is no such variable. Error?) - ModaL
    • one
      Eh ... I have lost the question of why was used for to bypass the result of the query. No mistake, read mang foreach . - Indifferent
    • @ Indifferent, $ a as $ agent_id => $ agent_rate_summ. According to your code, these variables are not even set ... - ModaL