Browsing articles from "April, 2009"

Paging with LINQ in ASP.NET MVC

Apr 25, 2009   //   by Michael D. Irizarry   //   JavaScript  //  No Comments

Here’s a really neat and simple way to add pagination to your LINQ results in ASP.NET MVC. The simple class can be also implemented in non ASP.NET MVC projects.
Note: You can change the source parameter to be List or just add a method overload.

PaginatedList Class

using System;
using System.Linq;
using System.Collections.Generic;
 
namespace MyApp.Helpers
{
    public class PaginatedList<T> : List<T>
    {
 
        public int PageIndex { get; private set; }
        public int PageSize { get; private set; }
        public int TotalCount { get; private set; }
        public int TotalPages { get; private set; }
 
        public PaginatedList(IQueryable<T> source, int pageIndex, int pageSize)
        {
            PageIndex = pageIndex;
            PageSize = pageSize;
            TotalCount = source.Count();
 
            TotalPages = (int) Math.Ceiling(TotalCount / (double)PageSize);
 
            this.AddRange(source.Skip((PageIndex) * PageSize).Take(PageSize));
        }
 
        public bool HasPreviousPage
        {
            get {return (PageIndex > 0);}
        }
 
        public bool HasNextPage {
            get {return (PageIndex+1 < TotalPages);}
        }
 
    }
}

Controller

        public ActionResult Index(int? page)
        {
            int pageSize = 15;
            var content = MyAppRepository.FindContentByDate().ToList();
            var pagedContent = new PaginatedList<Content>(content, page ?? 0, pageSize);
 
            return View("Index", pagedContent);
        }

View

        <div id="nav_paging">
        <% if (Model.HasPreviousPage) { %>
        <div class="nav_prev">
            <%= Html.RouteLink("Previous", "Index", new { page = (Model.PageIndex - 1) })%>
        </div>
        <% } %>
 
        <% if (Model.HasNextPage) { %>
        <div class="nav_next">
            <%= Html.RouteLink("Next", "Index", new { page = (Model.PageIndex + 1) })%>
        </div>
        <% } %>
        </div>

Routes in Global.asax

routes.MapRoute(
                "Index",
                "Index/Page/{page}",
                new { controller = "Home", action = "Index", page = 0 }
            );

WordPress Building simple lists using get_posts()

Apr 11, 2009   //   by Michael D. Irizarry   //   PHP, Wordpress  //  1 Comment

It’s really simple to build lists using the get_posts() method. Here’s an example of getting all the posts from a single category.
Read more >>

Random CSS themes give your Social Space a Design Twist

Apr 11, 2009   //   by Michael D. Irizarry   //   CSS, JavaScript, PHP  //  1 Comment

Many sites are using this technique to give a fresh interface on every page the user visits, giving the illusion of a fast passed user experience. Sites like MTV, Hulu, MP3.com and many others use different techniques to change the sites layout either by changing the colors, backgrounds or just converting the site to a huge ad campaign.

I came up with a simple PHP script to Random your Themes. It checks for CSS files on a specified folder and randomizes it. You can also set the parameter (theme.php?file=theme.css) if you want to have a static version. Note: Security has been stripped for clarity, setup up the script with your own security.

PHP

<?php
 
// Themes Folder
$folder = './';
 
// File Type Default CSS
$fileTypes = array();
$fileTypes['css'] = 'text/css';
 
// File
$file = null;
 
// Sanity Check
if (substr($folder,-1) != '/') {
	$folder = $folder.'/';
}
 
// Request Param
if (isset($_GET['file'])) {
	$meta = pathinfo($_GET['file']);
	if (
	    isset( $fileTypes[ strtolower( $meta['extension'] ) ] ) &&
        file_exists( $folder.$meta['basename'] )
    ) {
		$file = $folder.$meta['basename'];
	}
} else {
	$fileList = array();
 
	if (is_dir($folder)) {
	    if ($handle = opendir($folder)) {
	        while (($file = readdir($handle)) !== false) {
				$file_info = pathinfo($file);
				if ( isset( $fileTypes[ strtolower( $file_info['extension'] ) ] ) ) {
					$fileList[] = $file;
				}
	        }
	        closedir($handle);
	    }
	}
 
	if (count($fileList) > 0) {
		$rnd = rand(0, count($fileList)-1);
		$file = $folder.$fileList[$rnd];
	}
}
 
// Header and Content
if ($file != null) {
	$meta = pathinfo($file);
	$contentType = 'Content-type: '.$fileTypes[ $meta['extension'] ];
	header ($contentType);
	readfile($file);
} 
?>

USE

<link rel="stylesheet" href="theme.php" type="text/css" media="screen" />

Twiidate.com – twitter just got a little more personal [Startup]

Apr 4, 2009   //   by Michael D. Irizarry   //   Inspiration, Startups, Web 2.0  //  No Comments

In an effort to do something productive during this weekend I decided to put myself to use and join the Startup Weekend trend. I came up with the following idea Twiidate.com a personals service website completely driven by Twitter users. Since I’m alone doing this I might not finish it by tomorrow but I sure will try. I just finished the logo, bought the domain, setup the server with Media Temple and all the configurations, setup the twitter OAuth and a few more things. I currently have a business model on mind but nothing solid. I’ll keep you updated.

You can follow the updates thru Twiidate twitter account.

Triangle Startup Weekend

Apr 4, 2009   //   by Michael D. Irizarry   //   Inspiration, Web 2.0  //  No Comments

Startup Weekend is jazz for entrepreneurs. Each person improvises using their own instruments and style to create something entirely new and wonderful–all in one brief session. Launching a company from conception to launch creates a business and a space for connections between forward-thinking people in the context of working together. From April 3-5, Edge Office in Raleigh will host the Startup Weekend as entrepreneurs from across the Triangle, NC area gather in this experiment for community and profit.

Triangle Startup Weekend seems to be about breaking the traditional ad business model and shifting to new fresh and creative ideas of getting revenue. There are few interesting videos and ideas that are worth following.

Reading ID3 tags with AS3

Apr 3, 2009   //   by Michael D. Irizarry   //   Flash  //  No Comments

In order to access the ID3 Tags of an MP3 remotely you need to pass the SoundLoaderContext class with the checkPolicyFile property set to true to the Sound class load method. If not Event.ID3 will not be fired since it will throw a security sandbox violation. Also you need to remember one important step that is having a cross domain policy file in the root of the server that will be serving the MP3 files.

AS3 Code

 
var soundLoaderContext:SoundLoaderContext = new SoundLoaderContext();
soundLoaderContext.checkPolicyFile = true;
 
var mp3:URLRequest = new URLRequest("weezer-pork_and_beans.mp3");
 
var sound:Sound = new Sound();
sound.addEventListener(Event.ID3, id3Handler);
sound.load(mp3, soundLoaderContext);
sound.play();
 
// ID3 events
function id3Handler(evt:Event):void {
      var id3:ID3Info = evt.target.id3;
      trace(id3.artist + ' - ' + id3.songName);
}

cross-domain XML

 
<? xml version="1.0">
<cross-domain-policy>
<allow-access-from domain="*.domain.com" /></allow>
</cross>
Pages:12»