Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS 

record grouping

@users = User.find(
  :all, 
  :select => "MONTH(created_at) as month, YEAR(created_at) as year, COUNT(*) AS records",
  :conditions => ["admin = 0'"], 
  :group => "year, month")

<% for @user in @users %>
  <%=@user.month%>
  <%=@user.year%>
  <%=@user.records%>
<% end %>

Array group by count

Function for group by count of elements, with a optional parameter for sort on asc and desc mode

<?php
function ArrayGroupByCount($_array, $sort = false) {
   $count_array = array();

   foreach (array_unique($_array) as $value) {
       $count = 0;

		foreach ($_array as $element) {
		    if ($element == $value)
		        $count++;
		}
	
		$count_array[$value] = $count;
	}
	
	if ( $sort == 'desc' )
		arsort($count_array);
	elseif ( $sort == 'asc' )
		asort($count_array);

	return $count_array;
}
?>

SPLIT-UNIQUE - split a block into unique and duplicate values

    split-unique: func [block [any-block!] /local uniq dupe dest] [
        uniq: copy []
        dupe: copy []
        foreach item block [
            dest: either find/only uniq item [dupe] [uniq]
            append/only dest item
        ]
        reduce [uniq dupe]
    ]

GROUP - group like elements in a block

    group: func [
        {Returns a block of sub-blocks with items partitioned by value.}
        block  [any-block!]
        /local result
    ][
        result: copy []
        ; First, build up a list of keys, with a place for values
        ; to go with each key.
        foreach item block [
            if not find/only/skip result item 2 [
                repend result [item copy []]
            ]
        ]
        ; Add items to the block associated with each key.
        foreach item block [append/only select result item item]
        result
    ]
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS