<?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>dom111.co.uk &#187; select box</title>
	<atom:link href="http://www.dom111.co.uk/blog/tag/select-box/feed" rel="self" type="application/rss+xml" />
	<link>http://www.dom111.co.uk/blog</link>
	<description>Move along. Nothing to see here.</description>
	<lastBuildDate>Wed, 14 Jul 2010 19:30:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>CakePHP: Select Box Pagination</title>
		<link>http://www.dom111.co.uk/blog/coding/cakephp-select-box-pagination/182</link>
		<comments>http://www.dom111.co.uk/blog/coding/cakephp-select-box-pagination/182#comments</comments>
		<pubDate>Thu, 27 Aug 2009 12:40:54 +0000</pubDate>
		<dc:creator>dom111</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[drop-down]]></category>
		<category><![CDATA[pagination]]></category>
		<category><![CDATA[select box]]></category>

		<guid isPermaLink="false">http://www.dom111.co.uk/blog/?p=182</guid>
		<description><![CDATA[Using CakePHP&#8217;s built in pagination system has saved me so much time in my current day job, but the latest designs I&#8217;m working on have drop-down box style pagination, as it&#8217;s possible to get many pages of results. To change the named parameters in the current URL I&#8217;ve created this small javascript function that will [...]]]></description>
			<content:encoded><![CDATA[<p>Using CakePHP&#8217;s built in pagination system has saved me so much time in my current day job, but the latest designs I&#8217;m working on have drop-down box style pagination, as it&#8217;s possible to get many pages of results.</p>
<p>To change the named parameters in the current URL I&#8217;ve created this small javascript function that will amend the URL:<span id="more-182"></span></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/**
 * setURL
 *
 * Modifies the current URL and redirects the browser
 *
 * @param string key The name of the parameter to set
 * @param mixed value The value to set the parameter to
 * @return void
 * @author Dom Hastings
 */</span>
<span style="color: #003366; font-weight: bold;">function</span> setURL<span style="color: #009900;">&#40;</span>key<span style="color: #339933;">,</span> value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #006600; font-style: italic;">// set up the url separators</span>
  <span style="color: #003366; font-weight: bold;">var</span> separator <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// site.url/controller/action/key1:value1/key2:value2</span>
    <span style="color: #3366CC;">'key'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'/'</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">'value'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">':'</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// get the current url</span>
  <span style="color: #003366; font-weight: bold;">var</span> url <span style="color: #339933;">=</span> window.<span style="color: #660066;">location</span>.<span style="color: #660066;">href</span><span style="color: #339933;">;</span>
  <span style="color: #006600; font-style: italic;">// check if the specified key already exists</span>
  <span style="color: #003366; font-weight: bold;">var</span> exists <span style="color: #339933;">=</span> url.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span>separator.<span style="color: #660066;">key</span> <span style="color: #339933;">+</span> key <span style="color: #339933;">+</span> separator.<span style="color: #660066;">value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// if it does</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>exists <span style="color: #339933;">&gt;</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// find the next separator.key</span>
    <span style="color: #003366; font-weight: bold;">var</span> last <span style="color: #339933;">=</span> url.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span>separator.<span style="color: #660066;">key</span><span style="color: #339933;">,</span> exists <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// if there is one</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>last <span style="color: #339933;">&gt;</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #006600; font-style: italic;">// replcae the existing value with the one passed</span>
      url <span style="color: #339933;">=</span> url.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> exists<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> separator.<span style="color: #660066;">key</span> <span style="color: #339933;">+</span> key <span style="color: #339933;">+</span> separator.<span style="color: #660066;">value</span> <span style="color: #339933;">+</span> escape<span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> url.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span>last<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// if not</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #006600; font-style: italic;">// just append it</span>
      url <span style="color: #339933;">=</span> url.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> exists<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> separator.<span style="color: #660066;">key</span> <span style="color: #339933;">+</span> key <span style="color: #339933;">+</span> separator.<span style="color: #660066;">value</span> <span style="color: #339933;">+</span> escape<span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// if it's not already in there</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// if the URL doesn't end with a separator.key</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>url.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> separator.<span style="color: #660066;">key</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #006600; font-style: italic;">// append it</span>
      url <span style="color: #339933;">+=</span> separator.<span style="color: #660066;">key</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// append the value</span>
    url <span style="color: #339933;">+=</span> key <span style="color: #339933;">+</span> separator.<span style="color: #660066;">value</span> <span style="color: #339933;">+</span> escape<span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// set the url</span>
  window.<span style="color: #660066;">location</span>.<span style="color: #660066;">href</span> <span style="color: #339933;">=</span> url<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Coupled with the following code to my view:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;!-- Items per page: --&gt;
&lt;select onchange=&quot;setURL('limit', this.value)&quot;&gt;
  &lt;option value=&quot;10&quot;&lt;?=($params['named']['limit'] == '10') ? ' selected=&quot;selected&quot;' : ''?&gt;&gt;10&lt;/option&gt;
  &lt;option value=&quot;20&quot;&lt;?=($params['named']['limit'] == '20') ? ' selected=&quot;selected&quot;' : ''?&gt;&gt;20&lt;/option&gt;
  &lt;option value=&quot;50&quot;&lt;?=($params['named']['limit'] == '50') ? ' selected=&quot;selected&quot;' : ''?&gt;&gt;50&lt;/option&gt;
  &lt;option value=&quot;100&quot;&lt;?=($params['named']['limit'] == '100') ? ' selected=&quot;selected&quot;' : ''?&gt;&gt;100&lt;/option&gt;
&lt;/select&gt;
&nbsp;
&lt;!-- Page: --&gt;
&lt;select onchange=&quot;setURL('page', this.value)&quot;&gt;
&lt;?php for ($i = 1; $i &lt;= $paginator-&gt;counter(array('format' =&gt; '%pages%')); $i++): ?&gt;
  &lt;option value=&quot;&lt;?=$i?&gt;&quot;&lt;?=($params['named']['page'] == $i) ? ' selected=&quot;selected&quot;' : ''?&gt;&gt;&lt;?=$i?&gt;&lt;/option&gt;
&lt;?php endfor ?&gt;
&lt;/select&gt;</pre></div></div>

<p>Hopefully it&#8217;ll help anyone else deal with a similar problem!</p>
<p><strong>Note</strong>: If there&#8217;s a simpler, more accessible way to do it, I&#8217;d really like to know!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dom111.co.uk/blog/coding/cakephp-select-box-pagination/182/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
