Jump to content

sorting


DanielBP

Recommended Posts

Part of my code:

 

<?php

$arr = array ("Christina", "Daniel", "Andreea);
reset($arr);

foreach ($arr as $username) {

//more code here ...

foreach ($stats as $result) 
{
echo $username."'s points - ".$result."<br />";
}

}

?>

 

And gives this:

Christina's points - 14
Daniel's points - 45
Andreea's points 23

 

My question is: can you sort the the $result so it will show

 

Daniel's points - 45
Andreea's points 23
Christina's points - 14

 

Thank you

 

Link to comment
Share on other sites

You have to build an array of username=>score and then use ksort to sort by keys.

To quote someone else from today "I hate to turn your posts back at you, but . . ."

 

His example shows the scores sorted in reverse order. If the scores are the values then the function to use would arsort() which will sort the values in reverse order and maintain index association.

Link to comment
Share on other sites

Well I did tried to use the sort functions in php but I don't know how so I found an alternative to it by using a javascript (this) and it works

 

 


<table class="sortable">
<tr>
<th>Username</th>
<th>Points</th>
</tr>

<?php

$arr = array ("Christina", "Daniel", "Andreea");
reset($arr);

foreach ($arr as $username) {

//more code here ...

foreach ($stats as $result) 
{
echo "<tr><td>".$username."</td><td>".$result."</td></tr>";
}

}

?>

</table>

 

 

The only problem is, the user HAS to click the column to sort the results, it won't do it automatically. Bahhh, I'm such a mess at coding :(

 

Link to comment
Share on other sites

You have to build an array of username=>score and then use ksort to sort by keys.

To quote someone else from today "I hate to turn your posts back at you, but . . ."

 

His example shows the scores sorted in reverse order. If the scores are the values then the function to use would arsort() which will sort the values in reverse order and maintain index association.

however this might be an example array.. we are not sure if the array will be in reverse order everytime.. I like choosing the order manually here, however if the array is always in reverse order.. then yes arsort() would be a good option..

Link to comment
Share on other sites

You have to build an array of username=>score and then use ksort to sort by keys.

To quote someone else from today "I hate to turn your posts back at you, but . . ."

 

His example shows the scores sorted in reverse order. If the scores are the values then the function to use would arsort() which will sort the values in reverse order and maintain index association.

however this might be an example array.. we are not sure if the array will be in reverse order everytime.. I like choosing the order manually here, however if the array is always in reverse order.. then yes arsort() would be a good option..

 

The OP gave an example of how the records were to be sorted. The only thing that is sorted in that example is the score - in reverse order. I am not going to make wild conclusions based on information that the OP did not provide. Even, ManiacDan had suggested using a reverse sort, he just goofed and provided a function that would sort by the name instead of the score, which is clearly not what the OP is looking for.

Link to comment
Share on other sites

You have to build an array of username=>score and then use ksort to sort by keys.

To quote someone else from today "I hate to turn your posts back at you, but . . ."

 

His example shows the scores sorted in reverse order. If the scores are the values then the function to use would arsort() which will sort the values in reverse order and maintain index association.

however this might be an example array.. we are not sure if the array will be in reverse order everytime.. I like choosing the order manually here, however if the array is always in reverse order.. then yes arsort() would be a good option..

 

The OP gave an example of how the records were to be sorted. The only thing that is sorted in that example is the score - in reverse order. I am not going to make wild conclusions based on information that the OP did not provide. Even, ManiacDan had suggested using a reverse sort, he just goofed and provided a function that would sort by the name instead of the score, which is clearly not what the OP is looking for.

going off of strictly with what the OP provided, yes the solution given would be the best way to approach this

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.