Oxymen Posted September 5, 2006 Share Posted September 5, 2006 Hi.I have a script where I get names and values which are related to each other.something like $name=bob$bobsvalue=3.32I need to be able to sort all the names according to their value so that if I had another name$name=ted$tedsvalue=5.23This 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 recordedvalue 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.23Bob - 3.32I 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 Quote Link to comment https://forums.phpfreaks.com/topic/19818-sorting-names-acording-to-their-value/ Share on other sites More sharing options...
Ninjakreborn Posted September 5, 2006 Share Posted September 5, 2006 easy, an array of course(as you said.) if you have the name-value together likenew 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. Quote Link to comment https://forums.phpfreaks.com/topic/19818-sorting-names-acording-to-their-value/#findComment-86680 Share on other sites More sharing options...
radar Posted September 5, 2006 Share Posted September 5, 2006 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... Quote Link to comment https://forums.phpfreaks.com/topic/19818-sorting-names-acording-to-their-value/#findComment-86681 Share on other sites More sharing options...
ronverdonk Posted September 5, 2006 Share Posted September 5, 2006 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) Quote Link to comment https://forums.phpfreaks.com/topic/19818-sorting-names-acording-to-their-value/#findComment-86687 Share on other sites More sharing options...
Oxymen Posted September 5, 2006 Author Share Posted September 5, 2006 Thanks, Ronald, that code was exactly what I was looking for. It was exactly like mine, just I have not usedforeach 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 bitlarger 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? Quote Link to comment https://forums.phpfreaks.com/topic/19818-sorting-names-acording-to-their-value/#findComment-86705 Share on other sites More sharing options...
Barand Posted September 5, 2006 Share Posted September 5, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/19818-sorting-names-acording-to-their-value/#findComment-86726 Share on other sites More sharing options...
Oxymen Posted September 6, 2006 Author Share Posted September 6, 2006 Thanks a lot!That works beautifully. Just what I wanted.Now I learned something today too :) Quote Link to comment https://forums.phpfreaks.com/topic/19818-sorting-names-acording-to-their-value/#findComment-86969 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.