tomfmason Posted October 25, 2006 Share Posted October 25, 2006 I am not sure but I may have over thought this one..lolAnyway, 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 Link to comment https://forums.phpfreaks.com/topic/25022-counting-last-five-values-in-an-array/ Share on other sites More sharing options...
Stooney Posted October 25, 2006 Share Posted October 25, 2006 try something simple:$num=count($array); to count how many elements are in your arraylets say there were 17.$temp=$num-5; $temp is 5 less than our total countfor($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 Link to comment https://forums.phpfreaks.com/topic/25022-counting-last-five-values-in-an-array/#findComment-114031 Share on other sites More sharing options...
tomfmason Posted October 25, 2006 Author Share Posted October 25, 2006 I just thought about something.. I knew I was trying to over think this one..lolI 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..lolThanks,Tom Link to comment https://forums.phpfreaks.com/topic/25022-counting-last-five-values-in-an-array/#findComment-114032 Share on other sites More sharing options...
Stooney Posted October 25, 2006 Share Posted October 25, 2006 also if you're dealing with a query, you can just select column from table order by column desc limit 5that 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. Link to comment https://forums.phpfreaks.com/topic/25022-counting-last-five-values-in-an-array/#findComment-114033 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.