0

I am trying to sort this array by username from a mssql query. have tried using a couple of sort functions that others have made on the web, but have had no luck.

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>
4
  • 1
    At an ORDER BY clause to your database query...! Commented Sep 19, 2012 at 16:21
  • 3
    What do you get? What do you want? What code is relevant for your problem? Commented Sep 19, 2012 at 16:23
  • Can't add an order by to the database query as is used for other things! Would be the easiest way :-> Commented Sep 19, 2012 at 16:30
  • Take a look at OPENQUERY. msdn.microsoft.com/en-us/library/ms188427.aspx something like SELECT * INTO #tmp FROM OPENQUERY(yourserverr, 'EXEC salespersontargettracker (?)') ORDER BY ... Commented Sep 19, 2012 at 18:20

2 Answers 2

1

You should use usort and write a callback function like

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

    return $targetTracker;
}
Sign up to request clarification or add additional context in comments.

3 Comments

how would I integrate this function into the code above, I'm pretty useless!
You could put it in your function before returning the array, declare the sorting function then call the usort one
tried this but am having no luck return $targetTracker; } function cmp($a, $b) { return strcmp($a->username, $b->username); } $targetTrackerall = getSalesPersonTargetTracker(); $targetTrackerallt = cmp($targetTrackerall)
0

If the username field is unique then you can use it as the key for the agents array and use ksort on the array.

<?php
...

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

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

?>

2 Comments

This works really well, thank you. Just need to find out how to make it ignore capitalisation!
@AdamClark you can use $targetTracker->agents[strtolower($salesPerson->username)] = $salesPerson;. If your php version is above 5.4.0, you can also use ksort($targetTracker->agents, SORT_STRING | SORT_FLAG_CASE);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.