2009-07-21 74 views
1

我有這個代碼打印出我的數據庫中的列,併爲我添加一列「Profit」。 距離是以一種複雜的方式計算的,所以在一個循環中完成,而距離到「利潤」的轉換是在打印出來時完成的。在php數組中排序

我想要做的是按照「利潤」的降序排列。我相信(但不知道)最好的方法是將它們存儲在一個數組中並「在那裏排序」,然後從那裏打印出來。

如何確定將數組粘貼到哪一行?
如何在數組中排序?
如何循環訪問數組,以便不能將它們打印出來?

//display results 
// now we retrieve the routes from the db 
$query = "SELECT * FROM routes ORDER BY `id`;"; 

// run the query. if it fails, display error 
$result = @mysql_query("$query") or die('<p class="error">There was an unexpected error grabbing routes from the database.</p>'); 

?> 
<tr> 
    <td style="background: url(http://www.teamdelta.byethost12.com/barbg.jpg) repeat-x top;"> 
     <center><b><font color="#F3EC84">»Matches«</font></b></center> 
    </td> 
</tr> 

<tr> 
<td style="background: #222222;"> 

</font> 
<table border="0" width="100%"><tr> 


<td width="10%"><b><center><b>Player</b></center></b></td> 
<td width="10%"><center><b>Base</b></center></td> 
<td width="10%"><b>Location</b></td> 
<td width="5%"><b>Econ</b></td> 
<td width="10%"><b>Distance</b></td> 
<td width="10%"><center><b>Profit cred./h</b></center></td> 
<td width="40%"><b>Comment</b></td> 
<td width="5%"><align="right"><b>Delete</b></align></td> 

</tr> 

<? 

// while we still have rows from the db, display them 
while ($row = mysql_fetch_array($result)) { 

    $dname = stripslashes($row['name']); 
    $dbase = stripslashes($row['base']); 
    $dlocation = stripslashes($row['location']); 
    $dx = stripslashes($row['x']); 
    $dy = stripslashes($row['y']); 
    $dgalaxy = stripslashes($row['galaxy']); 
    $dplanet = stripslashes($row['planet']); 
    $dcomment = stripslashes($row['comment']); 
    $did = stripslashes($row['id']); 
    $decon = stripslashes($row['econ']); 

    $distance = -1 ;//default 

    //distance calc 
    if($dgalaxy == $galaxy) 
    {//interstellar 
     if(($dx == $x) && ($dy == $y)) 
     {//inter planitary 
      if ((floor($planet/10)*10) == (floor($dplanet/10)*10)) 
      {// intra planitary loonar 
      $distance = abs(fmod($planet,10)-fmod($planet,10))*0.1; 
      } 
      else 
      {// inter planitary 
      $distance = abs((floor($planet/10)*10)-(floor($planet/10)*10))*0.2; 
      } 
     }else 
     {//interstllar 
      $distance = round(Sqrt(pow(($dx-$x),2)+pow(($dy-$y),2)));//interstllar 
     } 
    } 
    else 
    {//intergalatic 
     if ((floor($galaxy/10)*10) == (floor($dgalaxy/10)*10)) 
     {//intra galactic cluster 
      $distance = abs(($galaxy-$dgalaxy))*200; 
     }  
     else 
     {//inter galactic cluster 
      if ($galaxy < $dgalaxy) 
      {//anti clockwise inter galactic cluster 
      $distance = (((9-fmod($galaxy,10))*200)+2000+(fmod($dgalaxy,10)*200)); 
      } 
      else 
      {//clockwise inter galactic cluster 
      $distance = (((fmod($galaxy,10))*200)+2000+(fmod(9-$dgalaxy,10)*200)); 
      } 
     } 
    } 

    echo('<tr> 
    <td width=\'20px\'><center>('.$dname.')</center></td> 
    <td><center>'.$dbase.'</center></td> 
    <td><a href="http://delta.astroempires.com/map.aspx?loc='.$dlocation.'">'.$dlocation.'</a></td> 
    <td>'.$decon.'</td><td>'.$distance.' </td> 
    <td>'.round(Sqrt(min($decon,$econ))*(1+Sqrt($distance)/75+Sqrt($players)/10)).'</td> 
    <td>['.$dcomment.']</td> 
    <td><a href=deleterouteconfirm.php?id='.$did.'>Delete</a></td> 
    </tr>'); 
} 
?></table><!--display results table--> 

回答

1

我認爲最容易實現的解決方案將是一個雙擊數據庫結果。

第一遍將爲每行生成「距離」和「利潤」值,並將這些行存儲到我們要排序的數組中。

第二遍只是簡單地循環遍歷第一遍創建的數組,然後顯示它們,然後對其進行正確排序並轉義輸出。

<?php 
//display results 
// now we retrieve the routes from the db 
$query = "SELECT * FROM routes ORDER BY `id`;"; 

// run the query. if it fails, display error 
$result = @mysql_query("$query") or die('<p class="error">There was an unexpected error grabbing routes from the database.</p>'); 

?> 
<tr> 
    <td 
     style="background: url(http://www.teamdelta.byethost12.com/barbg.jpg) repeat-x top;"> 
    <center><b><font color="#F3EC84">»Matches«</font></b></center> 
    </td> 
</tr> 

<tr> 
    <td style="background: #222222;"></font> 
    <table border="0" width="100%"> 
     <tr> 


      <td width="10%"><b> 
      <center><b>Player</b></center> 
      </b></td> 
      <td width="10%"> 
      <center><b>Base</b></center> 
      </td> 
      <td width="10%"><b>Location</b></td> 
      <td width="5%"><b>Econ</b></td> 
      <td width="10%"><b>Distance</b></td> 
      <td width="10%"> 
      <center><b>Profit cred./h</b></center> 
      </td> 
      <td width="40%"><b>Comment</b></td> 
      <td width="5%"><align="right"><b>Delete</b></align></td> 

     </tr> 

<? 

// while we still have rows from the db, display them 
$resultSet = array(); 
while ($row = mysql_fetch_array($result)) 
{ 

    $dname = stripslashes($row['name']); 
    $dbase = stripslashes($row['base']); 
    $dlocation = stripslashes($row['location']); 
    $dx = stripslashes($row['x']); 
    $dy = stripslashes($row['y']); 
    $dgalaxy = stripslashes($row['galaxy']); 
    $dplanet = stripslashes($row['planet']); 
    $dcomment = stripslashes($row['comment']); 
    $did = stripslashes($row['id']); 
    $decon = stripslashes($row['econ']); 

    $distance = -1; //default 


    //distance calc 
    if ($dgalaxy == $galaxy) 
    { //interstellar 
    if (($dx == $x) && ($dy == $y)) 
    { //inter planitary 
     if ((floor($planet/10) * 10) == (floor($dplanet/10) * 10)) 
     { // intra planitary loonar 
     $distance = abs(fmod($planet, 10) - fmod($planet, 10)) * 0.1; 
     } else 
     { // inter planitary 
     $distance = abs((floor($planet/10) * 10) - (floor($planet/10) * 10)) * 0.2; 
     } 
    } else 
    { //interstllar 
     $distance = round(Sqrt(pow(($dx - $x), 2) + pow(($dy - $y), 2))); //interstllar 
    } 
    } else 
    { //intergalatic 
    if ((floor($galaxy/10) * 10) == (floor($dgalaxy/10) * 10)) 
    { //intra galactic cluster 
     $distance = abs(($galaxy - $dgalaxy)) * 200; 
    } else 
    { //inter galactic cluster 
     if ($galaxy < $dgalaxy) 
     { //anti clockwise inter galactic cluster 
     $distance = (((9 - fmod($galaxy, 10)) * 200) + 2000 + (fmod($dgalaxy, 10) * 200)); 
     } else 
     { //clockwise inter galactic cluster 
     $distance = (((fmod($galaxy, 10)) * 200) + 2000 + (fmod(9 - $dgalaxy, 10) * 200)); 
     } 
    } 
    } 
    $row['distance'] = $distance; 
    $row['profit'] = round(Sqrt(min($decon, $econ)) * (1 + Sqrt($distance)/75 + Sqrt($players)/10)); 
    $resultSet[] = $row; 
} 

// Perform custom sort 
usort($resultSet, 'sorter'); 
function sorter($a, $b) 
{ 
    if ($a['profit'] == $b['profit']) return 0; 
    return ($a['profit'] < $b['profit']) ? -1 : 1; 
} 

// Switch to "descending" 
array_reverse($resultSet); 

// Output escape the values 
$safeForHtml = array_map('htmlspecialchars', $resultSet); 

foreach($safeForHtml as $row) 
{ 
    echo ('<tr> 
       <td width=\'20px\'><center>(' . $row['name'] . ')</center></td> 
       <td><center>' . $row['base'] . '</center></td> 
       <td><a href="http://delta.astroempires.com/map.aspx?loc=' . $row['location'] . '">' . $row['location'] . '</a></td> 
       <td>' . $row['econ'] . '</td> 
       <td>' . $row['distance'] . ' </td> 
       <td>' . $row['profit'] . '</td> 
       <td>[' . $row['comment'] . ']</td> 
       <td><a href=deleterouteconfirm.php?id=' . $row['id'] . '>Delete</a></td> 
       </tr>'); 
} 
?> 
</table> 
    <!--display results table--> 
1

你從MySQL得到您的數據。爲什麼不直接從查詢中排序結果?

$query = "SELECT * FROM routes ORDER BY `profit` DESC, `id`;"; 

編輯:重讀你的問題,利潤是不是一個領域,但你可能要填充表與你的利潤值,而不是每次都重新計算它。

編輯2:或者,繫住你的輸出,計算你的利潤,把一切都放在一個數組,然後使用以下命令:

$resultArray; //Your array with all your rows plus a profit key-value pair. 

$sortedArray = array_msort($resultArray, array('profit'=>SORT_DESC)); 

// array_msort by cagret at gmail dot com 
function array_msort($array, $cols) 
{ 
    $colarr = array(); 
    foreach ($cols as $col => $order) { 
     $colarr[$col] = array(); 
     foreach ($array as $k => $row) { $colarr[$col]['_'.$k] = strtolower($row[$col]); } 
    } 
    $params = array(); 
    foreach ($cols as $col => $order) { 
     $params[] =& $colarr[$col]; 
     $params = array_merge($params, (array)$order); 
    } 
    call_user_func_array('array_multisort', $params); 
    $ret = array(); 
    $keys = array(); 
    $first = true; 
    foreach ($colarr as $col => $arr) { 
     foreach ($arr as $k => $v) { 
      if ($first) { $keys[$k] = substr($k,1); } 
      $k = $keys[$k]; 
      if (!isset($ret[$k])) $ret[$k] = $array[$k]; 
      $ret[$k][$col] = $array[$k][$col]; 
     } 
     $first = false; 
    } 
    return $ret; 

} 
+0

我不認爲他知道的利潤,直到它在代碼的計算:`輪(SQRT(分鐘($去污,$經濟))*(1 + SQRT($距離)/ 75 + SQRT( $ players)/ 10)` – 2009-07-21 16:23:13

+0

編輯我的帖子...他應該用他的利潤創建一個表... – 2009-07-21 16:23:47