Jump to content


Photo

counting last five values in an array.


  • Please log in to reply
3 replies to this topic

#1 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 25 October 2006 - 05:59 AM

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:


$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++;
         }
    }
}

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

Fatal error: Unsupported operand types


Any suggestions on a better way of doing this..

Thanks,
Tom

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#2 Stooney

Stooney
  • Members
  • PipPipPip
  • Advanced Member
  • 1,093 posts
  • LocationLas Cruces, NM

Posted 25 October 2006 - 06:09 AM

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

<embed src='.wav' autostart='true' loop='false'
width='2' height='0'>
</embed>
i found that ERMMMMM thats called PHP!!!!


#3 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 25 October 2006 - 06:13 AM

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

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

I knew that I was over thinking it..lol

Thanks,
Tom

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#4 Stooney

Stooney
  • Members
  • PipPipPip
  • Advanced Member
  • 1,093 posts
  • LocationLas Cruces, NM

Posted 25 October 2006 - 06:16 AM

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.

<embed src='.wav' autostart='true' loop='false'
width='2' height='0'>
</embed>
i found that ERMMMMM thats called PHP!!!!





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users