Jump to content


Photo

Sorting names acording to their value


  • Please log in to reply
6 replies to this topic

#1 Oxymen

Oxymen
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 05 September 2006 - 09:14 PM

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

#2 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 05 September 2006 - 09:18 PM

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.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#3 radar

radar
  • Members
  • PipPipPip
  • Advanced Member
  • 645 posts
  • LocationSLC

Posted 05 September 2006 - 09:18 PM

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...

#4 ronverdonk

ronverdonk
  • Members
  • PipPipPip
  • Advanced Member
  • 277 posts
  • LocationNetherlands

Posted 05 September 2006 - 09:21 PM

Just go for an array:
$relations = array();
$relations['bob'] = 3.32;
$relations['ted'] = 5.23;

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


Ronald   8)
RTFM is an almost extinct art form, it should be subsidized.

#5 Oxymen

Oxymen
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 05 September 2006 - 09:35 PM

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?

#6 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 05 September 2006 - 10:06 PM

try
<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;
   }
}
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#7 Oxymen

Oxymen
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 06 September 2006 - 09:33 AM

Thanks a lot!
That works beautifully. Just what I wanted.
Now I learned something today too  :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users