Jump to content

Archived

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

tomfmason

counting last five values in an array.

Recommended Posts

I am not sure but I may have over thought this one..lol

Anyway, I have a function within a class the returns an array of user names. Well what I am trying to do is get the last five values in the array.

I have tried the following:

[code=php:0]

$users = new users;
$last_members = $users->lastMembers();
$user_count = array_count_values($last_members);

if ($user_count < 5) {
    echo 'Latest '. $user_count . ' Members';
    $i = 0;
    $j = 1;
    foreach ($last_members as $last_member) {
        if ($i <= $usercount) {
            echo $j . ': ' . $last_member . '<br />';
            $i++;
            $j++;
        }
    }
} else {
    echo 'Lastest 5 Members';
    $i = $user_count - 5;
    $j = 1;
    foreach ($last_members as $last_member) {
        if ($i == array_keys($last_members, $last_member) {
            echo $j . ': ' . $last_member . '<br />';
            $i++;
            $j++;
        }
    }
}[/code]

I get the following error with the second part of that snippet.

[quote author=PHP Error]
Fatal error: Unsupported operand types
[/quote]

Any suggestions on a better way of doing this..

Thanks,
Tom

Share this post


Link to post
Share on other sites
try something simple:

$num=count($array);                      to count how many elements are in your array

lets say there were 17.

$temp=$num-5;                              $temp is 5 less than our total count
for($i=$temp; $i<=$num; $i++)          for loop will start at $temp and end at $num  (aka last 5 values of our array)
{
    //this will loop 5 times to do what you want
}


hope it helps

Share this post


Link to post
Share on other sites
I just thought about something.. I knew  I was trying to over think this one..lol

I thought why don't I just adjust my query.. Like this..

[code=php:0]
function lastMembers() {
    $users = array();
    $sql = mysql_query("SELECT * FROM `users`");
    $count = mysql_num_rows($sql);
    if ($count < 5) {
      while($rw = mysql_fetch_assoc($sql)) {
              $users[] = $rw['username'];
      }
    } else {
        $start = $count - 5;
        $q = mysql_query("SELECT * FROM `users` LIMIT '$start', '$count'");
        while($rw = mysql_fetch_assoc($q)) {
                $users[] = $rw['username'];
        }
  }
  return $users;
}[/code]

I knew that I was over thinking it..lol

Thanks,
Tom

Share this post


Link to post
Share on other sites
also if you're dealing with a query, you can just

select column from table order by column desc limit 5

that will only get 5 fields from the chosen column sorted in descending order.  (not always, only if its like an id, date, etc.  names will be alphabetized and whatnot.

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.