2012-09-19 203 views
0

我是相當新的編程,並試圖從用戶名從mssql查詢排序此數組。已經嘗試過使用其他人在網絡上做出的一些排序功能,但沒有運氣任何幫助都會令人驚歎!排序基於php數組的對象

function getSalesPersonTargetTracker() 
{ 
$targetTracker = new StdClass(); 
$targetTracker->agents = array(); 

$server = 'blah'; 
$connectioninfo = array('database' => 'blah', 'uid' => 'blah', 'pwd' => 'blah'); 

$connection = sqlsrv_connect($server, $connectioninfo); 

$query = "{call salespersontargettracker (?)}"; 
$params = array(
       date_format(new DateTime(), '01/M/Y') 
      ); 

//Run the Query    
$statement = sqlsrv_query($connection, $query, $params); 

if (sqlsrv_fetch($statement) === false) 
    print_r(sqlsrv_errors(), true); 

//Read in the overall numbers 
$targetTracker->target = (float)sqlsrv_get_field($statement, 0); 
$targetTracker->workingDays = (int)sqlsrv_get_field($statement, 1); 
$targetTracker->currentDay = (int)sqlsrv_get_field($statement, 2); 
$targetTracker->dailyTarget = (float)sqlsrv_get_field($statement, 3); 
$targetTracker->pointsShouldBeOn = (float)sqlsrv_get_field($statement, 4); 
$targetTracker->pointsOn = (float)sqlsrv_get_field($statement, 5); 
$targetTracker->pointsDifference = (float)sqlsrv_get_field($statement, 6); 
$targetTracker->moneyShouldBeOn = (float)sqlsrv_get_field($statement, 7); 
$targetTracker->moneyOn = (float)sqlsrv_get_field($statement, 8); 
$targetTracker->moneyDifference = (float)sqlsrv_get_field($statement, 9); 

//Move to the next result 
sqlsrv_next_result($statement); 

//Run through all of the agents and show their details 
while (sqlsrv_fetch($statement)) 
{ 
    $salesPerson = new StdClass(); 
    $salesPerson->team = sqlsrv_get_field($statement, 0); 
    $salesPerson->username = sqlsrv_get_field($statement, 1); 
    $salesPerson->target = (float)sqlsrv_get_field($statement, 2); 
    $salesPerson->workingDays = (int)sqlsrv_get_field($statement, 3); 
    $salesPerson->currentDay = (int)sqlsrv_get_field($statement, 4); 
    $salesPerson->dailyTarget = (float)sqlsrv_get_field($statement, 5); 
    $salesPerson->pointsShouldBeOn = (float)sqlsrv_get_field($statement, 6); 
    $salesPerson->pointsOn = (float)sqlsrv_get_field($statement, 7); 
    $salesPerson->pointsDifference = (float)sqlsrv_get_field($statement, 8); 
    $salesPerson->moneyShouldBeOn = (float)sqlsrv_get_field($statement, 9); 
    $salesPerson->moneyOn = (float)sqlsrv_get_field($statement, 10); 
    $salesPerson->moneyDifference = (float)sqlsrv_get_field($statement, 11); 

    //Add this person to the people list 
    $targetTracker->agents[] = $salesPerson; 
} 

return $targetTracker; 
} 
$targetTrackerall = getSalesPersonTargetTracker(); 

?> 
    <table width="100%"> 
     <thead> 
      <tr> 
       <th><center>Username</center></th> 
       <th><center>Day</center></th> 
       <th><center>%</center></th>  
       <th><center>% +/-</center></th>     
       <th><center>Actual Total</center></th> 
       <th><center>+/- Total</center></th> 
      </tr> 
     </thead> 

    <?php 
    //This loops through every single agent in our array 
    foreach ($targetTrackerall->agents as $agent) { 
     //If their points difference is a negative, then name & shame them 
     if ($agent->pointsDifference < 0) 
     { 
      ?> 
      <tr> 
       <td><b><?php echo $agent->username ?></b></td> 
       <td style="text-align: center;"><?php echo $agent->currentDay ?>/<b><?php echo $agent->workingDays?></b></td> 
       <td style="text-align: center;"><?php echo $agent->pointsOn ?>%</td> 
       <td style="text-align: center;"><?php echo round($agent->pointsDifference) ?>%</td> 
       <td style="text-align: center;">£<?php echo number_format($agent->moneyOn) ?></td> 
       <td style="color: red; text-align: center;">-£<?php echo number_format(($agent->moneyDifference)*-1) ?></td> 

    <?php 

     } 
    } 
    ?> 
    </tr> 
    </table> 
+1

在您的數據庫查詢的ORDER BY條款...! – deceze

+3

你會得到什麼?你想要什麼?哪些代碼與您的問題相關? – PiTheNumber

+0

無法添加到數據庫查詢的訂單,就像用於其他事情一樣!將是最簡單的方法: - > –

回答

0

如果用戶名域是唯一的,那麼你可以使用它作爲代理陣列的關鍵和陣列上使用kso​​rt。

<?php 
... 

while (sqlsrv_fetch($statement)) 
{ 
    ... 

    //Add this person to the people list 
    $targetTracker->agents[$salesPerson->username] = $salesPerson; 
    ksort($targetTracker->agents); 
} 
... 

?> 
+0

這很好用,謝謝。只需要找出如何讓它忽略大小寫! –

+0

@AdamClark可以使用'$ targetTracker->代理[strtolower($ salesPerson-> username)] = $ salesPerson;'。如果你的PHP版本高於5.4.0,你也可以使用'ksort($ targetTracker-> agents,SORT_STRING | SORT_FLAG_CASE);' – air4x

1

您應該使用usort,寫一個回調函數像

} 
    function cmp($a, $b) 
    { 
     return strcmp($a->username, $b->username); 
    } 
    usort($targetTracker, 'cmp'); 

    return $targetTracker; 
} 
+0

我該如何整合函數轉換成上面的代碼,我很無用! –

+0

你可以在返回數組之前把它放到你的函數中,聲明排序函數然後調用usort –

+0

試過這個但是我沒有運氣\t return $ targetTracker; } 函數cmp($ a,$ b) { return strcmp($ a-> username,$ b-> username); } $ targetTrackerall = getSalesPersonTargetTracker(); $ targetTrackerallt = cmp($ targetTrackerall) –