<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Connecting the dots... &#187; pagination</title>
	<atom:link href="http://blog.rajatpandit.com/tag/pagination/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.rajatpandit.com</link>
	<description>Thoughts on Web Development, Infrastructure and Application Scalability</description>
	<lastBuildDate>Thu, 29 Dec 2011 13:21:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>sfPropelPager and pagination</title>
		<link>http://blog.rajatpandit.com/2009/01/04/sfpropelpager-and-pagination/</link>
		<comments>http://blog.rajatpandit.com/2009/01/04/sfpropelpager-and-pagination/#comments</comments>
		<pubDate>Sun, 04 Jan 2009 20:18:19 +0000</pubDate>
		<dc:creator>rp</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[pagination]]></category>
		<category><![CDATA[sfPropelPager]]></category>
		<category><![CDATA[Symfony]]></category>

		<guid isPermaLink="false">http://blog.rajatpandit.com/?p=106</guid>
		<description><![CDATA[I am writing this post just after finishing 30mins of really annoying problems with sfPropelPager and implementing a pager partial, which can be re-used though out the project. The problem...]]></description>
			<content:encoded><![CDATA[<p>I am writing this post just after finishing 30mins of really annoying problems with sfPropelPager and implementing a pager partial, which can be re-used though out the project. The problem that I was facing was that there is no way (or at-least I don&#8217;t know off) to ensure that the current params from the browser are also passed as the user paginates from page to page.<br />
So here I was dealing with two situations:</p>
<ul>
<li>
User posts in a search box (GET request) and the parameters are passed like</p>
<pre class="brush: bash; title: ; notranslate">
search/index?q=foo&amp;p=10
</pre>
<p>Now as the results appears on the page I want the following links for my pager:</p>
<pre class="brush: bash; title: ; notranslate">
search/index?page=1&amp;q=foo&amp;p=10
search/index?page=2&amp;q=foo&amp;p=10
</pre>
<p>and so on. </li>
<li>
<p>Where I have a neat url coming from symfony already. Like.</p>
<pre class="brush: bash; title: ; notranslate">
website/show/flickr.html
</pre>
<p>In this case I want urls to appear as</p>
<pre class="brush: bash; title: ; notranslate">
website/show/flickr/1
website/show/flickr/2
</pre>
<p>and so on.
</li>
</ul>
<p>I dont claim this is the best way to do, if you think there is one, please leave a comment, however for others who have spent some time trying to figure this out, this might get them up and running quickly and then then an improvise this further. So here&#8217;s my solution.</p>
<p>Code form the Partial (_pager.php)</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
$query_string = '';
$pager_param_name = 'page';
// use this value in case you are getting your parameters from a get request
// e.g. search where you are showing results as part of submitting a get form
$query_params = $sf_request-&gt;getGetParameters();
// use this when you already have a proper fancy url in place
// and you want to get the internal rep of the query params.
// this doesnt return any query params if the intial request is sent thru
// the query string (e.g. in the above case this would return nothing after ? in the url)
$uri          = sfContext::getInstance()-&gt;getRouting()-&gt;getCurrentInternalUri();

$args = explode('?', $uri);
if (isset($args[1])) {
  $params = $args[1];
} else {
  $params   = $sf_request-&gt;getGetParameters();
  $counter  = 0;
  $len      = count($params);
  $tmp      = '';
  foreach($params as $key =&gt; $value) {
    $tmp .= $key . '=' . $value;
    $counter +=1;
    if ($counter &lt; $len) {
      $tmp .= '&amp;';
    }
  }
  $params = $tmp;
}
// fancy reg to make sure we dont reset the value for the pager param again
$query_string = eregi_replace('(&amp;){0,1}' . $pager_param_name . '=[0-9]{1,}(&amp;){0,1}', '', $params);

if ('' != $query_string) {
  $query_string = '&amp;' . $query_string;
}
?&gt;

&lt;div class=&quot;mod pager&quot;&gt;
    &lt;div class=&quot;bd&quot;&gt;
        &lt;?php if ($pager-&gt;haveToPaginate()): ?&gt;
        &lt;ol&gt;
            &lt;li&gt;&lt;?php echo link_to('&amp;laquo;', $route .'?' . $pager_param_name. '='.$pager-&gt;getFirstPage() . $query_string) ?&gt;&lt;/li&gt;
            &lt;li&gt;&lt;?php echo link_to('&amp;lt;', $route . '?' . $pager_param_name. '='.$pager-&gt;getPreviousPage() . $query_string) ?&gt;&lt;/li&gt;
                &lt;?php $links = $pager-&gt;getLinks(); foreach ($links as $pagecount): ?&gt;
                    &lt;li&gt;
                        &lt;?php echo ($pagecount == $pager-&gt;getPage()) ?
                                        '&lt;span class=&quot;current&quot;&gt;' . $pagecount . '&lt;/span&gt;' :
                                        link_to($pagecount, $route . '?' . $pager_param_name. '='.$pagecount . $query_string)
                        ?&gt;
                    &lt;/li&gt;
                &lt;?php endforeach ?&gt;
            &lt;li&gt;&lt;?php echo link_to('&amp;gt;', $route . '?' . $pager_param_name. '='.$pager-&gt;getNextPage(). $query_string) ?&gt;&lt;/li&gt;
            &lt;li&gt;&lt;?php echo link_to('&amp;raquo;', $route . '?' . $pager_param_name. '='.$pager-&gt;getLastPage() . $query_string) ?&gt;&lt;/li&gt;
         &lt;/ol&gt;
        &lt;?php endif ?&gt;
    &lt;/div&gt;
&lt;/div&gt;
</pre>
<h2>Code Inside the action class</h2>
<pre class="brush: php; title: ; notranslate">
    public function executeShow(sfWebRequest $request)
    {
        $pager = UsernamePeer::getListByWebsitePager($website, $count);
        $pager-&gt;setPage($this-&gt;getRequestParameter('page',1));
        $pager-&gt;init();
        $this-&gt;pager = $pager;
}
</pre>
<p>And then finally the template that calls the partial</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php include_partial('global/username_list', array('pager' =&gt; $pager, 'route' =&gt; 'website/show'))?&gt;
</pre>
<div id="in_post_ad_bottom_1" style="clear:both;margin:0;padding:0;"><div class="brp-bp-234">
<script type="text/javascript"><!--
google_ad_client = "ca-pub-4254382394977039";
/* brp-234x60-bp */
google_ad_slot = "7787511801";
google_ad_width = 234;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>
<div class="brp-bp-234">
<script type="text/javascript"><!--
google_ad_client = "ca-pub-4254382394977039";
/* brp-234x60-BP-1 */
google_ad_slot = "9111022353";
google_ad_width = 234;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div></div><div style='clear:both'></div>]]></content:encoded>
			<wfw:commentRss>http://blog.rajatpandit.com/2009/01/04/sfpropelpager-and-pagination/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

