Jump to content

Sorting names acording to their value


Oxymen

Recommended Posts

Hi.
I have a script where I get names and values which are related to each other.
something like

$name=bob
$bobsvalue=3.32

I need to be able to sort all the names according to their value so that if I had another name

$name=ted
$tedsvalue=5.23

This will be echoed before the first because the value is higher.
Untill now I only needed the highest number so I just ran the names and values through a for loop and checked the highest recorded
value to the current value and updated if the current value was higher than the highest.
But now I need to echo every name and value and sort them. So the above example would echo:

Ted - 5.23
Bob - 3.32

I tried to do this with arrays, but I couldnt do it with the relation so all my attemts would sort the numbers and the names differently.
How can I sort this information by the values and keep the names with the right value?

Thanks! :D
Link to comment
Share on other sites

Thanks, Ronald, that code was exactly what I was looking for. It was exactly like mine, just I have not used
foreach before, so I didn't know about it.
Now I only have one more request to ask on this subject.
This is only for cosmetic reasons, but I want to make the top three names be bold and the top one to have a bit
larger font, then the second would have a slightly smaller font. To do this I would need to isolate the top three results.
Is this possible with the current code?
Link to comment
Share on other sites

try
[code]<STYLE type="text/css">
span.one {
    font-size: 12pt;
    font-weight: 700;
}
span.two {
    font-size: 11pt;
    font-weight: 700;
}
span.three {
    font-size: 10pt;
    font-weight: 700;
}
span {
    font-size: 9pt;
    font-weight: 300;
}
</STYLE>
<?php
$relations = array();
$relations['bob'] = 8.32;
$relations['carol'] = 7.23;
$relations['alice'] = 5.32;
$relations['peter'] = 4.23;
$relations['mary'] = 1.23;
$relations['ted'] = 6.23;

arsort($relations);
$k=0;
foreach ($relations as $key => $val) {
  switch (++$k) {
  case 1:
        echo "<span class='one'>$key = $val</span><br>";
        break;
  case 2:
        echo "<span class='two'>$key = $val</span><br>";
        break;
  case 3:
        echo "<span class='three'>$key = $val</span><br>";
        break;
  default:
        echo "<span>$key = $val</span><br>";
        break;
  }
}
?>[/code]
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.