I am trying to create custom WordPress loop

I am trying to implement custom loop using WP_query but the problem I am having is in pagination function. I have tried a lot to figure it out but now I need someone advice to resolve my issue.

{
$showitems = ($range * 2)+1;

global $paged;
if(empty($paged)) $paged = 1;

if($pages == '')
{
    global $wp_query;
    $pages = $wp_query->max_num_pages;
    if(!$pages)
    {
        $pages = 1;
    }
}

if(1 != $pages)
{
    echo "<div class=\"pagination\"><span>Page ".$paged." of ".$pages."</span>";
    if($paged &gt; 2 &amp;&amp; $paged &gt; $range+1 &amp; $showitems &lt; $pages) echo "<a href='".get_pagenum_link(1)."'>&laquo; First</a>";
    if($paged &gt; 1 &amp;&amp; $showitems &lt; $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo; Previous</a>";

    for ($i=1; $i &lt;= $pages; $i++)
    {
        if (1 != $pages &amp;&amp;( !($i &gt;= $paged+$range+1 || $i &lt;= $paged-$range-1) || $pages &lt;= $showitems ))
        {
            echo ($paged == $i)? "&lt;span class=\"current\"&gt;".$i."&lt;/span&gt;":"<a href='".get_pagenum_link($i)."'>".$i."</a>";
        }
    }

    if ($paged &lt; $pages &amp;&amp; $showitems &lt; $pages) echo "<a>Next &rsaquo;</a>";
    if ($paged &lt; $pages-1 &amp;&amp;  $paged+$range-1 &lt; $pages &amp;&amp; $showitems &lt; $pages) echo "<a href='".get_pagenum_link($pages)."'>Last &raquo;</a>";
    echo "&lt;/div&gt;\n";
}

}

I have use this pagination WordPress article as reference.