2

私は現在、データベースからデータを取得し、その一部を計算してからテーブルに出力するコードを持っています。ただし、印刷された数字を大きいものから小さいものへと並べたいと思います。クエリでの順序付けには精通していますが、クエリは最終結果がどうなるかわかりません。このデータを再配置する最も効率的な方法は何でしょうか。

<?php
    $hourAmount = $mlYeild * (3600 / $cycleTime);
    $oreVolumePriceQuery = "SELECT od.oreID, od.inGameID, od.size, ir.officialCorpRate FROM oreData od JOIN itemRates ir ON od.inGameID = ir.typeID order by od.oreID asc";
    $oreVolumePriceResult = mysql_query($oreVolumePriceQuery);
$hourYeild = $mlYeild * (3600 / $cycleTime);
?>
<TABLE>
    <TR>
        <TD><h1>Ore</h1></TD>
        <TD><h1>Hourly Yeild</h1></TD>
        <TD><h1>Hourly Sale</h1></TD>
    </TR>
    <?php
        $ores = array();
        While($row = mysql_fetch_assoc($oreVolumePriceResult)){
            $inGameID = $row['inGameID'];
            $volume = $row['size'];
            $rate = $row['officialCorpRate'];
            $name = $row['name'];
            $hourlyYeild = $hourAmount/$volume;
            $hourlyIncome = $hourlyYeild * $rate;

            echo "<TR>";
            echo "<TD>".$name."</TD>";
            echo "<TD>".$hourlyYeild."</TD>";
            echo "<TD>".$hourlyIncome."</TD>";
            echo "</TR>";
        }
    ?>
</TABLE>

要するに、入ってくるデータから、$hourlyIncome desc

4

2 に答える 2

2
  1. First store your computations back into the $row (it looked like $hourAmount was missing--I assumed that this was $rate, hope I got it right):

    $inGameID = $row['inGameID'];
    $volume = $row['size'];
    $rate = $row['officialCorpRate'];
    $name = $row['name'];
    $hourlyYield = $row['hourlyYield'] = $rate /$volume;
    $hourlyIncome = $row['hourlyIncome '] = $hourlyYield * $rate;

  2. Append the $row to a $rows array: $rows[] = $row;

  3. When you have all the $rows, use a custom comparison function to sort the $rows array.

    User-defined comparison:

    function cmp($a, $b)  
    {  
        if ($a['hourlyIncome'] == $b['hourlyIncome']) {  
            return 0;  
        }  
        return ($a['hourlyIncome'] < $b['hourlyIncome']) ? 1 : -1;  
    }
    
  4. Sort $rows via usort($rows, $comp);

  5. Do a standard loop over $rows to display.

Note: Still seems to me like all of these calculations could have been done on the SQL side.

于 2013-01-11T00:10:07.203 に答える
0

Well, I don't know if it's the most efficient way but you can store the calculated data in an array and then sort it.

于 2013-01-11T00:11:06.340 に答える