Jump to content

Easy question.


KANiS

Recommended Posts

Hello. I am new at php, but it is a very interesting language, so I'm learning it. So far it was going well. But now, I cant figure out one thing:

 

I have info stored in .ini files. In that files there are names, and points gained on my site. I can read the .ini, get the name and score, but. How do I make a top 10 of users? Could anyone give me an example, because yet, I have no idea how to do this.

 

The function I use to read .ini's:

 

function readsave($file,$item) {
  $source = file($file);
  foreach ($source as $temp) {
if (preg_match("/^".$item."=/i",$temp)) {
          $result = explode("=",$temp);
	if(!$result[1]) $result[1] = "--";
          return $result[1];
        }
  }
}

 

 

If anyone could help me, I would be very thankful.  :)

Link to comment
https://forums.phpfreaks.com/topic/119750-easy-question/
Share on other sites

you can fill up an array with the names/scores, and then sort the array:

 

function readsave($file,$item) {
 $source = file($file);
 $scores = array();
 foreach ($source as $temp) {
if (preg_match("/^".$item."=/i",$temp)) {
         $result = explode("=",$temp);
	if(!$result[1]) $result[1] = 0;
         $scores["$result[0]"] = $result[1];
       }
 }
 return $scores;
}

 

readsave() will return an array of the format name => score. use arsort() with what i would assume is SORT_NUMERIC, and there you are.

 

EDITED BY akitchin:  barand has beaten me to it, and with more handy solutions i believe.

Link to comment
https://forums.phpfreaks.com/topic/119750-easy-question/#findComment-616972
Share on other sites

Archived

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

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