Jump to content

Archived

This topic is now archived and is closed to further replies.

Oxymen

Sorting names acording to their value

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

Share this post


Link to post
Share on other sites
easy, an array of course(as you said.) if you have the name-value together like
new array('name'=>'value', 'name'=>'value)
and so on, then they will sort either by name, or by value, and they will all stay connected properly.

Share this post


Link to post
Share on other sites
Store the info in a database where the name = Ted or Bob and Value = 5.23 or 3.32.. then sort by value on the end of the query...  that'll turn back an array but already pre-sorted...

Share this post


Link to post
Share on other sites
Just go for an array:
[code]
$relations = array();
$relations['bob'] = 3.32;
$relations['ted'] = 5.23;

arsort($relations);
foreach ($relations as $key => $val) {
  echo "$key = $val\n";
}
[/code]


Ronald   8)

Share this post


Link to post
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?

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
Thanks a lot!
That works beautifully. Just what I wanted.
Now I learned something today too  :)

Share this post


Link to post
Share on other sites

×

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.